Newtons forward | Backward | Raphson Method | Lagrange Interpolation Formula

Newtons forward and Backward Interpolation Formula

Newtons forward | Backward | Raphson Method | Lagrange Interpolation Formula:          




   /*Newtons forward interpolation formula.*/



Newtons forward interpolation:


#include<math.h>
#include<stdio.h>
main()
{
float x[15],y[15],X,p=1,u,h,d;
int i,j,n;
printf("\t\t Output\n");
printf("\nEnter the no. of points: ");
scanf("%d",&n);
printf("\n Enter the value of X at which y is reqd. ");
scanf("%f",&X);
printf("Enter the values of x and y :\n\n");
printf("x\ty=f(x)\n");
for(i=1;i<=n;i++)
scanf("%f\t%f",&x[i],&y[i]);
h=x[2]-x[1];
u=(X-x[1])/h;
d=y[1];
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
y[j]=y[j+1]-y[j];
}
p=p*(u-i+1)/i;
d=d+p*y[1];
}
printf("\n\nThe result of Newtons forward interpolation\n\n");
printf("y(%1.2f)=%3.5f\n",X,d);
}





/*Newtons Backward interpolation formula*/




Newtons Backward interpolation:

#include<math.h>
#include<stdio.h>
main()
{
float x[15],y[15],X,p=1,u,h,d;
int i,j,k,n;
printf("\t\t Output\n");
printf("\nEnter the no. of points: ");
scanf("%d",&n);
printf("enter the value of X at which y is reqd ");
scanf("%f",&X);
printf("Enter the values of x and y :\n");
printf("x\ty=f(x)\n");
for(i=1;i<=n;i++)
scanf("%f\t%f",&x[i],&y[i]);
h=x[2]-x[1];
u=(X-x[n])/h;
d=y[n];
for(i=1;i<=n-1;i=i+1)
{
for(j=n;j>=i+1;j=j-1)
{
y[j]=y[j]-y[j-1];
}
p=p*(u+i-1)/i;
d=d+p*y[n];
}
printf("\n\nThe result of Newtons backward interpolation\n");
printf("y(%f)=%f\n",X,d);
}






/*Lagrange Interpolation Formula*/



Lagrange Interpolation Formula:


#include<math.h>
#include<stdio.h>
main()
{
float x[15],y[15],sum=0.0,X,prod;
int i,j,n;
printf("\t\t Output\n");
printf("\nEnter the no. of points: ");
scanf("%d",&n);
printf("enter the value of X at which y is reqd. ");
scanf("%f",&X);
printf("Enter the values of x and y :\n");
printf("\nx\ty=f(x)\n");
for(i=1;i<=n;i++)
scanf("%f\t%f",&x[i],&y[i]);
for(i=1;i<=n;i++)
{
prod=y[i];
for(j=1;j<=n;j++)
{
if(j!=i)
prod=prod*(X-x[j])/(x[i]-x[j]);
}
sum=sum+prod;
}
printf("\nAt x=%f,\n \n y=%f\n",X,sum);
}








/* Newton Raphson Method*/




Newton Raphson Method:

#include<math.h> #include<stdio.h> float f(float x)
{
return (x*x*x-x-0.1);
}
float f1(float x)
{
return (3*x*x-1);
}
main()
{
float a,b,e,x0,x1;
int n=0;
printf("\n Enter the accuracy: "); scanf("%f",&e);
do
{
printf("\n Enter interval a,b : "); scanf("%f%f",&a,&b);
}while(f(a)*f(b)>0);
x0=(a+b)/2;
printf("\nx0=%f",x0);
do
{
x1=x0-(f(x0)/f1(x0));
x0=x1;
printf("\n n=%d \t x0=%f",n,x0);
n=n+1;
}while(fabs(f(x0))>e);
printf("\n Iteration=%d\tRoot=%f", n,x0);
}






Post a Comment

2 Comments