Newton Raphson Interpolation Formula implementation in C.
The code below finds the root of linear algebraic any order equation by "Newton-Raphson" method
Code: C
#include <stdio.h>
#include <conio.h>
#define N 100
float comb(float,int);
/*****************************************************************/
/***** Newton-raphson-interpolation formula implementation *****/
/***** This program uses the forward difference formula *****/
/*****************************************************************/
void main()
{
/* y=y[0]+rC1*dely[0]+rC2*del2y[0]+rC3*del3y[0]+...+rCn*delny[0] */
/* where r is given as ==> x=x[0]+r*h */
float x[N],y[N],n,temp[N],res,r,X,h;
int i,j;
char ch;
do
{
printf("\nEnter the total number of points for interpolation\n");
scanf("%f",&n);
printf("\nEnter the values of x\n");
for(i=0;i<n;i++)
scanf("%f",&x[i]);
h=x[1]-x[0];
printf("\nEnter the values of y\n");
for(i=0;i<n;i++)
scanf("%f",&y[i]);
res=y[0];
printf("\nEnter the values of x to find y\n");
scanf("%f",&X);
r=(X-x[0])/h;
for(i=1;i<n;i++)
{
for(j=0;j<n;j++)
temp[j]=y[j+1]-y[j];
for(j=0;j<n;j++)
y[j]=temp[j];
res=res+comb(r,i)*temp[0];
}
printf("\nThe answer is %g\n",res);
printf("\nDo you wish to continue[y/n]\n");
ch=getche();
}while(ch=='Y' || ch=='y');
printf("\nPress any key to exit\n");
}
float comb(float n,int r)
{
int i;
float res=1;
for(i=1;i<=r;i++)
res=res*(n-i+1)/i;
return(res);
}
Code: C
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#define ACC 0.01
#define N 10
int degree;
float coeff[N];
void input(void);
float F(float);
float dF_dx(float);
/********************************************************/
/***** Finds the root of any linear algebraic any *****/
/***** order equation by "Newton-Raphson" method *****/
/********************************************************/
void main()
{
char ch;
do
{
input();
printf("Do you wish to continue[y/n]\n");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='Y' || ch=='y');
}
void input(void)
{
int i,print,loop=0;
float ig;
float h;
printf("\nEnter the degree of diferential equation\n");
scanf("%d",°ree);
print=degree;
for(i=0;i<=degree;i++)
{
printf("Enter the co-efficient of x to the power of %d\t",print--);
scanf("%f",&coeff[i]);
}
printf("\nEnter the initial guess\n");
scanf("%f",&ig);
if(F(ig)==0)
printf("\n%f is the root of the equation\n",ig);
do
{
h=-(F(ig))/(dF_dx(ig));
ig=ig-h;
loop++;
if(loop>=40)
exit(0);
}while(fabs(h)>=ACC);
printf("\n%f is the root\n",ig);
}
float dF_dx(float x)
{
int i,deg;
float func=0.0;
deg=degree;
for(i=0;i<degree;i++)
{
func=func+coeff[i]*deg*pow(x,(deg-1));
deg--;
}
if(func==0)
return(1);
return(func);
}
float F(float x)
{
int i,deg;
float func=0.0;
deg=degree;
for(i=0;i<=degree;i++)
{
func=func+coeff[i]*pow(x,deg);
deg--;
}
return(func);
}


