You are on page 1of 25

KALYANI GOVERNMENT ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Assignment on
Numerical Methods LAB (MCS 491)

Submitted By

Name:First_Name Last_Name Department: CSE


University Roll:102XXXXXXX ClassRoll: xxxxx Sem:4th
Assignment No: 1 and 2 Submission Date:

Session: 2016-2017
CONTENTS

Sl. Problem Page Instructor's Remarks


Problem Statement
No. No. No. and Signature

Write a C Program to generate Newtons Forward Interpolation


1 1 3
[difference Table] from the given distinct equally spaced data points.
Write a C Program to generate Newtons Backward Interpolation
2 2 [difference Table] from the given distinct equally spaced data points.
4

Write the C Program to find the value of y for a given value of x


3 3 5
from Newtons Forward Interpolation Formula.
Write the C Program to find the value of y for a given value of x
4 4 from Newtons Backward Interpolation Formula.
7

Write a C program to find the value of f(x) using Lagranges


5 5 9
interpolation formula.

6 6 Write C Code for Trapezoidal rule. 10

7 7 Write C code for Simpsons 1/3 rule. 11


KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Assignment On: Numerical MethodsLAB(MCS 491) Page No.:3

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:

Enter the number of elements:6


Enter the elements x and y in order:
x y
0 12
1 15
2 20
3 27
4 39
5 52
Forward Difference table:
0.0012.003.00 2.00 0.00 3.00 -10.00
1.0015.005.00 2.00 3.00 -7.00
2.0020.007.00 5.00 -4.00
3.00 27.0012.00 1.00
4.0039.0013.00
5.0052.00
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Assignment On: Numerical MethodsLAB(MCS 491) Page No.:4

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:

Enter the number of elements:6


Enter the elements x and y in order:
x y
0 12
1 15
2 20
3 27
4 39
5 52
Backward Difference table:
0.00 12.00
1.00 15.00 3.00
2.00 20.00 5.00 2.00
3.00 27.00 7.00 2.00 0.00
4.00 39.00 12.005.00 3.003.00
5.00 52.0013.00 1.00-4.00 -7.00 -10.00
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Assignment On: Numerical MethodsLAB(MCS 491) Page No.: 5

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:

Enter the number of elements:8


Enter the elements x and y in order:
x y
1 1
2 8
3 27
464
5125
6216
7 343
8 512
Enter the value of x for which you require y:1.5

Newtons Forward Difference Interpolation:


1.00 1.00 7.00 12.00 6.00 0.00 0.00 0.00 0.00
2.00 8.00 19.00 18.00 6.00 0.00 0.00 0.00
3.00 27.00 37.00 24.00 6.00 0.00 0.00
4.00 64.00 61.00 30.00 6.00 0.00
5.00 125.00 91.00 36.00 6.00
6.00 216.00 127.00 42.00
7.00343.00 169.00
8.00 512.00

Interpolated value is:3.375000


KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Assignment On: Numerical MethodsLAB(MCS 491) Page No.: 7

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:

Enter the number of elements:8


Enter the elements x and y in order:
x y
1 1
2 8
3 27
4 64
5 125
6 216
7 343
8 512
Enter the value of x for which you require y:7.5

Newtons Backward Difference Interpolation:


1.00 1.00
2.00 8.00 7.00
3.00 27.00 19.00 12.00
4.00 64.00 37.00 18.00 6.00
5.00 125.00 61.00 24.00 6.00 0.00
6.00 216.00 91.00 30.00 6.00 0.00 0.00
7.00 343.00 127.00 36.00 6.00 0.00 0.00 0.00
8.00 512.00 169.00 42.00 6.00 0.00 0.00 0.00 0.00

Interpolated value is:421.875000


KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Assignment On: Numerical MethodsLAB(MCS 491) Page No.: 9

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:

Enter the number of elements:4


Enter the elements x and y in order:
x y
0 5
1 6
3 50
4 105
Enter the value of x for finding y:2
y=19.000000
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Assignment On: Numerical MethodsLAB(MCS 491) Page No.: 10

Problem Statement 6:

Write C Code for Trapezoidal rule.


Source Code:

#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:

Enter the number of elements:6


Enter the elements x and y in order:
x y
0.0 0.0
0.20.008
0.4 0.064
0.60.216
0.8 0.512
1.0 1.0

I=0.260000
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Assignment On: Numerical MethodsLAB(MCS 491) Page No.: 11

Problem Statement7:

Write C code for Simpsons 1/3 rule.

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:

Enter the number of elements:5


Enter the elements x and y in order:
x y
1.22.0333
1.32.0692
1.4 2.1143
1.52.1667
1.62.2250

I=0.847683
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Assignment On: Numerical MethodsLAB(MCS 491) Page No.: 12

Problem Statement8:

Write a C program to solve a system


of linear equations using Gauss-
elimination
method.

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

Enter the elements of augmented matrix row-wise:

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

The solution is:

x1=-7.809086
x2=-8.690904
x3=-7.41878
KALYANI GOVERNMENT ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Assignment On: Numerical MethodsLAB(MCS 491) Page No.: 14

Problem Statement7:

Write a C program to solve a system


of linear equations using Gauss-
Seidal method.

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

Assignment On: Numerical MethodsLAB(MCS 491) Page No.: 16

Problem Statement7:

Write a C program to solve


Algebraic equation by Regula-falsi
method.

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

Assignment On: Numerical MethodsLAB(MCS 491) Page No.: 17

Problem Statement7:

Write a C program to solve


Algebraic equation by Newton
Raphson method.

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

Assignment On: Numerical MethodsLAB(MCS 491) Page No.: 18

Problem Statement7:

Write a C program to solve ordinary


differential equation by Eulars
method.

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

Assignment On: Numerical MethodsLAB(MCS 491) Page No.: 19

Problem Statement7:

Write a C program to solve ordinary


differential equation by Runga-Kutta
method.

Source Code:

#include<stdio.h>
#include<math.h>

float f(float x,float y);


int main()
{
float x0,y0,m1,m2,m3,m4,m,y,x,h,xn;
printf("Enter x0,y0,xn,h:");
scanf("%f %f %f %f",&x0,&y0,&xn,&h);
x=x0;
y=y0;
printf("\n\nX\t\tY\n");
while(x<xn)
{
m1=f(x0,y0);
m2=f((x0+h/2.0),(y0+m1*h/2.0));
m3=f((x0+h/2.0),(y0+m2*h/2.0));
m4=f((x0+h),(y0+m3*h));
m=((m1+2*m2+2*m3+m4)/6);
y=y+m*h;
x=x+h;
printf("%f\t%f\n",x,y);
}
}
float f(float x,float y)
{
float m;
m=(x-y)/(x+y);
return m;
}

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

You might also like