hi all,i just learn about newton raphson method and im thinking to create a code for it.(im not expert in c++,so my code could be wrong)
For the function and it derivative,i hard code it in my program.
im using:
f(x)=-0.6x^2+2.4x+5.5
f'(x)=-1.2x+2.4
eps=0.00001
initial guess=input by user
the problem is something wrong with the answer.Can someone assist me???
For the function and it derivative,i hard code it in my program.
im using:
f(x)=-0.6x^2+2.4x+5.5
f'(x)=-1.2x+2.4
eps=0.00001
initial guess=input by user
the problem is something wrong with the answer.Can someone assist me???
Code:
#include<iostream>
#include<cmath>
using namespace std;
double NRM(double x0,double eps);
double f_one(double x);
double f_one_prime(double x);
double equation_1(double x0);
double temp_criterion(double x0,double x_new);
int main()
{
double x0,root,eps=0.00001;
cout<<"enter the initial guess:";
cin>>x0;
if((f_one(x0)!=0) && (f_one_prime(x0)!=0))
{
root=NRM(x0,eps);
}
cout<<"Root is taken as:" << root<<endl;
getchar();
getchar();
}
double f_one(double x)
{
return(-0.6*x*x+2.4*x+5.5);
}
double f_one_prime(double x)
{
return(-1.2*x+2.4);
}
double NRM(double x0,double eps)
{
double x_new,c_t,i=0;
x_new=0;
c_t=temp_criterion(x0,x_new);
do
{
x_new=equation_1(x0);
c_t=temp_criterion(x0,x_new);
cout<<"i="<<i++<<"|"<<"xi="<<x0<<"|"<<"temp criterion="<<c_t<<endl;
x0=x_new;
}while(c_t>eps);
return(x_new);
}
double temp_criterion(double x0,double x_new)
{
return((abs(x_new-x0))/abs(x_new));
}
double equation_1(double x0)
{
double term;
term=f_one(x0)/f_one_prime(x0);
return(x0-term);
}
Comment