- #1
ggeo1
- 63
- 0
Hello , i have a Newton raphson algorithm.
The problem is that it isn't making the loop and of course not giving a result.
I tried two cases.
One with " if ((f1>eps) && (fd1>eps)) {
while ((error>eps) "
and one instead of the above " while ((error>eps) && (fd1>eps) && (f1>eps)) "
but nothing works.
Any suggestions?
The problem is that it isn't making the loop and of course not giving a result.
I tried two cases.
One with " if ((f1>eps) && (fd1>eps)) {
while ((error>eps) "
and one instead of the above " while ((error>eps) && (fd1>eps) && (f1>eps)) "
but nothing works.
Any suggestions?
Code:
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
double function(double); // declare of function
double fderivative(double);//declare of function
double error=10.0;//initial value to error in order to compare with tolerance
double eps=1e-6; //my error tolerance
int main()
{
double x1,x2,f1,f2,fd1,error;
cout << "Give x1 , x(-4,-3) : "<<endl;
cin >>x1;
int i=0;
f1=function(x1);
f2=function(x2);
fd1=fderivative(x1);
cout << "The function f(x1)= " << f1 << endl;
cout << "The function f'(x1)= " << fd1 <<endl;
while ((error>eps) && (fd1>eps) && (f1>eps))
{
x2=x1 - (f1/fd1);
x1=x2;
error = fabs((x1-x2)/x1);
i++;
cout << "\n f(x2) is : " << f2 << "\t and the root is : " << x2<<endl;
}
cout << "\nThe root is : " <<x2 <<"\tand the number of iterations was :" << i <<endl;
return 0;
}
double function(double x) // my function
{
double y;
y =exp(x)-sin(x);
return y;
}
double fderivative(double x)
{
double y;
y=exp(x)-cos(x);
return y;
}