Numerical Approximation

Hi,

I have to code two numerical approximations and compare them by giving them two different values of h, namely h1 and h2. I have already written the code, but the only output it gives me is the value of i and x. Here is my code:

#include<iostream>
#include<cmath>
using namespace std;

double R(double x)
{
double y;
do {
y=1/((25*x*x)+1);
} while (x>=-1 && x<=1);
return y;
}

double D(double x)
{
double y;
do {
y=-(50*x)/(R(x)*R(x));
}while (x>=-1 && x<=1);
return y;
}


int main()
{
double h1=0.01;
double h2=0.02;
double x;
double y;
double z;
int i;
cout<< "Enter a value i:";
cin >> i;
do
{
x=-1+(i*0.125);
cout << "x=" << x;
y=((exp(h1*D(x))-R(x)))/h1;
cout << "y=" << y;
z=(exp(h2*D(x))-exp(-h2*D(x)))/(2*h2);
cout << "z=" << z;
cout<< "The numerical approximation using the forward difference operator is=" << y <<"\n";
cout<< "The numerical approximation using the central and average difference operator is=" << z <<"\n";
}while(i>=0 && i<=16);

return 0;
}

I would really appreciate if you gave me a hint, where the mistake could be.
You have been asked before to use code tags. PLEASE DO SO.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
If you're not going to make the slightest bit of effort to make your posts readable, why should we spend the slightest bit of effort helping you?
I will not respond further until you apply code tags.
Why exactly do you have a loop inside your R() and D() functions? x doesn't change.

D() looks as if it is intended to be the derivative of R(). Why?

Your expression for y is bizarre. If that is supposed to approximate the derivative of R(x) then it would be
y = (R(x+h1)-R(x))/h1;
z = (R(x+h2)- R(x-h2))/(2*h2);
No idea why you want an exponential function there. (EDIT: I've just realised what you might be doing, but I can assure you that you have got completely the wrong end of the stick). And why should h2 be any different from h1?

Given that i doesn't change why have you put a do-while loop in the main function? Actually, what is the purpose of i here anyway? If you want to loop through a series of values of x then use a for-loop (and change your output statements).

Please explain exactly what you are trying to do.

Last edited on
Ok, so I got rid of the loop in the two of the functions defined. You're right! It doesn't make any sense if x is given in the main function. I made some changes to the code and before running it, I calculated myself. It produces the same output as mine, but the problem now is that the output runs infinitely!

Here is my code:
#include<iostream>
#include<cmath>
using namespace std;

double R(double x)
{
double y;
y=1.0/((25*x*x)+1);
return y;
}

double D(double x)
{
double y=-(50*x)/(((25*x*x)+1)*((25*x*x)+1));
return y;
}

double for_op(double x)
{
double h1=1;
double y= (exp(h1*-(50*x)/(((25*x*x)+1)*((25*x*x)+1)))-(1.0/((25*x*x)+1)));
return y;
}

double cent_av(double x)
{
double h2=2;
double y;
y=exp(h2*-(50*x)/(((25*x*x)+1)*((25*x*x)+1)))-exp(-h2*-(50*x)/(((25*x*x)+1)*((25*x*x)+1)));
return y;
}


int main()
{
int i;
double h1=1;
double h2=2;
double x;
double y;
double z;
cout<< "Enter a value i:";
cin >> i;
while(i>=0 && i<=16)
{
x=-1+(i*0.125);
cout << "x=" << x<< endl;
cout << "Runge(x)=" << R(x) << endl;
cout << "Derivative of Runge(x)=" << D(x) << endl;
y=for_op(x)/h1;
z= cent_av(x)/(2*h2);
cout <<"The numerical approximation using the forward difference operator is=" << y <<"\n";
cout<< "The numerical approximation using the central and average difference operator is=" << z <<"\n";

};

Ps: In the main function there is a loop because the output has to produce values for x such that with its definition i is one value is the set {0,1,2,...,16}.
Of course it runs for ever. You NEVER CHANGE THE VALUE OF i.

Just use a for-loop, with i going from 0 to 16. You don't need to input i.

You are still refusing to say exactly what you are trying to do. Without revealing that there is precious little one can do to help you. I would hazard a guess that you were trying to numerically approximate the derivative of R(x), but if so then you've taken a wrong turn.

And PLEASE USE CODE TAGS.
Last edited on
Topic archived. No new replies allowed.