Sunteți pe pagina 1din 8

ACADEMY OF TECHNOLOGY

Campus address: G.T. Road, Adisaptagram, P.O.- Aedconagar, Hooghly– 712121, West Bengal
Corporate address: CA- 150, SaltLake, Sector- I, Kolkata- 700064
Website :: www.aot.edu.in Email ID :: academy@aot.edu.in

All C programs done in the lab


of the subject
Numerical Metods Laboratory [M(CS)391]

Prepared by
SL. NO. Programs on Page no.

01 Newton’s Forward Interpolation 03


02 Newton’s Backward Interpolation 03
03 Lagrange’s Interpolation 04
04 Newton’s Divided Difference Interpolation 05
05 Trapezoidal Rule 06
06 Simpson’s 1/3rd Rule 06
07 Weddle’s Rule 07
08 Bisection Method 07
09 Regula- Falsi Method 08
1. Problem: Find out the value of f(1.5) from the following data table using Newton’s forward
interpolation method.
x 0 1 2 3 4 5
y = f(x) 1 5 31 121 341 781

Coding:
#include<stdio.h>
int main() {
int i,j,n,k=1;
float a,u,x[10],y[10][10],sum,p=1.0;
printf("\n\ Enter the no. of terms: ");
scanf("%d",&n);
printf("\nThe values of x: ");
for(i=0;i<n;i++) {
scanf("%f",&x[i]);
}
printf("\nThe values of y: ");
for(i=0;i<n;i++) {
j=0;
scanf("%f",&y[i][j]);
}
for(j=0;j<n;j++) {
for(i=0;i<(n-j-1);i++) {
y[i][j+1] = y[i+1][j] - y[i][j];
}
}
for(i=0;i<n;i++) {
for(j=0;j<(n-i);j++) {
printf("%f\t",y[i][j]);
}
printf("\n");
}
printf("\nEnter the Interpolating point: ");
scanf("%f",&a);
u= (a-x[0])/(x[1]-x[0]);
sum=y[0][0];
for(i=n-2;i>=0;i--) {
p=1;
for(j=1;j<=i;j++) {
p *= (u-j+1)/j;
}
sum += (p*y[0][i]);
}
printf("f(%f)= %f",a,sum);
return 0;
}

2. Problem: Find out the value of f(7.5) from the following data table using Newton’s backward
interpolation method.
x 1 2 3 4 5 6 7 8
y = f(x) 1 8 27 64 125 216 343 512

Coding:
#include<stdio.h>
int main() {
int i,j,n,k=1;
float a,u,x[10],y[10][10],sum,p=1.0;
printf("\n Enter the no. of terms: ");
scanf("%d",&n);
printf("\nThe values of x: ");
for(i=0;i<n;i++) {
scanf("%f",&x[i]);
}
printf("\nThe values of y: ");
for(i=0;i<n;i++) {
j=0;
scanf("%f",&y[i][j]);
}
for(j=0;j<n;j++) {
for(i=0;i<(n-j-1);i++) {
y[i][j+1] = y[i+1][j] - y[i][j];
}
}
for(i=0;i<n;i++) {
for(j=0;j<(n-i);j++) {
printf("%f\t",y[i][j]);
}
printf("\n");
}
printf("\nEnter the Interpolating point: ");
scanf("%f",&a);
u= (a-x[n-1])/(x[1]-x[0]);
sum=y[n-1][0];
for(i=n-2;i>=0;i--) {
k=1;
p=1;
for(j=1;j<=n-i-1;j++) {
p *= (u-j+k)/j;
k=k+2;
}
sum += (p*y[i][n-i-1]);
}
printf("f(%f)= %f",a,sum);
return 0;
}

3. Problem: Find the value of f(6) from the following data table using Lagrange’s interpolation
method.
x 2 5 8 14
y = f(x) 94.8 87.9 81.3 68.7

Coding:
#include<stdio.h>
int main() {
float a,x[10],y[10],sum,p;
int i,j,n;
printf("\nEnter the no. of terms: ");
scanf("%d",&n);
printf("\nEnter the values of x: ");
for(i=0;i<n;i++) {
scanf("%f",&x[i]);
}
printf("\nEnter the values of y: ");
for(i=0;i<n;i++) {
scanf("%f",&y[i]);
}
printf("\nEnter the interpolating point: ");
scanf("%f",&a);
sum=0.0;
for(i=0;i<n;i++) {
p=1;
for(j=0;j<n;j++) {
if(i != j)
p *= (a-x[j])/(x[i]-x[j]);
}
sum += p*y[i];
}
printf("\nf(%f)= %f",a,sum);
return 0;
}

4. Problem: Find the value of f(6) from the following data table using Newton’s divided -
difference interpolation method.
x 2 5 8 14
y = f(x) 94.8 87.9 81.3 68.7

Coding:
include<stdio.h>
int main() {
int i,j=1,n;
float x[10],y[10],p[10],a,sum,s1=1.0,s2=0.0;
printf("\nEnter the no. of terms: ");
scanf("%d",&n);
printf("\nEnter the values of x: ");
for(i=0;i<n;i++) {
scanf("%f",&x[i]);
}
printf("\nEnter the values of y: ");
for(i=0;i<n;i++) {
scanf("%f",&y[i]);
}
printf("\nEnter the interpolating point: ");
scanf("%f",&a);
sum = y[1];
do{
for(i=1;i<n;i++) {
p[i] = ((y[i+1]-y[i])/(x[i+j]-x[i]));
y[i]=p[i];
}
s1=1;
for(i=1;i<=j;i++) {
s1 *= (a-x[i]);
}
s2 += (y[1]*s1);
n--;
j++;
}
while(n != 1);
sum += s2;
printf("\nf(%f)= %f",a,sum);
return 0;
}
5. Problem: Write a C program to compute using Trapezoidal rule of Integration.
Coding:
#include<stdio.h>
#include<math.h>
float f(float x) {
return (1/(1+x));
}
int main() {
int i,n;
float a,b,s=0,y=0,h;
printf("\nEnter the no. of intervals: ");
scanf("%d",&n);
printf("\nEnter the Lower limit: ");
scanf("%f",&a);
printf("\nEnter the Upper limit: ");
scanf("%f",&b);
h=(b-a)/n;
for(i=1;i<n;i++) {
s += f(a+(i*h));
}
y = (f(a)+f(b)+(2*s))*(h/2);
printf("\nFinal integration is %f",y);
return 0;
}

6. Problem: Write a C program to compute using Simpson’s 1/3rd rule of Integration.


Coding:
0071#include<stdio.h>
#include<math.h>
float f(float x) {
return (1/(1+x));
}
int main() {
int i,n;
float a,b,s1=0,s2=0,sum,h;
printf("\nEnter the no. of intervals: ");
scanf("%d",&n);
printf("\nEnter the Lower limit: ");
scanf("%f",&a);
printf("\nEnter the Upper limit: ");
scanf("%f",&b);
h=(b-a)/n;
if(n%2 == 0) {
for(i=1;i<n;i++) {
if(i%2 == 0) {
s1 += f(a+(i*h));
}
else {
s2 += f(a+(i*h));
}
}
sum = (h/3)*(f(a)+f(b)+(4*s2)+(2*s1));
printf("\nFinal integration: %f",sum);
}
return 0;
}
7. Problem: Write a C program to compute using Weddle’s rule of Integration.
Coding:
#include<stdio.h>
int main() {
int i,n;
float a,b,h,sum,x[100],y[100],sum1;
printf("\n Enter the Lower limit: ");
scanf("%f",&a);
printf("\nEnter the Upper limit: ");
scanf("%f",&b);
printf("\nEnter the no. of intervals: ");
scanf("%d",&n);
h=(b-a)/n;
for(i=0;i<=n;i++) {
x[i]= a+(i*h);
y[i]= (((x[i]*x[i])+2)/((x[i]*x[i])+1));
}
sum= y[0]+y[n];
for(i=1;i<n;i++) {
if(i%2 == 0 && i%3 != 0)
sum += y[i];
else if(i%3 == 0 && i%2 != 0)
sum += 6*(y[i]);
else if(i%6 == 0)
sum += 2*(y[i]);
else
sum += 5*(y[i]);
}
sum1 = sum * (3*(h/10));
printf("\nThe value is: %f",sum1);
return 0;
}

8. Problem: Find the root of the equation 3x2 + 5x – 40 = 0 using Bisection method.
Coding:
#include<stdio.h>
#define f(x) ((3*x*x)+(5*x)-40)
int main() {
float a,b,e,x;
printf("\Enter the desired accuracy: ");
scanf("%f",&e);
do {
printf("\nEnter the interval: ");
scanf("%f%f",&a,&b);
}
while(f(a)*f(b)>0);
printf("\nThe values of x:\n");
do {
x=(a+b)/2;
if(f(a)*f(x)<0) {
b=x;
}
else {
a=x;
}
printf("%f\n",x);
}
while (fabs(b-a)>e);
printf("\nThe Root is: %f",x);
return 0;
}

8. Problem: Find the root of the equation x3 – 9x + 1 = 0 using Regula- Falsi method.
Coding:
#include<stdio.h>
#include<math.h>
#define f(x) (pow(x,3)-(9*x)+1)
int main() {
float a,b,e,x;
printf("\nEnter the desired accuracy: ");
scanf("%f",&e);
do {
printf("\nEnter the interval: ");
scanf("%f%f",&a,&b);
}
while(f(a)*f(b)>0);
printf("\nThe values of x:\n");
do {
x=a-(((b-a)*(f(a)))/(f(b)-f(a)));
if(f(a)*f(x)<0) {
b=x;
}
else {
a=x;
}
printf("%f\n",x);
}
while (fabs(b-a)>e);
printf("\nThe Root is: %f",x);
return 0;
}

S-ar putea să vă placă și