0% found this document useful (0 votes)
121 views24 pages

Program 1: Te A Program To Implement Bisec

The document contains 12 programs demonstrating various numerical methods for solving equations including: - Bisection method - Regular Falsi method - Iteration method - Secant method - Newton-Raphson method - Gauss elimination method - Euler's method - Trapezoidal rule - Simpson's 1/3 rule - Simpson's 3/8 rule - Booles method - Gauss-Seidel method The programs contain functions to implement the respective numerical methods, take user input for parameters, perform calculations, and output the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views24 pages

Program 1: Te A Program To Implement Bisec

The document contains 12 programs demonstrating various numerical methods for solving equations including: - Bisection method - Regular Falsi method - Iteration method - Secant method - Newton-Raphson method - Gauss elimination method - Euler's method - Trapezoidal rule - Simpson's 1/3 rule - Simpson's 3/8 rule - Booles method - Gauss-Seidel method The programs contain functions to implement the respective numerical methods, take user input for parameters, perform calculations, and output the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

PROGRAM 1

//Write a program to implement bisection method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 10
float f(float x);
void main()
{
float a,b,c,d,t;
int i,j,n;
clrscr();
printf("\nEnter the number of iteration : ");
scanf("%d",&n);
for(i=-max;i<max;i++)
{
a=i;
b=i+1;
if(f(a)*f(b)<=0)
{
if(f(a)>f(b)) {t=a;a=b;b=t;}
printf("\nThe initial approximate limits are : ");
printf("%f%f",a,b);
printf("\n\n a b c f(c)");
for(j=0;j<n;j++)
{
c=(a+b)/2;
printf("\n\n %10.6f %10.6f %10.6f %10.6f",a,b,c,f(c));
if(f(c)<0) a=c;else b=c;
}
getch();
}
}
}
float f(float x)
{
return(cos(x)-3*x+1);
}
PROGRAM 2

//Write a program to implement Regular Falsi method

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return cos(x) - x*exp(x);
}
void regula (float *x, float x0, float x1, float fx0, float fx1, int
*itr)
{
*x = x0 - ((x1 - x0) / (fx1 - fx0))*fx0;
++(*itr);
printf("Iteration no. %3d X = %7.5f \n", *itr, *x);
}
int main ()
{
int itr = 0, maxmitr;
float x0,x1,x2,x3,allerr;
clrscr();
printf("\nEnter the values of x0, x1, allowed error and maximum
iterations:\n");
scanf("%f %f %f %d", &x0, &x1, &allerr, &maxmitr);
regula (&x2, x0, x1, f(x0), f(x1), &itr);
do
{
if (f(x0)*f(x2) < 0)
x1=x2;
else
x0=x2;
regula (&x3, x0, x1, f(x0), f(x1), &itr);
if (fabs(x3-x2) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x3);
getch();
return 0;
}
x2=x3;
}
while (itr<maxmitr);
printf("Solution does not converge or iterations not sufficient:\n");
getch();
return 1;
}
PROGRAM 3
//Write a program to implement Iteration method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 10
float f(float x);
void main()
{
float a,b,x,d;
int i,j,n;
clrscr();
printf("Enter the number of iterations : ");
scanf("%d",&n);
printf("/nn x i(c)");
for(j=0;j<n;j++)
{
printf("\n%d %10.6f %10.6f",j+1,x,f(x));
x=f(x);
}
getch();
}
float f(float x)
{
return((cos(x)+1)/3);
}
PROGRAM 4
//Write a program to implement secant method
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(x*x*x-4);
}
float main()
{
float a,b,c,d,e;
int count=1,n;
clrscr();
printf("\nEnter the values of a and b : ");
scanf("%f%f",&a,&b);
printf("Enter the values of allowed error and maximum number : ");
scanf("%f%d",&e,&n);
do
{
if(f(a)==f(b))
{
printf("\nSolution cannot be found as the values of a and b");
return 0;
}
c=(a*f(b)-b*f(a))/(f(b)-f(a));
a=b;
b=c;
printf("Iteration No-%d x=%f\n",count,c);
count++;
if(count==n)
{
break;
}
}
while(fabs(f(c))>e);
printf("\nThe Required solution is %f\n",c);
getch();
}
PROGRAM 5
//Write a program to implement Newton Rapshon method
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(cos(x)-3*x+1);
}
float f1(float y)
{
return(-sin(y)-3);
}
void main()
{
float a,b,c,d;
int i,j,n;
clrscr();
printf("\nEnter the number of iterations : ");
scanf("%d",&n);
for(i=-8;i<8;i++)
{
a=1;
b=i+1;
if(f(a)*f(b)<=0)
{
printf("\nThe initial approximate value is : ");
c=(a+b)/2.0;
printf("%f",c);
printf("\nn c f(c)");
for(j=0;j<n;j++)
{
printf("\n%-5d%15.6f%15.6f",j+1,c,f(c));
d=c-(f(c)/f1(c));
c=d;
}
getch();
}
}}
PROGRAM 6
//Write a program to implemet Gauss Elimination method
#include<stdio.h>
#include<conio.h>
#include<math.h>
void matrix(float mat[10] [11],int n);
void swap(float mat[10][11],int n,int r1,int r2);
void main()
{
float mat[10][11],out[10],ratio,s;
int i,j,k,n,flag=0,r=0;
clrscr();
textcolor(WHITE);
textbackground(BLUE);
gotoxy(10,5);
printf("Enter the order of matrix : ");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n+1;j++)
{
gotoxy(10*j+10,i+7);
if(j<n) printf("%c*",'z'-n+j+1);
scanf("%f",&mat[i][j]);
gotoxy(16+10*j,i+7);
if(j<n-1)printf("+");
else if(j==n-1) printf("=");
}
matrix(mat,n);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
ratio=mat[j][i]/mat[i][i];
for(k=0;k<n+1;k++)
mat[j][k] -= ratio*mat[i][k];
}
gotoxy(10,13);
printf("echolon form:-");
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
gotoxy(10+10*j,i+14);
printf("%-10.3f",mat[i][j]);
if(mat[i][j] !=0) flag=1;
}
if(flag!=0) {r++;flag=0;}
}
if(r!=n)
{
gotoxy(10, ++i+14);
printf("\nEquation has more than one solution ");
}
for(i=n-1;i>=0;i--)
{
s=0;
for(j=n-1;j>1;j--)
{
s += mat[i][j]*out[j];
}
s=mat[i] [n]-s;
out[i]=s/mat[i][j];
}
for(i=0;i<n;i++)
{
gotoxy(15*i+10,21);
printf("%c = %-10.3f",'z'-n+i+1,out[i]);
}
getch();
getch();
}
void matrix(float mat[10][11],int n)
{
int i=0,j=0;
do
{
if((mat[i][i]!=mat[j][i]) && (mat[i][i]==0) && (j<n))
swap(mat,n,i,j);
j++;
if(j%n==0) j=++i;
}while(i<n);
}
void swap(float mat[10][11],int n,int r1,int r2)
{
float temp;
int i;
for(i=0;i<=n;i++)
{
temp=mat[r1][i];
mat[r1][i]=mat[r2][i];
mat[r2][i]=temp;
}
}
PROGRAM 7

//Write a program to implement Euler's method


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x,float y)
{
return(y*y-x*x);
}
void main()
{
float h,x,y,y0,x0,y1;
clrscr();
printf("\nenter initial approximate x0,y0&interval width h:");
scanf("%f%f%f",&x0,&y0,&h);
printf("enter the value of x at which y is required:");
scanf("%f",&x);
printf("\n\t x \t y");
do
{
y=h*f(x0,y0);
y1=y+y0;
printf("\n%f%f",x0,y1);
x0=x0+h;
y0=y1;
}
while(x0<x);
getch();
}
PROGRAM 8

//Write a program to implement trapezoidal rule


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x);
void main()
{
float k=0.0,h,x,a,b;
int i;
clrscr();
printf("the function f(x):/(1+x*x)");
printf("\nenter the lower and upper limits=");
printf("\na=");
scanf("%f",&a);
printf("b=");
scanf("%f",&b);
printf("enter the difference 'h'=");
scanf("%f",&h);
printf("\n x f(x)");
for(i=0,x=a;x<b-h;x=x+h,i++)
{
k+=f(x)+f(x+h);
printf("\n%.6f%15.6f",x,f(x+h));
}
printf("\n%f%15.6f",x,f(x+h));
printf("\n\nthe definite integral is %15.f",(h/2.0)*k);
getch();
}
float f(float x)
{
return(1/(1+x*x));
}
PROGRAM 9
//Write a program to implement Simpson 1/3 rule
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x);
void main()
{
float k=0.0,h,x,a,b;
int i,n;
clrscr();
printf("the function f(x):1/(1+x*x)");
printf("\nenter the lower and upper limits=");
printf("\na=");
scanf("%f",&a);
printf("b=");
scanf("%f",&b);
printf("enter the number of strips(in multiple of '2') = ");
scanf("%d",&n);
h=(b-a)/(2*(n/2));
printf("\n x f(x)");
for(x=a,i=0;x<b;x=x+2*h,i=i+2)
{
k+=f(x)+4*f(x+h)+f(x+2*h);
printf("\n%.6f%15.6f",x,f(x));
printf("\n%.6f%15.6f",x+h,f(x+h));
}
printf("\n\nthe definite integral is %15.6f",(h/3.0)*k);
getch();
}
float f(float x)
{
return(1/(1+x*x));
}
PROGRAM 10

//Write a program to implement Simpson 3/8 rule


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x);
void main()
{
float k=0.0,h,x,a,b;
int i,n;
clrscr();
printf("the function f(x):1/(1+x*x)");
printf("\nenter the lower and upper limits=");
printf("\na=");
scanf("%f",&a);
printf("b=");
scanf("%f",&b);
printf("enter the number of strips(in multiple of '2') = ");
scanf("%d",&n);
h=(b-a)/(2*(n/2));
printf("\n x f(x)");
for(x=a,i=0;x<b;x=x+3*h,i=i++)
{
k+=f(x)+3*f(x+h)+3*f(x+2*h)+f(x+3*h);
printf("\n%.6f%15.6f",x,f(x));
printf("\n%.6f%15.6f",x+h,f(x+h));
printf("\n%.6f%15.6f",x+2*h,f(x+2*h));
}
printf("\n %f%15.6f",x,f(x));
printf("\n\nthe definite integral is %15.6f",(3*h/8.0)*k);
getch();
}
float f(float x)
{
return(1/(1+x*x));
}
PROGRAM 11

//Write a program to implement Booles method

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x);
void main()
{
float k=0.0,h,x,a,b;
int i,j,n;
clrscr();
printf("the function f(x):1/(1+x*x)");
printf("\nenter the lower and upper limits=");
printf("\na=");
scanf("%f",&a);
printf("b=");
scanf("%f",&b);
printf("enter the number of strips(in multiple of '2') = ");
scanf("%d",&n);
h=(b-a)/(2*(n/2));
printf("\n x f(x)");
for(x=a,i=0;x<b;x=x+4*h,i=i+4)
{
k+=7*f(x)+32*f(x+h)+12*f(x+2*h)+32*f(x+3*h)+7*f(x+4*h);
printf("\n%.6f%15.6f",x,f(x));
printf("\n%.6f%15.6f",x+h,f(x+h));
}
printf("\n %f%15.6f",x,f(x+h));
gotoxy(10,8+(++j));
printf("\n\nthe definite integral is %15.6f",(2*h/45)*k);
getch();
}
float f(float x)
{
return(x);
}
PROGRAM 12

//Write a program to implement Gauss seidal method

#include<stdio.h>
#include<conio.h>
#include<math.h>
int check_matrix(float[10][11],int);
void clear();
void main()
{
float mat[10][11],temp,out[10]={0},ratio,o,s;
int i,j,k=0,n,ck,flag;
clrscr();
gotoxy(10,3);
cprintf("Enter order of matrix : ");
cscanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n+1;j++)
{
gotoxy(10*j+10,i+5);
if(j<n)cprintf("%c*",'z'-n+j+1);
cscanf("%f",&mat[i][j]);
gotoxy(16+10*j,j+5);
if(j<n-1)cprintf("+");
else if(j==n-1)cprintf("=");
}
ck=check_matrix(mat,n);
if(ck==-1)
{
gotoxy(10,15);
cprintf("The given equation are not valid for gauss seidal
method");
}
gotoxy(10,10);
cprintf("A table for gauss seidal method:-");
for(j=0;j<n+1;j++)
{
gotoxy(5*j+3,12);
if(j<n)cprintf("%c",'z'-n+j+1);
}
do
{
flag=1;
if(k%8==0)clear();
for(i=0;i<n;i++)
{
temp=out[i];
s=0;
for(j=0;j<n;j++)
{
if(j!=1)
{
s+=out[j]*mat[i][j];
}
}
out[i]=(mat[i][n]-s)/mat[i][j];
gotoxy(10+15*i,13+k%8);
cprintf("%-f",out[i]);
if(fabs(temp-out[i])>0.000001)flag=0;
}
k++;
}
while(flag==0);
getch();
}
int check_matrix(float mat[10][11],int n)
{
int i,j;
float sum=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)sum+=abs(mat[i][j]/mat[i][j]);
}
if(sum>1)return-1;
}
return 0;
}
void clear()
{
int i,j;
gotoxy(15,22);
cprintf("Press any key to see more...");
getch();
for(i=10;i<70;i++)
for(j=13;j<21;j++)
{
gotoxy(i,j);
printf(" ");
}
}
PROGRAM 13

//Write a program to implement bessel's formula

#include<stdio.h>
#include<conio.h>
#include<math.h>
int fact(int n)
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
void main()
{
int n,i,l,g,h,g1,h1,s,z,j;
float diff[10][10],xy[10][10],x0,x[10],y[10];
float u,v,v1,t,ans=0.000000,ans1,ans2;
clrscr();
printf("enter how many values you want=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the value of x%d and y%d: ",i,i);
scanf("%f%f",&x[i],&y[i]);
xy[i][0]=x[i];xy[i][1]=y[i];
}
for(i=0;i<n;i++)
{
diff[i][0]=y[i+1]-y[i];
}
z=n;
for(j=1;j<n;j++)
{
for(i=0;i<z;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
z--;
}
for(j=2;j<n+1;j++)
for(i=0;i<n-1;i++)
xy[i][j]=xy[i+1][j-1]-xy[i][j-1];
printf("\n the difference table is=\n");
printf("\n x f(x)");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n+1-i;j++)
printf(" %0.4f",xy[i][j]);
}
printf("\n\n enter the value of x which you want the value of y:");
scanf("%f",&x0);
for(i=0;x[i]<=x0;i++);
i--;
for(z=0;x[z]<=x0;z++);
u=(float)(x0-x[i])/(x[1]-x[0]);
v=u;
v1=u-1;
t=v1;
ans=ans+((y[i]+y[i+2]/2));
z=1;
for(j=0;j<n-1;j++)
{
g=h=g1=h1=1;
if(j==0)
{
if(j%2==0)
{
u=u*(v+g);
g++;
t=t*(v1-h1);
h1++;
}
else
{
t=t*(v1+g1);
g1++;
u=u*(v-h);
h++;
}
}
ans1=(u*diff[i][j]/fact(j+1));
ans2=(t*diff[z][j]/fact(j+1));
ans=ans+(ans1+ans2)/2;
if(j%2==0)
i--;
else
z--;}
printf("\n\n the value of y when x=%f is y=%f",x0,ans);
getch();
}
PROGRAM 14

//Write a program to implement Newton Gregory forward interpolation


formula

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n;
float xy[10][11],h,p,px=1,x,y;
char str[80];
clrscr();
printf("Enter the number of data : ");
scanf("%d",&n);
printf("\nEnter the data : \n");
for(i=0;i<n;i++)
{
printf("x(%d) and y(%d) : ",i+1,i+1);
scanf("%f%f",&xy[i][0],&xy[i][1]);
}
for(j=2;j<n+1;j++)
for(i=0;j<n-1;j++)
xy[i][j]=xy[i+1][j-1]-xy[i][j-1];
printf("\nThe diference table is :- ");
printf("\nx f(x) ");
for(i=0;i<n-1;i++)
printf("?^%d ",i+1);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n+1-i;j++)
{
printf("%.4f ",xy[i][j]);
}
}
printf("\nEnter the value of 'x' : ");
scanf("%f",&x);
h=xy[i][0]-xy[0][0];
p=(x-xy[0][0])/h;
y=xy[0][1];
for(i=1;i<n;i++)
{
px*=(p-(i-1))/i;
y+=xy[0][i+1]*px;
}
printf("\n The value of function at x =%f is %f ",x,y);
getch();
}
PROGRAM 16

//Write a program to implement Newton gregory backward interpolation


formula

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n;
float xy[10][11],h,p,px=1,x,y;
char str[80];
clrscr();
printf("Enter the number of data : ");
scanf("%d",&n);
printf("\nEnter the data : \n");
for(i=0;i<n;i++)
{
printf("x(%d) and y(%d) : ",i+1,i+1);
scanf("%f%f",&xy[i][0],&xy[i][1]);
}
for(j=2;j<n+1;j++)
for(i=0;j<n-1;j++)
xy[i][j]=xy[i+1][j-1]-xy[i][j-1];
printf("\nThe diference table is :- ");
printf("\nx f(x) ");
for(i=0;i<n-1;i++)
printf("?^%d ",i+1);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n+1-i;j++)
{
printf("%.4f ",xy[i][j]);
}
}
printf("\nEnter the value of 'x' : ");
scanf("%f",&x);
h=xy[n-1][0]-xy[n-2][0];
p=(x-xy[n-1][0])/h;
y=xy[n-1][1];
for(i=1;i<n;i++)
{
px*=(p+(i-1))/i;
y+=xy[n-1-i][i+1]*px;
}
printf("\n The value of function at x =%f is %f ",x,y);
getch();
}
PROGRAM 17

//Write a program to implement gauss forward interpolation formula

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n;
float ax[10],ay[10],xy[20][20],diff[20][20];
float y1,y2,y3,y4,x,y=0,h,p,nr,dr;
clrscr();
printf("\nEnter the number of terms : ");
scanf("%d",&n);
printf("\nEnter the value in the form of x and y :- \n");
for(i=0;i<n;i++)
{
printf("Enter the value of x%d and y%d :- ",i,i);
scanf("%f %f ",&ax[i],&ay[i]);
}
printf("\nEnter the value of x for which you want value of y :- ");
scanf("%f",&x);
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
{
xy[i][0]=ax[i];
xy[i][0]=ay[i];
}
for(i=0;i<n-1;i++)
{
diff[i][1]=ay[i+1]-ay[i];
}
for(j=2;j<=4;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
for(j=2;j<=n+1;j++)
for(i=0;i<=n-1;i++)
xy[i][j]=xy[i+1][j-1]-xy[i][j-1];
printf("The difference table is :- ");
printf("\nx f(x) ");
for(i=0;i<n-1;i++)
printf("^%d :",i+1);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n+1-i;j++);
printf("%.4f",xy[i][j]);
}
i=0;
do
{
i++;}
while(ax[i]<x);
i--;
p=(x-ax[i])/h;
y1=p*diff[i][1];
y2=p*(p-1)*diff[i-1][2]/2;
y3=(p+1)*p*(p-1)*diff[i-2][3]/6;
y4=(p+1)*p*(p-1)*(p-2)*diff[i-3][4]/24;
y=ay[i]+y1+y2+y3+y4;
printf("\n When x = %6.4f,y = %6.8f",x,y);
printf("\n\n\n press enter to exit");
getch();
}
PROGRAM 18

//Write a program to implement gauss backward interpolation formula


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n;
float ax[10],ay[10],xy[20][20],diff[20][20];
float y1,y2,y3,y4,x,y=0,h,p,nr,dr;
clrscr();
printf("\nEnter the number of terms : ");
scanf("%d",&n);
printf("\nEnter the value in the form of x and y :- \n");
for(i=0;i<n;i++)
{
printf("Enter the value of x%d and y%d :- ",i,i);
scanf("%f %f ",&ax[i],&ay[i]);
}
printf("\nEnter the value of x for which you want value of y :- ");
scanf("%f",&x);
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
{
xy[i][0]=ax[i];
xy[i][0]=ay[i];
}
for(i=0;i<n-1;i++)
{
diff[i][1]=ay[i+1]-ay[i];
}
for(j=2;j<=4;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
for(j=2;j<=n+1;j++)
for(i=0;i<=n-1;i++)
xy[i][j]=xy[i+1][j-1]-xy[i][j-1];
printf("The difference table is :- ");
printf("\nx f(x) ");
for(i=0;i<n-1;i++)
printf("^%d :",i+1);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n+1-i;j++);
printf("%.4f",xy[i][j]);
}
i=0;
do
{
i++;}
while(ax[i]<x);
i--;
p=(x-ax[i])/h;
y1=p*diff[i-1][1];
y2=p*(p+1)*diff[i-1][2]/2;
y3=(p+1)*p*(p-1)*diff[i-2][3]/6;
y4=(p+2)*p*(p+1)*(p-1)*diff[i-3][4]/24;
y=ay[i]+y1+y2+y3+y4;
printf("\n When x = %6.4f,y = %6.8f",x,y);
printf("\n\n\n press enter to exit");
getch();
}
PROGRAM 19

//Write a program to implement newton divided difference formula

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a;
float xy[10][11]={0.0000},h,p,px=1,x,y;
clrscr();
printf("\nEnter the numbers of data = ");
scanf("%d",&n);
printf("\nEnter the data = \n");
for(i=0;i<n;i++)
{
printf("Enter the value of x%d and y%d = ",i+1,i+1);
scanf("%f%f",&xy[i][0],&xy[i][1]);
}
for(j=2;j<n+1;j++)
for(i=0;i<n-1;i++)
xy[i][j]=xy[i+1][j-1]-xy[i][j-1];
printf("\nThe difference table is = ");
printf("\n x f(x)");
for(i=0;i<n-1;i++)
printf("^%d",i+1);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n+1;j++)
{
printf("%.4f",xy[i][j]);
}}
printf("\nEnter the value of 'x' = ");
scanf("%f",&x);
while(xy[j][0]<x){j++;}
h=xy[1][0]-xy[0][0];
p=(x-xy[j][0]/h);
y=xy[j][1];
for(i=0;i<=n-1;i++)
{
a=((i+2)/2-1)*((i%2)?1:-1);
px*=(p-a)/i;
y+=xy[j][1];
(i%2)?j:j--;
}
printf("\nThe value of function at x=%f is %f",x,y);
getch();
}
PROGRAM 20

//Write a program to implement lagrange interpolation formula

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
float x[10],y[10],h,p,xx,yy=0,nr,dr;
clrscr();
printf("Enter the number of data = ");
scanf("%d",&n);
printf("\nEnter the data : \n");
for(i=0;i<n;i++)
{
printf("x(%d) and y(%d) : ",i+1,i+1);
scanf("%f%f",&x[i],&y[i]);
}
printf("\nEnter the value of 'x' = ");
scanf("%f",&xx);
for(i=0;i<n;i++)
{
nr=dr=1;
for(j=0;j<n;j++)
{
if(i!=j)
{
nr*=(xx-x[j]);
dr*=(x[i]-x[j]);
}
}
yy+=(nr/dr)*y[i];
}
printf("\nThe value of function at x=%f is %f",xx,yy);
getch();
}

You might also like