Academy of Technology: Numerical Metods Laboratory
Academy of Technology: Numerical Metods Laboratory
Academy of Technology: Numerical Metods Laboratory
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
Prepared by
SL. NO. Programs on Page no.
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;
}
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;
}