The problem is as follows:
Your task is to find the four solutions to the equation sin(x) = exp(-0.4x) in the region from zero to ten radians.
This is accomplished with iteration and interpolation.
The output from my program is shown below:
The 4 solutions to sin(x) = exp(-0.4x) starting from x = 0 are:
Solution No. 1:
x (iteration only) = 0.809 with an error of -5.77136e-005
x (with interpolation) = 0.808941 with an error of -2.32525e-008
Solution No. 2:
x (iteration only) = 2.811 with an error of 0.00024367
x (with interpolation) = 2.8107 with an error of -3.94877e-008
Solution No. 3:
x (iteration only) = 6.362 with an error of -0.000244374
x (with interpolation) = 6.36176 with an error of -8.23243e-009
Solution No. 4:
x (iteration only) = 9.402 with an error of 0.00048913
x (with interpolation) = 9.40151 with an error of -3.37411e-009
So far I have written coding to find the solution for the first iteration. I'm not sure how to use the range, (or how to show this in radians); also, my solution is not correct. Can anybody tell me what my problem is?
Lastly, should I use a for loop to show the 4 solutions, or is there something else I should be doing?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
//Iteration and Interpolation
//David Karayof
//October 25, 2013
#include <cmath>
#include <iostream>
using namespace std;
double Fx(double x);
double interp(double x, double step);
int main ()
{
double x= 0, step = 0.001 ;
while (Fx(x) > 0.0)
x = x + step;
cout << "\n x (iteration only) = " << x;
cout << "\n with an error of " << Fx(x);
x = interp(x,step);
cout << "\n\n x (with interpolation) = " << x;
cout << "\n with an error of " << Fx(x);
cout << "\n\n";
return 0;
}
double Fx(double x)
{
return((sin(x)) - (exp(-0.4 * x)));
}
double interp(double x, double step)
{
double ratio;
ratio = - Fx(x-step)/Fx(x);
return (x - step + ratio * step /(1 + ratio));
}
|