0% found this document useful (0 votes)
3 views11 pages

Interpolation 1

The document contains three separate programs in C: the first program uses the Secant method to find the roots of a cubic equation, the second program implements interpolation using the Newton Gregory forward difference formula, and the third program uses the Newton Gregory backward difference formula for interpolation. Each program prompts the user for input, performs calculations, and outputs the results. The programs include necessary headers and utilize arrays for data storage and manipulation.

Uploaded by

ramneetm0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Interpolation 1

The document contains three separate programs in C: the first program uses the Secant method to find the roots of a cubic equation, the second program implements interpolation using the Newton Gregory forward difference formula, and the third program uses the Newton Gregory backward difference formula for interpolation. Each program prompts the user for input, performs calculations, and outputs the results. The programs include necessary headers and utilize arrays for data storage and manipulation.

Uploaded by

ramneetm0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Program 3: To find the roots of a given equation using Secant method.

#include<stdio.h>

#include<math.h>

float f(float x)

return x*x*x+x*x-1;

int main(){

float x0,x1,x2;

int max_iterations = 100,i;

printf("22331466,22331532 \n");

printf("Enter two initial guesses:");

scanf("%f %f", &x0,&x1);

for(i=0;i<max_iterations;i++){

x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0));

if(fabs(f(x2))<0.0001){

printf("Root found:%.4f\n",x2);

return 0;

x0=x1;

x1=x2;

printf("Solutions did not converge.\n");

return 0;

}
Program 4: Interpolation by Newton Gregory forward difference formula

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

float a[100][100],x,h,p1,p,y;

int i,j,n,L;

printf("22331466,22331532\n\n");

//Input the Given Values

printf("Enter the Number of Terms in the given table: ");

scanf("%d",&n);

printf("\nEnter the Values of X\n");

for(i=0;i<n;i++)

scanf("%f",&a[i][0]);

printf("\nEnter the Values of Y\n");

for(i=0;i<n;i++)

scanf("%f",&a[i][1]);

//Find the Difference Table

for(j=2;j<n+1;j++)

for(i=0;i<n-j+1;i++)

a[i][j] = a[i+1][j-1]-a[i][j-1];

//Print the Difference Table


printf("\nDifference Table is as follows: \n X Y");

for(i=0;i<n-1;i++)

printf("\t F%d",i+1);

printf("\n");

for(i=0;i<n;i++)

for(j=0;j<=n-i;j++)

printf("%.2f ",a[i][j]);

printf("\n");

//Find the Value of p;

printf("\nEnter the Value of x: ");

scanf("%f",&x);

h=a[1][0]-a[0][0];

i=0;

do

p=(x-a[i][0]);

i++;

}while(p>h);

i=i-1;

p=(x-a[i][0])/h;

//Calculate Output

y=a[i][1];

p1=p;

L=1;
for(j=2;j<=n;j++)

y=y+(p1*a[i][j])/L;

L=L*j;

p1=p1*(p-(j-1));

printf("\nThe Value at X = %.2f is Y = %.2f",x,y);

return 0;

}
Program 5: Interpolation by Newton Gregory backward difference formula

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

float a[100][100],x,h,p1,p,y;

int i,j,n,L;

printf("22331466, 22331532\n\n");

//Input the Given Values

printf("Enter the Number of Terms in the given table: ");

scanf("%d",&n);

printf("\nEnter the Values of X\n");

for(i=0;i<n;i++)

scanf("%f",&a[i][0]);

printf("\nEnter the Values of Y\n");

for(i=0;i<n;i++)

scanf("%f",&a[i][1]);

//Find the DiƯerence Table

for(j=2;j<n+1;j++)

for(i=j-1;i<n;i++)

a[i][j] = a[i][j-1]-a[i-1][j-1];

//Print the DiƯerence Table


printf("\nDiƯerence Table is as follows: \n X Y");

for(i=0;i<n-1;i++)

printf("\t F%d",i+1);

printf("\n");

for(i=0;i<n;i++)

for(j=0;j<i+2;j++)

printf("%.2f ",a[i][j]);

printf("\n");

//Find the Value of p;

printf("\nEnter the Value of X: ");

scanf("%f",&x);

h=a[1][0]-a[0][0];

i=0;

do

p=(x-a[i][0]);

i++;

}while(p>h);

i=i-1;

p=(x-a[i][0])/h;

//Calculate Output

y=a[i][1];

p1=p;

L=1;
for(j=2;j<=n;j++)

y=y+(p1*a[i][j])/L;

L=L*j;

p1=p1*(p+(j+1));

printf("\nThe Value at X = %.2f is Y = %.2f",x,y);

return 0;

You might also like