Hello I am trying to programme c to use the newton raphson method to evaluate roots of an equation.
My code is as follows:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double function(double );
double function2(double );
int main()
{
double x[100];
double fx[100];
double gradx[100];
int n1=0;
x[0]=0;
n1=-1;
do
{
n1++;
fx[n1]=function(x[n1]);
gradx[n1]=function2(x[n1]);
x[n1+1]=x[n1]-(fx[n1]/gradx[n1]);
}
while(fx[n1+1]<1e-8||(fx[n1+1]>-1e-8));
for(n1=0;n1<100;n1++)
{
printf("%g\t %g\t %g\n",x[n1],fx[n1],gradx[n1]);
}
return 0;
}
double function(double argument1)
{
double result;
double a;
a=0.4+(8039.0/25000);
result = sin(pow(argument1,a)-pow(argument1,(1/a))+(a*argument1));
return result;
}
double function2(double argument1)
{
double result2;
double aa;
aa=0.4+(8039.0/25000);
result2=((aa*pow(argument1, aa-1))-(1/aa*pow(argument1, (1/aa)-1))+aa)*sin(pow(argument1,aa)-pow(argument1,(1/aa))+(aa*argument1));
return result2;
}
I'm getting an answer out but don't think it is the correct one. Any advice on where I am going wrong would be appreciated.