Professional Documents
Culture Documents
Assignment on
Numerical Methods LAB (MCS 491)
Submitted By
Session: 2016-2017
CONTENTS
Problem Statement 1:
Write a C Program to generate Newtons Forward Interpolation [difference Table] from the given distinct
equally spaced data points.
Source Code:
#include <stdio.h>
int main()
{
float a[10][10];
intn,i,j;
printf(Enter the number of elements:);
scanf(%d, &n);
printf(\nEnter the elements x and y in order:);
printf(\nx y\n);
for(i=0;i<n;i++)
scanf(%f %f,&a[i][0], &a[i][1]); /*taking x and y values*/
for(i=2;i<n+1;i++) /*calculating forward difference table*/
{
for(j=0;j<n-i+1;j++)
a[j][i]=a[j+1][i-1]-a[j][i-1];
}
printf(\nForward Difference table:);
for(i=0;i<n;i++) /*printing forward difference table*/
{
for(j=0;j<n+1-i;j++)
printf(%.2f , &a[i][j]);
printf(\n);
}
}
Output:
Problem Statement2:
Write a C Program to generate Newtons Backward Interpolation [difference Table] from the given distinct
equally spaced data points.
Source Code:
#include <stdio.h>
int main()
{
float a[10][10];
intn,i,j;
printf(Enter the number of elements:);
scanf(%d, &n);
printf(\nEnter the elements x and y in order:);
printf(\nx y\n);
for(i=0;i<n;i++)
scanf(%f %f, &a[i][0], &a[i][1]); /*taking x and y values*/
for(i=2;i<n+1;i++) /*calculating backward difference table*/
{
for(j=n-1;j>i-2;j--)
a[j][i]=a[j][i-1]-a[j-1][i-1];
}
printf(\nBackward Difference table:);
for(i=0;i<n;i++) /*printing backward difference table*/
{
for(j=0;j<=i+1;j++)
printf(%.2f , &a[i][j]);
printf(\n);
}
}
Output:
Problem Statement3:
Write the C Program to find the value of y for a given value of x from Newtons Forward Interpolation
Formula.
Source Code:
#include <stdio.h>
main()
{
float a[10][10],u,x,h,x0,sum,product;
intn,i,j;
printf(Enter the number of elements:);
scanf(%d, &n);
printf(\nEnter the elements x and y in order:);
printf(\nx y\n);
for(i=0;i<n;i++)
scanf(%f %f, &a[i][0], &a[i][1]); /*taking x and y values*/
printf(Enter the value of x for which you require y:);
scanf(%f, &x); /*taking x value for which corresponding y value is required*/
if(x<a[0][0] || x>a[n-1][0])
printf(Value lies outside the given values of x);
else
{
for(i=2;i<n+1;i++) /*calculating forward difference table*/
{
for(j=0;j<n-i+1;j++)
a[j][i]=a[j+1][i-1]-a[j][i-1];
}
x0=a[0][0];
h=a[1][0]-a[0][0];
u=(x-x0)/h;
sum=a[0][1];
for(i=2;i<=n;i++) /*calculating y using formula*/
{
product=1;
for(j=1;j<i;j++)
product=product*(u-j);
f=factorial(i-1);
sum=sum + (product*a[0][i])/f;
}
printf(\n\nNewtonsForward Difference Interpolation:);
for(i=0;i<n;i++) /*printing forward difference table*/
{
for(j=0;j<n+1-i;j++)
printf(%.2f , &a[i][j]);
printf(\n);
}
printf(\n\nInterpolated value is:%f, sum); /*printing y for given x*/
}
}
int factorial(int m)
{
int fact=1,i;
for(i=1;i<=m;i++)
fact=fact*i;
return (fact);
}
Output:
Problem Statement4:
Write the C Program to find the value of y for a given value of x from Newtons Backward Interpolation
Formula.
Source Code:
#include <stdio.h>
main()
{
float a[10][10],u,x,h,x0,sum,product;
intn,i,j;
printf(Enter the number of elements:);
scanf(%d, &n);
printf(\nEnter the elements x and y in order:);
printf(\nx y\n);
for(i=0;i<n;i++)
scanf(%f %f, &a[i][0], &a[i][1]); /*taking x and y values*/
printf(Enter the value of x for which you require y:);
scanf(%f, &x); /*taking x value for which corresponding y value is required*/
if(x<a[0][0] || x>a[n-1][0])
printf(Value lies outside the given values of x);
else
{
for(i=2;i<n+1;i++) /*calculating backward difference table*/
{
for(j=n-1;j>i-2;j--)
a[j][i]=a[j][i-1]-a[j-1][i-1];
}
xn=a[n-1][0];
h=a[1][0]-a[0][0];
u=(x-xn)/h;
sum=a[n-1][1];
for(i=2;i<=n;i++) /*calculating y using formula*/
{
product=1;
for(j=1;j<i;j++)
product=product*(u+j);
f=factorial(i-1);
sum = sum + (product*a[n-1][i])/f;
}
printf(\n\nNewtonsBackward Difference Interpolation:);
for(i=0;i<n;i++) /*printing backward difference table*/
{
for(j=0;j<=i+1;j++)
printf(%.2f , &a[i][j]);
printf(\n);
}
printf(\n\nInterpolated value is:%f, sum); /*printing y for given x*/
}
}
int factorial(int m)
{
int fact=1,i;
for(i=1;i<=m;i++)
fact=fact*i;
return (fact);
}
Output:
Problem Statement 5:
Write a C program to find the value of f(x) using Lagranges interpolation formula.
Source Code:
#include <stdio.h>
main()
{
float a[10][10],sum=0,w=1,dr=1,xr,temp,product;
intn,i,j;
printf(Enter the number of elements:);
scanf(%d, &n);
printf(\nEnter the elements x and y in order:);
printf(\nx y\n);
for(i=0;i<n;i++)
scanf(%f %f, &a[i][0], &a[i][1]); /*taking x and y values*/
printf(Enter the value of x for finding y:);
scanf(%f, &x);
for(i=0;i<n;i++) /*calculating y for given x using formula*/
{
temp=1;
xr=a[i][0];
w=w*(x-xr);
yr=a[i][1];
for(j=0;j<n;j++)
{
if(a[j][0]!=xr)
temp=(xr-a[j][0])*temp;
}
dr=(x-xr)*temp;
sum = sum + yr/dr;
}
product=w*sum;
printf(\ny=%f, product);
}
Output:
Problem Statement 6:
#include <stdio.h>
main()
{
float a[10][10],sum=0,sum1=0,h;
intn,i;
printf(Enter the number of elements:);
scanf(%d, &n);
printf(\nEnter the elements x and y in order:);
printf(\nxy\n);
for(i=0;i<n;i++) /*Calculating integration using formula*/
scanf(%f %f, &a[i][0], &a[i][1]); /*taking x and y values*/
h=a[1][0]-a[0][0];
for(i=1;i<n-1;i++) /*Calculating integration using formula*/
sum1=sum1+a[i][1];
sum = (h/2)*(a[0][1]+a[n-1][1]+2*sum1);
printf(\nI=%f,sum);
}
Output:
I=0.260000
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Problem Statement7:
Source Code:
#include <stdio.h>
main()
{
float a[10][10],sum=0,oddsum=0,evensum=0,h;
intn,i;
printf(Enter the number of elements:);
scanf(%d, &n);
printf(\nEnter the elements x and y in order:);
printf(\nx y\n);
for(i=0;i<n;i++) /*Calculating integration using formula*/
scanf(%f %f, &a[i][0], &a[i][1]); /*taking x and y values*/
h=a[1][0]-a[0][0];
for(i=1;i<n-1;i++) /*Calculating integration using formula*/
{
if(i%2==0)
evensum=evensum+a[i][1];
else
oddsum=oddsum+a[i][1];
}
sum = (h/3)*(a[0][1]+a[n-1][1]+4*oddsum+2*evensum);
printf(\nI=%f,sum);
}
Output:
I=0.847683
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Problem Statement8:
Source Code:
#include<stdio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10],sum=0.0;
printf("\nEnter the order of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix row-wise:\n\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf("A[%d][%d] : ", i,j);
scanf("%f",&A[i][j]);
}
}
for(j=1; j<=n; j++) /* loop for the generation of upper triangular matrix*/
{
for(i=1; i<=n; i++)
{
if(i>j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
x[n]=A[n][n+1]/A[n][n];
/* this loop is for backward substitution*/
for(i=n-1; i>=1; i--)
{
sum=0;
for(j=i+1; j<=n; j++)
{
sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
}
printf("\nThe solution is: \n");
for(i=1; i<=n; i++)
{
printf("\nx%d=%f\t",i,x[i]); /* x1, x2, x3 are the required solutions*/
}
return(0);
}
OUTPUT:
Enter the order of matrix: 3
A[1][1] : 10
A[1][2] : -7
A[1][3] : 3
A[1][4] : 5
A[2][1] : -6
A[2][2] : 8
A[2][3] : 4
A[2][4] : 7
A[3][1] : 2
A[3][2] : 6
A[3][3] : 9
A[3][4] : -1
x1=-7.809086
x2=-8.690904
x3=-7.41878
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Problem Statement7:
Source Code:
#include<stdio.h>
#include<math.h>
#define X 2
int main()
{
float x[X][X+1],a[X], ae, max,t,s,e;
int i,j,r,mxit;
for(i=0;i<X;i++) a[i]=0;
puts(" Eneter the elemrnts of augmented matrix rowwise\n");
for(i=0;i<X;i++)
{
for(j=0;j<X+1;j++)
{
scanf("%f",&x[i][j]);
}
}
printf(" Enter the allowed error and maximum number of iteration: ");
scanf("%f%d",&ae,&mxit);
printf("Iteration\tx[1]\tx[2]\n");
for(r=1;r<=mxit;r++)
{
max=0;
for(i=0;i<X;i++)
{
s=0;
for(j=0;j<X;j++)
if(j!=i) s+=x[i][j]*a[j];
t=(x[i][X]-s)/x[i][i];
e=fabs(a[i]-t);
a[i]=t;
}
printf(" %5d\t",r);
for(i=0;i<X;i++)
printf(" %9.4f\t",a[i]);
printf("\n");
if(max<ae)
{
printf(" Converses in %3d iteration\n", r);
for(i=0;i<X;i++)
printf("a[%3d]=%7.4f\n", i+1,a[i]);
return 0;
}
}
}
OUTPUT:
Enter the elements of augmented matrix rowwise
2
-9
8
-7
4
8
Enter the allowed error and maximum number of iteration: 0.001 10
Iteration x[1] x[2]
1 4.0000 9.0000
Converse in 1 iteration
a[ 1]= 4.0000
a[ 2]= 9.0000
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Problem Statement7:
Source Code:
#include<stdio.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);
}
void main ()
{
int itr = 0, maxmitr;
float x0,x1,x2,x3,allerr;
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);
return 0;
}
x2=x3;
}
while (itr<maxmitr);
printf("Solution does not converge or iterations not sufficient:\n");
return 1;
}
OUTPUT:
Enter the values of x0,x1,allowed error and maximum iterations:
0 1 0.0005 20
Iteration no. 1 X = 0.31467
Iteration no. 2 X = 0.44673
Iteration no. 3 X = 0.49402
Iteration no. 4 X = 0.50995
Iteration no. 5 X = 0.51520
Iteration no. 6 X = 0.51692
Iteration no. 7 X = 0.51748
Iteration no. 8 X = 0.51767
After 8 iterations, root =0.5177
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Problem Statement7:
Source Code:
#include<stdio.h>
#include<math.h>
float f(float x)
{
return x*log10(x) - 1.2;
}
float df (float x)
{
return log10(x) + 0.43429;
}
void main()
{
int itr, maxmitr;
float h, x0, x1, allerr;
printf("\nEnter x0, allowed error and maximum iterations\n");
scanf("%f %f %d", &x0, &allerr, &maxmitr);
for (itr=1; itr<=maxmitr; itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);
if (fabs(h) < allerr)
{
printf("After %3d iterations, root = %8.6f\n", itr, x1);
return 0;
}
x0=x1;
}
printf(" The required solution does not converge or iterations are insufficient\n");
return 1;
}
OUTPUT:
Enter x0, allowed error and maximum iterations
2 0.0001 10
At Iteration no. 1, x = 2.813170
At Iteration no. 2, x = 2.741109
At Iteration no. 3, x = 2.740646
At Iteration no. 4, x = 2.740646
After 4 iterations, root = 2.74046
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Problem Statement7:
Source Code:
#include<stdio.h>
float fun(float x,float y)
{
float f;
f=x+y;
return f;
}
int main()
{
float a,b,x,y,h,t,k;
printf("\nEnter x0,y0,h,xn: ");
scanf("%f%f%f%f",&a,&b,&h,&t);
x=a;
y=b;
printf("\n x\t y\n");
while(x<=t)
{
k=h*fun(x,y);
y=y+k;
x=x+h;
printf("%0.3f\t%0.3f\n",x,y);
}
}
OUTPUT:
Enter x0,y0,h,xn: 0 1 0.1 1
x y
0.100 1.100
0.200 1.220
0.300 1.362
0.400 1.528
0.500 1.721
0.600 1.943
0.700 2.197
0.800 2.487
0.900 2.816
1.000 3.187
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Problem Statement7:
Source Code:
#include<stdio.h>
#include<math.h>
OUTPUT:
Enter x0,y0,xn,h:0 2 2 0.5
X y
0.500000 1.621356
1.000000 1.242713
1.500000 0.864069
2.000000 0.485426