You are on page 1of 54

 

// GAUSS ELIMINATION METHOD


#include<stdio.h>
#include<math.h>
void matprint(float aug[10][10], int n)
{
int i,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n;j++)
{
printf("\t %.2f",aug[i][j]);
}
printf("\n");
}
}
void pivot(float aug[10][10],int n) //Partial Pivoting
{
int i,j,k;
float max,temp;
for(k=0;k<=n;k++)
{
max=fabs(aug[k][k]);
for(i=k+1;i<=n-1;i++)
{
if(fabs(aug[i][k]>max))
{
max=aug[i][k];
for(j=0;j<=n;j++)
{
temp=aug[i][j];
aug[i][j]=aug[k][j];
aug[k][j]=temp;
}
}
}
}
}
//Normalisation of Matrices
void upper(float aug[10][10], int n)
{
int i,j,k;
float max,term;
for(i=0;i<=n-1;i++)
{
max=aug[i][i];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]/max;
}
}
for(k=0;k<=n-1;k++)
{
for(i=k+1;i<=n-1;i++)

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 1 

 
 

{
term=aug[i][k]/aug[k][k];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]-term*aug[k][j];
}
}
}
}
void main()
{
int n,i,j,k;
float max,temp,term,aug[10][10],x[10],sum;
clrscr();
printf("\n***************************************************");
printf("\n************GAUSS ELIMINATION METHOD***************");
printf("\n***************************************************");
printf("\n Enter N = ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nA[%d][%d] = ",i,j);
scanf("%f",&aug[i][j]);
}
printf("\nB[%d] = ",i);
scanf("%f",&aug[i][n]);
}
printf("\n Augumented Matrix >>> \n");
matprint(aug,n);
printf("\n After Partial Pivoting Augumented Matrix >>> \n");
pivot(aug,n);
matprint(aug,n);
upper(aug,n);
printf("\n UPPER TRIANGULAR MATRIX >>> \n");
matprint(aug,n);
// Backward Substitution
for(i=n-1;i>=0;i--)
{
sum=0;
for(j=i;j<=n-1;j++)
{
if(j>i)
{
sum=sum+aug[i][j]*x[j];
}
}
x[i]=(aug[i][n]-sum)/aug[i][i];
}
printf("\n SOLUTION>>>");
for(i=0;i<=n-1;i++)
{

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 2 

 
 

printf("\t x%d = %.2f",i,x[i]);


}
getch();
}
// GAUSS JORDON METHOD
#include<stdio.h>
#include<math.h>
void matprint(float aug[10][10], int n)
{
int i,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n;j++)
{
printf("\t %.2f",aug[i][j]);
}
printf("\n");
}
}
void pivot(float aug[10][10],int n)
{
int i,j,k;
float max, temp;
for(k=0;k<=n;k++)
{
max=fabs(aug[k][k]);
for(i=k+1;i<=n-1;i++)
{
if(fabs(aug[i][k]>max))
{
max=aug[i][k];
for(j=0;j<=n;j++)
{
temp=aug[i][j];
aug[i][j]=aug[k][j];
aug[k][j]=temp;
}
}
}
}
}
void normalise(float aug[10][10],int n)
{
int i,j,k;
float max,term;
for(i=0;i<=n-1;i++)
{
max=aug[i][i];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]/max;
}
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 3 

 
 

//reduction to Upper triangular Matrix


for(k=0;k<=n-1;k++)
{
for(i=0;i<=n-1;i++)
{
if(i!=k)
{
term=aug[i][k]/aug[k][k];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]-term*aug[k][j];
}
}
}
}
}

main()
{
int n,i,j,k;
float max,temp,term,aug[10][10],x[10],sum;
clrscr();
printf("\n***************************************************");
printf("\n************GAUSS JORDON METHOD***************");
printf("\n***************************************************");
printf("\n Enter N = ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nA[%d][%d] = ",i,j);
scanf("%f",&aug[i][j]);
}
printf("\nB[%d] = ",i);
scanf("%f",&aug[i][n]);
}
printf("\n Augumented Matrix >>> \n");
matprint(aug,n);
pivot(aug,n);
printf("\n After Partial Pivoting Augumented Matrix >>> \n");
matprint(aug,n);
printf("\n Conversion to Diagonal Matrix Matrix >>> \n");
normalise(aug,n);
matprint(aug,n);

//reduction to unit matrix


printf("\n Unit Augumented Matrix >>> \n");
normalise(aug,n);
matprint(aug,n);

printf("\n SOLUTION>>>");
for(i=0;i<=n-1;i++)

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 4 

 
 

{
printf("\t x%d = %.2f",i,aug[i][n]);
}
getch();
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 5 

 
 

// GAUSS SEIDAL METHOD


#include<stdio.h>
#include<math.h>
void matprint(float aug[10][10], int n)
{
int i,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n;j++)
{
printf("\t %.2f",aug[i][j]);
}
printf("\n");
}
}
void pivot(float aug[10][10],int n)
{
int i,j,k;
float max, temp;
for(k=0;k<=n;k++)
{
max=fabs(aug[k][k]);
for(i=k+1;i<=n-1;i++)
{
if(fabs(aug[i][k]>max))
{
max=aug[i][k];
for(j=0;j<=n;j++)
{
temp=aug[i][j];
aug[i][j]=aug[k][j];
aug[k][j]=temp;
}
}
}
}
}

main()
{
int n,i,j,k,m;
float max,temp,term,aug[10][10],x[10],sum;
clrscr();
printf("\n****************************************************");
printf("\n***************GAUSS SEIDAL METHOD******************");
printf("\n****************************************************");
printf("\n Enter N = ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nA[%d][%d] = ",i,j);

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 6 

 
 

scanf("%f",&aug[i][j]);
}
printf("\nB[%d] = ",i);
scanf("%f",&aug[i][n]);
}
printf("\n Augumented Matrix >>> \n");
matprint(aug,n);
pivot(aug,n);
printf("\n After Partial Pivoting Augumented Matrix >>> \n");
matprint(aug,n);
//Gauss Seidal Calculations
printf("\n No. of Iterations [M] = ");
scanf("%d",&m);
for(i=0;i<=n-1;i++)
{
printf("\n Initial Guess Value of X[%d] = ",i);
scanf("%f",&x[i]);
}
for(k=0;k<=m-1;k++)
{
for(i=0;i<=n-1;i++)
{
sum=aug[i][n];
for(j=0;j<=n-1;j++)
{
if(i!=j)
{
sum=sum-aug[i][j]*x[j];
}
x[i]=sum/aug[i][i];
}
printf("\t x%d = %f",i,x[i]);
}
printf("\n");
}
getch();
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 7 

 
 

// LU DECOMPOSITION METHOD
#include<stdio.h>
#include<math.h>

void matprint(float a[10][10], int n)


{
int i,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\t %.2f",a[i][j]);
}
printf("\n");
}
}

void pivot(float aug[10][10],int n)


{
int i,j,k;
float max, temp;
for(k=0;k<=n;k++)
{
max=fabs(aug[k][k]);
for(i=k+1;i<=n-1;i++)
{
if(fabs(aug[i][k]>max))
{
max=aug[i][k];
for(j=0;j<=n;j++)
{
temp=aug[i][j];
aug[i][j]=aug[k][j];
aug[k][j]=temp;
}
}
}
}
}

void normalise(float aug[10][10],int n)


{
int i,j;
float max;
for(i=0;i<=n-1;i++)
{
max=aug[i][i];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]/max;
}
}
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 8 

 
 

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 9 

 
 

main()
{
int n,i,j,k;
float [10][10],u[10][10],v[10],a[10][10],x[10],b[10],aug[10][10],term,sum;
clrscr();
printf("\n******************************************************");
printf("\n******************LU DECOMPOSITION********************");
printf("\n******************************************************");
printf("\n Enter N = ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nA[%d][%d] = ",i,j);
scanf("%f",&a[i][j]);
aug[i][j]=a[i][j];
}
printf("\nB[%d] = ",i);
scanf("%f",&b[i]);
aug[i][n]=b[i];
}
printf("\n Augumented Matrix >>> \n");
matprint(aug,n);
pivot(aug,n);
printf("\n After Partial Pivoting Augumented Matrix >>> \n");
matprint(aug,n);
printf("\n UPPER TRIANGULAR MATRIX >>> \n");
//reduction to Upper triangular Matrix
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
u[i][j]=0;
l[i][j]=0;
}
}
for(k=0;k<=n-1;k++)
{
l[k][k]=1;
for(i=k+1;i<=n-1;i++)
{
term=a[i][k]/a[k][k];
l[i][k]=term; // Note this point
for(j=0;j<=n;j++)
{
a[i][j]=a[i][j]-term*a[k][j];
}
}
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 10 

 
 

// UPPER TRIANGULAR MATRIX ELEMENTS


for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
u[i][j]=a[i][j];
}
}

printf("\n\n 'U' Matrix>>> \n");


matprint(u,n);
printf("\n\n 'L' Matrix>>> \n");
matprint(l,n);
//LUX=Y => LV=Y, Find out v[]
for(j=0;j<=n-1;j++)
{
v[j]=0;
}
//Forward Substitution
for(i=0;i<=n-1;i++)
{
sum=0;
for(j=0;j<=n-1;j++)
{
if(j<=i)
{
sum=sum+l[i][j]*v[j];
}
}
v[i]=(aug[i][n]-sum)/l[i][i];
printf("\t V%d = %f",i,v[i]);
}
// Backward Substitution
for(i=n-1;i>=0;i--)
{
sum=0;
for(j=i;j<=n-1;j++)
{
if(j>i)
{
sum=sum+u[i][j]*x[j];
}
}
x[i]=(v[i]-sum)/u[i][i];
}
printf("\n SOLUTION>>>");
for(i=0;i<=n-1;i++)
{
printf("\t x%d = %.2f",i,x[i]);
}
getch();
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 11 

 
 

// CHOLESKY DECOMPOSITION METHOD


#include<stdio.h>
#include<math.h>
void matprint(float a[10][10], int n)
{
int i,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\t %.2f",a[i][j]);
}
printf("\n");
}
}
void pivot(float aug[10][10],int n)
{
int i,j,k;
float max, temp;
for(k=0;k<=n;k++)
{
max=fabs(aug[k][k]);
for(i=k+1;i<=n-1;i++)
{
if(fabs(aug[i][k]>max))
{
max=aug[i][k];
for(j=0;j<=n;j++)
{
temp=aug[i][j];
aug[i][j]=aug[k][j];
aug[k][j]=temp;
}
}
}
}
}
void normalise(float aug[10][10],int n)
{
int i,j;
float max;
for(i=0;i<=n-1;i++)
{
max=aug[i][i];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]/max;
}
}
}

main()
{

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 12 

 
 

int n,i,j,k;
float ut[10][10],u[10][10],v[10],a[10][10],x[10],b[10],aug[10][10];
float term,sum;
clrscr();
printf("\n******************************************************");
printf("\n******************CHOLSKY DECOMPOSITION********************");
printf("\n******************************************************");
printf("\n Enter N = ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nA[%d][%d] = ",i,j);
scanf("%f",&a[i][j]);
aug[i][j]=a[i][j];
}
printf("\nB[%d] = ",i);
scanf("%f",&b[i]);
aug[i][n]=b[i];
}
printf("\n Augumented Matrix >>> \n");
matprint(aug,n);
pivot(aug,n);
printf("\n After Partial Pivoting Augumented Matrix >>> \n");
matprint(aug,n);
// CHOLESKY FACTORIZATION
u[0][0]=sqrt(a[0][0]); // First Element
for(j=1;j<=n-1;j++) // First Row Calculation
{
u[0][j]=a[0][j]/u[0][0];
}
for(j=1;j<=n-1;j++)
{
for(i=1;i<=j;i++)
{
sum=a[i][j];
for(k=0;k<=i-1;k++)
{
sum=sum-u[k][i]*u[k][j];
}
if(i<j)
u[i][j]=sum/u[i][i];
if(i==j)
u[i][j]=sqrt(sum);
}
}
printf("\n U-Matrix>>> \n");
matprint(u,n);
printf("\n U'-Matrix>>> \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 13 

 
 

{
ut[i][j]=u[j][i];
printf("\t %.2f",ut[i][j]);
}
printf("\n");
}
//U'UX=Y => U'V=Y, Find out v[]
for(j=0;j<=n-1;j++)
{
v[j]=0;
}
//Forward Substitution
for(i=0;i<=n-1;i++)
{
sum=0;
for(j=0;j<=n-1;j++)
{
if(j<=i)
{
sum=sum+ut[i][j]*v[j];
}
}
v[i]=(aug[i][n]-sum)/ut[i][i];
printf("\t V%d = %f",i,v[i]);
}
// Backward Substitution
for(i=n-1;i>=0;i--)
{
sum=0;
for(j=i;j<=n-1;j++)
{
if(j>i)
{
sum=sum+u[i][j]*x[j];
}
}
x[i]=(v[i]-sum)/u[i][i];
}
printf("\n SOLUTION>>>");
for(i=0;i<=n-1;i++)
{
printf("\t x%d = %.2f",i,x[i]);
}
getch();
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 14 

 
TE(Mechanical)_Computer Oriented Numerical Methods

UNIT#1: Errors and Approximation, Linear Programming

1.1 If x = 3.26426, find Absolute, relative and percentage error,


If, a. x is truncated to 4 decimal places,
b. x is rounded off to 4 decimal places.

1.2 The Maclaurian Series expansion for  is given by,


1 ……… , where 0 < z < x.
! ! !
Find value of n, the number of terms, such that their sum yields the value of
correct to five decimal places at x = 1.

1.3 Write short note on Truncation Error, Round off Error, Absolute Error and Relative
Error.

1.4 What is Error Propagation? Explain error propagation with respect to-
i. Addition & Subtraction,
ii. Multiplication,
iii. Division.

1.5 The deflection of free end of a cantilever beam is given by ⁄3 , where y is


the deflection of free end of the beam, p is the force applied in N, l is length of span
in mm, E is modulus of elasticity in N/mm2 and I is moment of inertia in mm4.
Estimate the maximum possible error in deflection, if applied load is 1 kN with
probable error of 2%, the measured length of beam is 1 m with probable error of
1%, the dimensions of section are 10 mm wide and 60 mm height with the probable
error of 1%,. Assume 2   10 N/ .

1.6 Volume ‘V’ of a certain solid is calculated using formula  6.4 / . Where x,
y and z are three dimensions. If maximum possible error in the measurement of x, y
and z is limited to 0.001, 0.002 and 0.003 respectively, estimate the maximum
possible error in the calculation of volume, if the nominal dimensions in x, y and z
directions are unity.

1.7 Use Simplex method to solve following problem;


Maximize, 2 5
Subject to,
4 24
3 21
9
, 0

1.8 A company manufactures two types of cloth, using three different colors of wool.
One meter length of type A cloth requires 4 kg of Red wool, 5 kg of green wool and 3
kg of yellow wool. One meter length of type B cloth requires 5 kg of Red wool, 2 kg
of green wool and 8 kg of yellow wool. The wool available with manufacturer is 1000

Prof. V. N. Chougule_CONM_07.10.2009  Page 1 
TE(Mechanical)_Computer Oriented Numerical Methods

kg of red wool, 1000 kg of green wool and 1200 kg of yellow wool. The
manufacturer can make a profit of Rs. 5 on one meter length of type A cloth and Rs. 3
on one meter cloth of type B cloth. Find best combination the quantities of type A and
type B cloth which gives him maximum profit by solving the problem with Simplex
method.

1.9 An organization is manufacturing to products P1 and P2. The profit per ton of two
products is Rs. 50 and Rs 60 resp. both the products require processing in three types
of machines. The following table indicates available machine hours per week and the
time required on each machine for one ton of P1 and P2. Find by Simplex method the
product mix which will maximize the profit.

Machine Product 1 Product 2 Total available machine hours per week


1 2 1 300
2 3 4 509
3 4 7 812

1.10 A company manufactures two types of products A and B and sells them at a profit
of Rs. 5 on type A and Rs. 3 on type B. each product is processed on two machines
M1 and M2. Type A requires one minute of processing time on M1 and two minute
on M2. Type B requires one minute of processing time on M1 and one minute on M2.
M1 is available not more than 6 hours and 40 minutes, while M2 is available for 10
hours during any working day. Formulate problem as LPP and determine quantities of
product A and B for maximum profit using Simplex method.

1.11 Compare the following:


i. Round off error and truncation error,
ii. Syntax error and formulation error.

Prof. V. N. Chougule_CONM_07.10.2009  Page 2 
TE(Mechanical)_Computer Oriented Numerical Methods

UNIT#2: Curve Fitting and Numerical Interpolation

2.1 A set of values of x and f(x) are given below. Using Lagrange’s interpolation
formula, find f (9).
X 5 7 11 13 17
f(x) 150 392 1452 2366 5202

Write a computer program for this problem

2.2 Find y(1.3) using Newton’s Divided difference formula, given


x 0 2 3 4 6
y 1 13 34 73 229
Write a computer program for this problem

2.3 State the order of polynomial which might be suitable for following function.
Calculate f (3.5) using forward difference formula.

x 2 3 4 5 6 7 8 9
y 19 48 99 178 291 444 643 894

Write a computer program for this problem

2.4 For following data using backward difference polynomials. Interpolate at x=0.25.

x 0.1 0.2 0.3 0.4 0.5


f(x) 1.4 1.56 1.76 2.00 2.28

Write a computer program for this problem

2.5 Use least square regression to fit a straight line to the data given below.

x 1 2 3 4 5 6 8
y 0.5 2.5 2.0 4.0 3.5 6.0 5.5
Write a computer program for this problem

2.6 Fit a curve y=axb using the following data

x 2000 3000 4000 5000 6000


y 15 15.5 16 17 18

Write a computer program for this problem

Prof. V. N. Chougule_CONM_07.10.2009  Page 3 
TE(Mechanical)_Computer Oriented Numerical Methods

2.7 Following is the data given for values of x and y. Fit a second degree polynomial of
the type axb + bx + c where a, b, c are constants

x -3 -2 -1 0 1 2 3
y 12 4 1 2 7 15 30

Write a computer program for this problem

2.8 Given

x 1 2 3 4 5 6 7 8
F(x) 1 8 27 64 125 216 343 512

Estimate f(7.5) using Newton Backward difference interpolation Formula.

2.9 Given

x 1.0 2.0 3.0 4.0


ln(x) 0.0 0.6931 1.0986 1.3863

Estimate f(7.5) using Newton Backward difference interpolation Formula.

2.10 Find the polynomial passing through points (0, 1), (1, 1), (2, 7), (3, 25), (4, 21),
(5, 121) using Newton’s Interpolation formula and hence find y and dy/dx at x = 0.5

2.11 Given the following data estimate f(4.12) using Newton-Gregory backward
difference interpolation polynomial:

x 0 1 2 3 4 5
f(x) 1 2 4 8 16 32

2.11 The upward velocity of a rocket is given as a function of time in Table 1.


Velocity as a function of time
t (s) v (t ) (m/s)
0 0
10 227.04
15 362.78
20 517.35
22.5 602.97
30 901.67

Determine the value of the velocity at t = 16 seconds using a first order Lagrange
polynomial.

Prof. V. N. Chougule_CONM_07.10.2009  Page 4 
TE(Mechanical)_Computer Oriented Numerical Methods

UNIT#3: Linear Simultaneous Equations:

3.1 Using Gauss Seidal method, solve the following set of simultaneous equations up to
0.01 accuracy. Do partial pivoting.
3.15 1.96 3.85z   12.95
2.13 5.12 2.89z    8.61
5.92 3.05 2.15z   6.88

3.2 Solve following equations using Gauss Elimination method,


2x1- 3x2-4x3=11
9x1+2x2-8x3=1.9
15x1- 8x2 +6x3=14.7

Write a computer program for this problem

3.3 Solve following equations using Gauss Elimination method,


3x+6y+z=16, 2x+4y+3z=13, x+3y+2z=9
Write a computer program for this problem

3.4 Solve following equations using Gauss Jordon Elimination method,


2x1+ 3x2-4x3=1, 5x1+9x2+3x3=17, -8x1- 2x2 +x3=-9
Write a computer program for this problem

3.5 Solve following linear equations using Gauss Seidal method,


4x+y+z=5, x+6y+2z=19, -x-2y-5z=10
Write a computer program for this problem

3.6 Using LU Decomposition Method solve the following set of equations. Do partial
pivoting.
2  2 10
4 2 8
3 2 2 7
3 2   5

3.7 Using Gauss Seidal method, solve using Gauss Seidal Method up to 4th iteration only.
4 2 z  40
6 2z  28
2 12z  86
3.8 A boat takes 10 hours to travel 30 km upstream and 44 km downstream, but takes
13 hours to travel 40 km upstream and 55 km downstream. Formulate the problem and
find out speed of the boat upstream and downstream.
Using the same find out speed of the boat in still water and speed of stream. Use
Gauss Elimination method only to solve the simultaneous equations.

Prof. V. N. Chougule_CONM_07.10.2009  Page 5 
TE(Mechanical)_Computer Oriented Numerical Methods

UNIT#4: Roots of Algebraic & Transcendental Equations and Numerical


Integration:

4.1 Determine using bi section method, a root of the equation cos x-1.3x = 0 with
accuracy of 0.01 .Write a computer program in C language for this problem.

4.3 4.2 Using three iterations of bi section method determine root of the equation .Initial
guesses are x1=2.8 and x2=3, f(x) =-0.9x2 +1.7 x+2.5 .Write a computer program in
C language for this problem.

4.4 Determine using false-position method, a root of the equation.xex cos3x-0.5between


limits of x1=0 and x2=1 with accuracy of 0.01. Write a computer program in C
language for this problem.

4.5 Find the root of sin x = x-2 near x =2.5 by Regula falsi method x is in radians. Not
more than 5 iterations are expected. Write a computer program in C language for this
problem.

4.6 Solve using Newton-Raphson method, ex cos x-1.4 =0 .Find the value of the root up
to accuracy of 0.001. Write a computer program in C language for this problem.

4.7 Solve the equation ex cos x-1.4 sin x = 0.8 using Newton –Raphson method taking x
=1 and doing 3 iterations. Write a computer program in C language for this problem.

4.8 Use Secant method to find the root of the equation cos x - x ex = 0. Use initial
guesses x1=0 and x2=1 to obtain accuracy of 0.01. Write a computer program in C
language for this problem.

4.9 Use secant method to obtain root of equation x ex –cos 3x -0.51 =0.Use initial guess
x1=0 and x2=1.0, Do 4 iterations. Write a computer program in C language for this
problem.

4.10 Find the integration of (4x+2) in limits 1 to 4 by using six strips. Write a
computer program in C language for this problem.

4.11 Evaluate using Simpson’s 1/3 Rule using four strips. Write a computer
program in C language for this problem.

4.12 Find the integration 1∫4 (4x-1).dx using Simpson’s 3/8 Rule using 6 strips. Write
a computer program in C language for this problem.

4.13 Find 0∫12 x2.dx by Weddle’s rule using 12 strips. Write a computer program in C
language for this problem.

Prof. V. N. Chougule_CONM_07.10.2009  Page 6 
TE(Mechanical)_Computer Oriented Numerical Methods

4.14 Find double integral of f(x,y) =x+y for x=1 to 3 and y =0 to 2 with step size for
both x and y is 2 using Trapezoidal rule Write a computer program in C language for
this problem.

4.15 Evaluate 6∫14 1∫5 (x-y+1).dx by using Simpson’s 1/3 Rule with number of strips
for x and y equal to 4. Write a computer program in C language for this problem.

4.16 The distance covered by a rocket in meters from t = 8 s to t = 30 s is given by


⎛ ⎡ ⎤ ⎞
30
140000
x = ∫ ⎜⎜ 2000 ln ⎢ ⎥ − 9.8t ⎟⎟dt
8⎝ ⎣140000 − 2100t ⎦ ⎠
Use a. Simpson’s 1/3 rule to find the approximate value of x
b. Gauss Quadrature 2 point formula
.
4.16 Evaluate 1 sin  2 , where x is in radians, using Simpson’s
rd
1/3 rule. Divide the entire interval in 8 strips.

4.17 Derive Gauss 2 point formula and use to evaluate .

4.18 Evaluate by using trapezoidal and Simpson’s 1/3rd rule.

4.19 A solid of revolution is formed by rotating about X axis, the area between X axis,
lines X=0 and X=1 and curve through the points. Estimate the volume of solid
formed.

x 0.00 0.25 0.50 0.75 1.00


f(x) 1.000 0.9886 0.9589 0.8489 0.9415

4.20 Evaluate 2.1 1.7 2 taking h=k=0.5.

4.21 A cylinder of radius 150 mm and length 900 mm with a movable piston is filled
with gas at a pressure of 10 bar. Find out work required to compress the gas to 1/3rd
of its volume by Simpson’s 3/8th rule. (hint: work done = Jpdv). Assume no. of
strips=3

Prof. V. N. Chougule_CONM_07.10.2009  Page 7 
TE(Mechanical)_Computer Oriented Numerical Methods

UNIT#5: Ordinary Differential Equations:

5.1 Using Runge Kutta 4th order method solve dy/dx -y=0 given y (0) =2, h=0.1 find y at
x=0.2. Write a computer program for this problem

5.2 Use Taylor’s series method, for the equation dy/dx = x2y and y(1)=1,to find the value
of y at x=1.1.Take step size=0.1 Write a computer program in C language for this
problem.

5.3 dy/dx =x-y2, for the given boundary condition that at x=0,y=1 find y at x=4 Take step
size h=1 using Euler method. Write a computer program in C language for this
problem.

5.4 Solve the following differential equation using Modified Euler’s method for the given
boundary condition dy/dx =log (x+y),y(1)=2,find value of y at x=1.4,upto
accuracy=0.001. Write a computer program in C language for this problem.

5.5 dy/dx = x+y given y(0)=1,h=0.1 find y(0.2) using Runge-kutta 2nd order method.
Write a computer program in C language for this problem.

5.6 A ball at 1200 K is allowed to cool down in air at an ambient temperature of 300 K.
Assuming heat is lost only due to radiation, the differential equation for the
temperature of the ball is given by

= −2.2067 × 10 −12 (θ 4 − 81× 108 ), θ (0 ) = 1200 K
dt
where θ is in K and t in seconds. Find the temperature at t = 480 seconds using
Runge-Kutta 4th order method. Assume a step size of h = 240 seconds.

5.7 A body of mass 2 kg is attached to a spring with a spring constant of 10. The
differential equation governing the displacement of the body y and time t is given by,
2 5 0. Find the displacement y at time t = 1.5 given that y(0)=2 and
y’= -4.

5.8 Solve the equation  , subjected to initial conditions y(0)=1,y’(0)=0.


th
Using Runge Kutta’s 4 order method and compare value of y at x=0.2.

5.9 The rate of disintegration of radioactive substance is proportional to the amount of


substance (m) remaining at time (t) is governed by the relation , where K
is constant of disintegration and is equal to 0.01. If mass of the substance is 100 gm,
evaluate the amount of substance remaining at the end of 10 seconds.(Increment in
time as 0.25 sec). use Euler’s method.

5.10 Solve using Runge-Kutta 4th Order method to find y at x = 0.4


given y(0.0) = 1.0. take h =0.2.

Prof. V. N. Chougule_CONM_07.10.2009  Page 8 
TE(Mechanical)_Computer Oriented Numerical Methods

5.11 Solve the differential equation 1.5 4.5 4.5. Using Euler’s method.
Assume at x = 0, dy/dx = -2 and y = 1. Tabulate the results for x = 0.1, 0.2 and 0.3.

5.12 Solve the equation . Estimate the values of y at x = 0.25, 0.50 and 0.75.
The boundary conditions are at x = 0 and y = 0 and at x = 1, y = 0.

5.13 Solve the following differential equation to get y(0.1).


,      0 1
Use i. Modified Euler’s method with h = 0.05
ii. Runge-Kutta 4th Order with h = 0.1.

5.14 Using Runge-Kutta method for 4th order, find y for x = 0.1, 0.2 and 0.3. Given
that dy/dx = xy + y2, y(0) = 1. Continue the solution at 0.4 using Milne’s method.

5.15 An object having surface area of 0.1 m2 is initially at 00C is dipped in the hot
water bath. Water is initially at 950C. Find the temperature of object after 10 sec.,
taking dt=2 sec.

Prof. V. N. Chougule_CONM_07.10.2009  Page 9 
TE(Mechanical)_Computer Oriented Numerical Methods

UNIT#6: Partial Differential Equations:

6.1 A Steel plate of 750 x 750 mm has two adjacent sides maintained at 1000C. While the
two other sides are maintained at 00C. What will be steady state temperature at
interior points assuming a grid size of 250 mm. Write a computer program for this
problem?

6.2 The transverse displacement ‘u’ of a point at a distance ‘x’ from one end and at any
time ‘t’ of the vibrating string satisfies the equation, 4  with

boundary conditions u = 0 at x = 4, t>0 and initial conditions u = x(4 - x) and


0, 0 4. Solve this equation numerically for one half period of
vibration, taking h = 1 and k = 0.5.

6.3 Solve the equation   10 10 over the square with sides 0


, 3 with u = 0 on the boundary and mesh length = 1.

6.4 Initial temperatures within an insulated cylindrical metal rod of 4 cm long are given
by, 50 4 ,0 4, where x is distance from one face. Both the ends are
maintained at 00C. Find the temperatures as a function of x and t (0 1.5) if the
heat flow is governed by, 2 . Use explicit method.
Assume ∆ 1 & ∆ 0.25

6.5 A steel plate of 150 mm X 150 mm has its four sides held at 100, 80, 50 and 00C as
shown in figure. Estimate the steady state temperature distribution at interior points
assuming grid size of 50 mm.
T = 1000C

1 2

T = 800C T = 00C
3 4

T = 500C

Prof. V. N. Chougule_CONM_07.10.2009  Page 10 
TE(Mechanical)_Computer Oriented Numerical Methods

6.6 Calculate a finite difference solution of the equation   0 1 subjected


to conditions sin   0   0 1 and 0 at x
0    0
Take 0.1, 0.001. find u at all locations at t = 0.002 by implicit method.
6.7 The steady state two dimensional heat flow in the metal plate is given by,
0

Given the boundary conditions as shown in the figure below. Find the temperatures
T1, T2, T3 and T4.
y 100 200
00 300

20 400
0

40 500
0

x
600 600 600 600

6.8 Solve Laplace equation for given grid and boundary conditions and find temperatures
T1, T2, T3 and T4.

900 900
80 100

80 800

80 600

x
400 400
400 400

Prof. V. N. Chougule_CONM_07.10.2009  Page 11 
 

// Program for Bisection Method


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

// Function Definition
float f(float x)
{
return exp(x)-4*x;
}

void main()
{
float x0,x1,x2,acc;

clrscr();
printf("\nEnter Initial Guess value X0=");
scanf("%f",&x0);
printf("\nEnter Initial Guess value X1=");
scanf("%f",&x1);
printf("\nEnter Accuracy = ");
scanf("%f",&acc);

if(f(x0)*f(x1)>=0)
{
printf("\Choose other guess values");
delay(5000);
exit();
}
do
{
x2=(x0+x1)/2; //for False position-Change Formula
if(f(x0)*f(x2)<=0)
x1=x2;
else
x0=x2;
printf("\n Root>>> %f",x2);
}while(fabs(x1-x0)>=acc);

getch();
}

NOTE: for Bisection and False Position Method program is same, only difference is in formula.

Prof. V. N. Chougule_CONM_Roots_Eqns_01.10.2009  Page 1 

 
 

// Program for Secant Method


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

// Function Definition
float f(float x)
{
return exp(x)-4*x;
}

void main()
{
float x0,x1,x2,acc;
clrscr();
printf("\nEnter Initial Guess value X0=");
scanf("%f",&x0);
printf("\nEnter Initial Guess value X1=");
scanf("%f",&x1);
printf("\nEnter Accuracy = ");
scanf("%f",&acc);

if(f(x0)*f(x1)>=0)
{
printf("\Choose other guess values");
delay(5000);
exit();
}
do{
x2=x0-((x1-x0)/(f(x1)-f(x0)))*f(x0);
x0=x1;
x1=x2;
printf("\n Root>>> %f",x2);
}while(fabs(x1-x0)>=acc);
getch();
}

Prof. V. N. Chougule_CONM_Roots_Eqns_01.10.2009  Page 2 

 
 

// Program for Newton Rhaphson Method

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

// Function Definition
float f(float x)
{
return pow(x,3)+2*x-5;
}

float f1(float x)
{
return 3*pow(x,2)+2;
}

void main()
{
float x0,x1,acc;
clrscr();
printf("\n \t ***** NEWTON RAPHSON METHOD ******");
printf("\n\n Enter Initial Guess values for roots: ");
scanf("%f", &x0);
printf("\n Enter Accuracy = ");
scanf("%f", &acc);

x1=x0-(f(x0)/f1(x0));

while(fabs(f(x1))>=acc)
{
x1=x0-(f(x0)/f1(x0));
x0=x1;
printf("\n x1>>> %f",x1);
}
getch();
}

Prof. V. N. Chougule_CONM_Roots_Eqns_01.10.2009  Page 3 

 
 

// GAUSS ELIMINATION METHOD


#include<stdio.h>
#include<math.h>
void matprint(float aug[10][10], int n)
{
int i,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n;j++)
{
printf("\t %.2f",aug[i][j]);
}
printf("\n");
}
}
void pivot(float aug[10][10],int n) //Partial Pivoting
{
int i,j,k;
float max,temp;
for(k=0;k<=n;k++)
{
max=fabs(aug[k][k]);
for(i=k+1;i<=n-1;i++)
{
if(fabs(aug[i][k]>max))
{
max=aug[i][k];
for(j=0;j<=n;j++)
{
temp=aug[i][j];
aug[i][j]=aug[k][j];
aug[k][j]=temp;
}
}
}
}
}
//Normalisation of Matrices
void upper(float aug[10][10], int n)
{
int i,j,k;
float max,term;
for(i=0;i<=n-1;i++)
{
max=aug[i][i];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]/max;
}
}
for(k=0;k<=n-1;k++)
{
for(i=k+1;i<=n-1;i++)

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 1 

 
 

{
term=aug[i][k]/aug[k][k];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]-term*aug[k][j];
}
}
}
}
void main()
{
int n,i,j,k;
float max,temp,term,aug[10][10],x[10],sum;
clrscr();
printf("\n***************************************************");
printf("\n************GAUSS ELIMINATION METHOD***************");
printf("\n***************************************************");
printf("\n Enter N = ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nA[%d][%d] = ",i,j);
scanf("%f",&aug[i][j]);
}
printf("\nB[%d] = ",i);
scanf("%f",&aug[i][n]);
}
printf("\n Augumented Matrix >>> \n");
matprint(aug,n);
printf("\n After Partial Pivoting Augumented Matrix >>> \n");
pivot(aug,n);
matprint(aug,n);
upper(aug,n);
printf("\n UPPER TRIANGULAR MATRIX >>> \n");
matprint(aug,n);
// Backward Substitution
for(i=n-1;i>=0;i--)
{
sum=0;
for(j=i;j<=n-1;j++)
{
if(j>i)
{
sum=sum+aug[i][j]*x[j];
}
}
x[i]=(aug[i][n]-sum)/aug[i][i];
}
printf("\n SOLUTION>>>");
for(i=0;i<=n-1;i++)
{

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 2 

 
 

printf("\t x%d = %.2f",i,x[i]);


}
getch();
}
// GAUSS JORDON METHOD
#include<stdio.h>
#include<math.h>
void matprint(float aug[10][10], int n)
{
int i,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n;j++)
{
printf("\t %.2f",aug[i][j]);
}
printf("\n");
}
}
void pivot(float aug[10][10],int n)
{
int i,j,k;
float max, temp;
for(k=0;k<=n;k++)
{
max=fabs(aug[k][k]);
for(i=k+1;i<=n-1;i++)
{
if(fabs(aug[i][k]>max))
{
max=aug[i][k];
for(j=0;j<=n;j++)
{
temp=aug[i][j];
aug[i][j]=aug[k][j];
aug[k][j]=temp;
}
}
}
}
}
void normalise(float aug[10][10],int n)
{
int i,j,k;
float max,term;
for(i=0;i<=n-1;i++)
{
max=aug[i][i];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]/max;
}
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 3 

 
 

//reduction to Upper triangular Matrix


for(k=0;k<=n-1;k++)
{
for(i=0;i<=n-1;i++)
{
if(i!=k)
{
term=aug[i][k]/aug[k][k];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]-term*aug[k][j];
}
}
}
}
}

main()
{
int n,i,j,k;
float max,temp,term,aug[10][10],x[10],sum;
clrscr();
printf("\n***************************************************");
printf("\n************GAUSS JORDON METHOD***************");
printf("\n***************************************************");
printf("\n Enter N = ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nA[%d][%d] = ",i,j);
scanf("%f",&aug[i][j]);
}
printf("\nB[%d] = ",i);
scanf("%f",&aug[i][n]);
}
printf("\n Augumented Matrix >>> \n");
matprint(aug,n);
pivot(aug,n);
printf("\n After Partial Pivoting Augumented Matrix >>> \n");
matprint(aug,n);
printf("\n Conversion to Diagonal Matrix Matrix >>> \n");
normalise(aug,n);
matprint(aug,n);

//reduction to unit matrix


printf("\n Unit Augumented Matrix >>> \n");
normalise(aug,n);
matprint(aug,n);

printf("\n SOLUTION>>>");
for(i=0;i<=n-1;i++)

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 4 

 
 

{
printf("\t x%d = %.2f",i,aug[i][n]);
}
getch();
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 5 

 
 

// GAUSS SEIDAL METHOD


#include<stdio.h>
#include<math.h>
void matprint(float aug[10][10], int n)
{
int i,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n;j++)
{
printf("\t %.2f",aug[i][j]);
}
printf("\n");
}
}
void pivot(float aug[10][10],int n)
{
int i,j,k;
float max, temp;
for(k=0;k<=n;k++)
{
max=fabs(aug[k][k]);
for(i=k+1;i<=n-1;i++)
{
if(fabs(aug[i][k]>max))
{
max=aug[i][k];
for(j=0;j<=n;j++)
{
temp=aug[i][j];
aug[i][j]=aug[k][j];
aug[k][j]=temp;
}
}
}
}
}

main()
{
int n,i,j,k,m;
float max,temp,term,aug[10][10],x[10],sum;
clrscr();
printf("\n****************************************************");
printf("\n***************GAUSS SEIDAL METHOD******************");
printf("\n****************************************************");
printf("\n Enter N = ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nA[%d][%d] = ",i,j);

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 6 

 
 

scanf("%f",&aug[i][j]);
}
printf("\nB[%d] = ",i);
scanf("%f",&aug[i][n]);
}
printf("\n Augumented Matrix >>> \n");
matprint(aug,n);
pivot(aug,n);
printf("\n After Partial Pivoting Augumented Matrix >>> \n");
matprint(aug,n);
//Gauss Seidal Calculations
printf("\n No. of Iterations [M] = ");
scanf("%d",&m);
for(i=0;i<=n-1;i++)
{
printf("\n Initial Guess Value of X[%d] = ",i);
scanf("%f",&x[i]);
}
for(k=0;k<=m-1;k++)
{
for(i=0;i<=n-1;i++)
{
sum=aug[i][n];
for(j=0;j<=n-1;j++)
{
if(i!=j)
{
sum=sum-aug[i][j]*x[j];
}
x[i]=sum/aug[i][i];
}
printf("\t x%d = %f",i,x[i]);
}
printf("\n");
}
getch();
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 7 

 
 

// LU DECOMPOSITION METHOD
#include<stdio.h>
#include<math.h>

void matprint(float a[10][10], int n)


{
int i,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\t %.2f",a[i][j]);
}
printf("\n");
}
}

void pivot(float aug[10][10],int n)


{
int i,j,k;
float max, temp;
for(k=0;k<=n;k++)
{
max=fabs(aug[k][k]);
for(i=k+1;i<=n-1;i++)
{
if(fabs(aug[i][k]>max))
{
max=aug[i][k];
for(j=0;j<=n;j++)
{
temp=aug[i][j];
aug[i][j]=aug[k][j];
aug[k][j]=temp;
}
}
}
}
}

void normalise(float aug[10][10],int n)


{
int i,j;
float max;
for(i=0;i<=n-1;i++)
{
max=aug[i][i];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]/max;
}
}
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 8 

 
 

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 9 

 
 

main()
{
int n,i,j,k;
float [10][10],u[10][10],v[10],a[10][10],x[10],b[10],aug[10][10],term,sum;
clrscr();
printf("\n******************************************************");
printf("\n******************LU DECOMPOSITION********************");
printf("\n******************************************************");
printf("\n Enter N = ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nA[%d][%d] = ",i,j);
scanf("%f",&a[i][j]);
aug[i][j]=a[i][j];
}
printf("\nB[%d] = ",i);
scanf("%f",&b[i]);
aug[i][n]=b[i];
}
printf("\n Augumented Matrix >>> \n");
matprint(aug,n);
pivot(aug,n);
printf("\n After Partial Pivoting Augumented Matrix >>> \n");
matprint(aug,n);
printf("\n UPPER TRIANGULAR MATRIX >>> \n");
//reduction to Upper triangular Matrix
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
u[i][j]=0;
l[i][j]=0;
}
}
for(k=0;k<=n-1;k++)
{
l[k][k]=1;
for(i=k+1;i<=n-1;i++)
{
term=a[i][k]/a[k][k];
l[i][k]=term; // Note this point
for(j=0;j<=n;j++)
{
a[i][j]=a[i][j]-term*a[k][j];
}
}
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 10 

 
 

// UPPER TRIANGULAR MATRIX ELEMENTS


for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
u[i][j]=a[i][j];
}
}

printf("\n\n 'U' Matrix>>> \n");


for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\t %.2f",u[i][j]);
}
printf("\n");
}
printf("\n\n 'L' Matrix>>> \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\t %.2f",l[i][j]);
}
printf("\n");
}
//LUX=Y => LV=Y, Find out v[]
for(j=0;j<=n-1;j++)
{
v[j]=0;
}
//Forward Substitution
for(i=0;i<=n-1;i++)
{
sum=0;
for(j=0;j<=n-1;j++)
{
if(j<=i)
{
sum=sum+l[i][j]*v[j];
}
}
v[i]=(aug[i][n]-sum)/l[i][i];
printf("\t V%d = %f",i,v[i]);
}
// Backward Substitution
for(i=n-1;i>=0;i--)
{
sum=0;
for(j=i;j<=n-1;j++)
{
if(j>i)

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 11 

 
 

{
sum=sum+u[i][j]*x[j];
}
}
x[i]=(v[i]-sum)/u[i][i];
}
printf("\n SOLUTION>>>");
for(i=0;i<=n-1;i++)
{
printf("\t x%d = %.2f",i,x[i]);
}
getch();
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 12 

 
 

// CHOLESKY DECOMPOSITION METHOD


#include<stdio.h>
#include<math.h>
void matprint(float a[10][10], int n)
{
int i,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\t %.2f",a[i][j]);
}
printf("\n");
}
}
void pivot(float aug[10][10],int n)
{
int i,j,k;
float max, temp;
for(k=0;k<=n;k++)
{
max=fabs(aug[k][k]);
for(i=k+1;i<=n-1;i++)
{
if(fabs(aug[i][k]>max))
{
max=aug[i][k];
for(j=0;j<=n;j++)
{
temp=aug[i][j];
aug[i][j]=aug[k][j];
aug[k][j]=temp;
}
}
}
}
}
void normalise(float aug[10][10],int n)
{
int i,j;
float max;
for(i=0;i<=n-1;i++)
{
max=aug[i][i];
for(j=0;j<=n;j++)
{
aug[i][j]=aug[i][j]/max;
}
}
}

main()
{

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 13 

 
 

int n,i,j,k;
float ut[10][10],u[10][10],v[10],a[10][10],x[10],b[10],aug[10][10];
float term,sum;
clrscr();
printf("\n******************************************************");
printf("\n******************CHOLSKY DECOMPOSITION********************");
printf("\n******************************************************");
printf("\n Enter N = ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nA[%d][%d] = ",i,j);
scanf("%f",&a[i][j]);
aug[i][j]=a[i][j];
}
printf("\nB[%d] = ",i);
scanf("%f",&b[i]);
aug[i][n]=b[i];
}
printf("\n Augumented Matrix >>> \n");
matprint(aug,n);
pivot(aug,n);
printf("\n After Partial Pivoting Augumented Matrix >>> \n");
matprint(aug,n);
// CHOLESKY FACTORIZATION
u[0][0]=sqrt(a[0][0]); // First Element
for(j=1;j<=n-1;j++) // First Row Calculation
{
u[0][j]=a[0][j]/u[0][0];
}
for(j=1;j<=n-1;j++)
{
for(i=1;i<=j;i++)
{
sum=a[i][j];
for(k=0;k<=i-1;k++)
{
sum=sum-u[k][i]*u[k][j];
}
if(i<j)
u[i][j]=sum/u[i][i];
if(i==j)
u[i][j]=sqrt(sum);
}
}
printf("\n U-Matrix>>> \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\t %.2f",u[i][j]);

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 14 

 
 

}
printf("\n");
}
printf("\n U'-Matrix>>> \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
ut[i][j]=u[j][i];
printf("\t %.2f",ut[i][j]);
}
printf("\n");
}
//U'UX=Y => U'V=Y, Find out v[]
for(j=0;j<=n-1;j++)
{
v[j]=0;
}
//Forward Substitution
for(i=0;i<=n-1;i++)
{
sum=0;
for(j=0;j<=n-1;j++)
{
if(j<=i)
{
sum=sum+ut[i][j]*v[j];
}
}
v[i]=(aug[i][n]-sum)/ut[i][i];
printf("\t V%d = %f",i,v[i]);
}
// Backward Substitution
for(i=n-1;i>=0;i--)
{
sum=0;
for(j=i;j<=n-1;j++)
{
if(j>i)
{
sum=sum+u[i][j]*x[j];
}
}
x[i]=(v[i]-sum)/u[i][i];
}
printf("\n SOLUTION>>>");
for(i=0;i<=n-1;i++)
{
printf("\t x%d = %.2f",i,x[i]);
}
getch();
}

Prof. V. N. Chougule_CONM_Linear Simultaneous Eqn_30.09.2009  Page 15 

 
 

// Double Integration by Trapezoidal Method


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

float f(float x, float y)


{
return exp(x+y);
}
main()
{
float x0,xn,y0,yn,h,k,a[10][10],area[10],int2;
int i,j,m,n;
clrscr();
printf("\n Enter \n x0 = ");
scanf("%f",&x0);
printf("\n xn = ");
scanf("%f",&xn);
printf("\n y0 = ");
scanf("%f",&y0);
printf("\n yn = ");
scanf("%f",&yn);
printf("\n m = ");
scanf("%d",&m);
printf("\n n = ");
scanf("%d",&n);
h=(xn-x0)/m;
k=(yn-y0)/n;
printf("\n H = %.2f & K = %.2f \n\n",h,k);
printf("\n TABULATED VALUES OF FUNCTION: \n\n");

// Calculation and Printing f(x,y) Table


for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
{
a[i][j]=f(x0+i*h,y0+j*k);
printf("\t %f",a[i][j]);
}
printf("\n");
}

// Calculation and Printing Double Integral Value


for(i=0;i<=m-1;i++)
{
int2=int2+(area[i]+area[i+1]);
}
int2=(h*k/4)*int2;
printf("\n\nDouble Integral is %f",int2);
getch();
Prof. V. N. Chougule_CONM_Integration_01.10.2009  Page 1 

 
 

// Double Integration by Simpson’s 1/3rd Rule


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

float f(float x, float y)


{
return exp(x+2*y);
}
main()
{
int i, j, m, n;
float x0, xn, y0, yn, h, k,x[10][10],di[10],sol=0;
clrscr();
printf("\n **************************************************");
printf("\n ****Double Integration - Simpson's 1/3 Method****");
printf("\n **************************************************\n");
printf("\n Enter X0 = ");
scanf("%f",&x0);
printf("\n Enter Xn = ");
scanf("%f",&xn);
printf("\n Enter Y0 = ");
scanf("%f",&y0);
printf("\n Enter Yn = ");
scanf("%f",&yn);
printf("\n No. of Steps along X: M = ");
scanf("%d", &m);
if(m%2!=0) //CONDITION CHECK
{
exit();
}
printf("\n No. of Steps along Y: N = ");
scanf("%d", &n);
if(n%2!=0) //CONDITION CHECK
{
exit();
}
h=(xn-x0)/m;
k=(yn-y0)/n;
printf("\n\t H = %f \n\t K = %f \n\n",h,k);
// Calculation and Printing f(x,y) Table
printf("\nFUNCTION VALUE TABLE => f(x,y) \n");
for(i=0;i<=m-1;i++)
{
di[i]=0;
printf("\n\n");
for(j=0;j<=n-1;j++)
{
Prof. V. N. Chougule_CONM_Integration_01.10.2009  Page 2 

 
 

x[i][j]=f(x0+i*h,y0+j*k);
printf("%f\t",x[i][j]);
}
}
// Calculation and Printing Double Integral Value
//Row wise calculations
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-2;j=j+2)
{
di[i]=di[i]+(x[i][j]+4*x[i][j+1]+x[i][j+2]);
}
printf("\n di[%d] = %f",i, di[i]);
}

// Column wise Calculations


for(i=0;i<=m-2;i=i+2)
{
sol=sol+(di[i]+4*di[i+1]+di[i+2]);
}
sol=((h*k)/9)*sol;

printf("\n\n Solution = %f",sol);


getch();
}

Prof. V. N. Chougule_CONM_Integration_01.10.2009  Page 3 

 
 

// Langrange's Interpolation Method


#include<stdio.h>
#include<conio.h>

main()
{
int i,j,n;
float xg,yg=0,x[10],y[10],li[10];
clrscr();
printf("\n****************************************************");
printf("\n*********Langrange's Interpolation Method***********");
printf("\n****************************************************");
printf("\nEnter N ="); // No. of Data Points
scanf("%d",&n);
printf("Xg =");
scanf("%f",&xg);
for(i=0;i<=n-1;i++) // Data Entry
{
printf("X[%d] =",i);
scanf("%f",&x[i]);
printf("Y[%d] =",i);
scanf("%f",&y[i]);
}

// Calculation of Li terms
for(i=0;i<=n-1;i++)
{
li[i]=1;
for(j=0;j<=n-1;j++)
{
if(i!=j)
{
li[i]=li[i]*(xg-x[j])/(x[i]-x[j]);
}
}
}

// Calculation of Yg
for(i=0;i<=n-1;i++)
{
yg=yg+li[i]*y[i];
}
printf("\nSolution is %f",yg);
getch();
}

Prof. V. N. Chougule_CONM_Interpolation_01.10.2009  Page 1 

 
 

// Inverse Interpolation by Langrange's Method


#include<stdio.h>
#include<conio.h>

main()
{
int i,j,n;
float xg,yg=0,x[10],y[10],li[10];
clrscr();
printf("\n****************************************************");
printf("\n*****Inverse: Langrange's Interpolation Method******");
printf("\n****************************************************");
printf("\nEnter N ="); // No. of Data Points
scanf("%d",&n);
printf("Yg =");
scanf("%f",&yg);

for(i=0;i<=n-1;i++)
{
printf("X[%d] =",i);
scanf("%f",&x[i]);
printf("Y[%d] =",i);
scanf("%f",&y[i]);
}

// Calculation of Li terms

for(i=0;i<=n-1;i++)
{
li[i]=1;
for(j=0;j<=n-1;j++)
{
if(i!=j)
{
li[i]=li[i]*(yg-y[j])/(y[i]-y[j]);
}
}
}

// Calculation of Xg
for(i=0;i<=n-1;i++)
{
xg=xg+li[i]*x[i];
}

printf("\nSolution is %f",xg);
getch();
}

Prof. V. N. Chougule_CONM_Interpolation_01.10.2009  Page 2 

 
 

// DIVIDED DIFFERANCE METHOD


#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;
float xg,yg,x[10],y[10],dy[10][10],term[10],prod;
clrscr();
printf("\n****************DIVIDED DIFFERANCE******************");
printf("\nEnter N =");
scanf("%d",&n);
printf("Xg =");
scanf("%f",&xg);
for(i=0;i<=n-1;i++) //DATA ENTRY
{
printf("X[%d] =",i);
scanf("%f",&x[i]);
printf("Y[%d] =",i);
scanf("%f",&y[i]);
dy[i][0]=y[i];
}
// Divided Differance Table
for(j=1;j<=n-1;j++)
{
for(i=0;i<=n-j-1;i++)
{
dy[i][j]=(dy[i+1][j-1]-dy[i][j-1])/(x[i+j]-x[i]);
}
}
// Print Forward Difference Table
printf("\n DIVIDED DIFFERANCE TABLE>>> \n X \t Y \t Difference Columns ");
for(i=0;i<=n-1;i++)
{
printf("\n %.2f",x[i]);
for(j=0;j<=n-i-1;j++)
{
printf("\t %.2f",dy[i][j]);
}
}
yg=y[0];
for(i=1;i<=n-1;i++)
{
prod=1;
for(j=0;j<i;j++)
{
prod=prod*(xg-x[j]);
}
yg=yg+prod*dy[0][i];
}
printf("\n\n Yg = %f at Xg = %f",yg,xg);
Prof. V. N. Chougule_CONM_Interpolation_01.10.2009  Page 3 

 
 

getch();
}
// NEWTON FORWARD DIFFERANCE METHOD
#include<stdio.h>
#include<conio.h>

main()
{
int i,j,n;
float s,h,xg,yg,x[10],y[10],dy[10][10],term=1;
clrscr();
printf("\n****************************************************");
printf("\n*************NEWTON FORWARD DIFFERANCE**************");
printf("\n****************************************************");
printf("\nEnter N =");
scanf("%d",&n);
printf("Step Size H =");
scanf("%f",&h);
printf("X[0] =");
scanf("%f",&x[0]);
printf("Xg =");
scanf("%f",&xg);
s=(xg-x[0])/h;
printf("\nS =%f\n",s);

for(i=0;i<=n-1;i++)
{
printf("Y[%d] =",i);
scanf("%f",&y[i]);
dy[i][0]=y[i];
x[i]=x[0]+i*h;
}

// Newton Forward Differance Table


for(j=1;j<=n-1;j++)
{
for(i=0;i<=n-j-1;i++)
{
dy[i][j]=dy[i+1][j-1]-dy[i][j-1];
}
}

// Print Forward Differance Table


printf("\n FORWARD DIFFERANCE TABLE>>> \n X \t Y \t Differance Columns ");
for(i=0;i<=n-1;i++)
{
printf("\n %.2f",x[i]);
for(j=0;j<=n-i-1;j++)
{
printf("\t %.0f",dy[i][j]);
Prof. V. N. Chougule_CONM_Interpolation_01.10.2009  Page 4 

 
 

}
}

Prof. V. N. Chougule_CONM_Interpolation_01.10.2009  Page 5 

 
 

// Calculation of Yg

yg=y[0];
for(i=1;i<=n-1;i++)
{
term=1;
for(j=1;j<=i;j++)
{
term=term*(s-j+1)/j;
}
yg=yg+term*dy[0][i];
}
printf("\nSolution is %f",yg);
getch();
}

Prof. V. N. Chougule_CONM_Interpolation_01.10.2009  Page 6 

 
 

// NEWTON BACKWARD DIFFERANCE METHOD


#include<stdio.h>
#include<conio.h>

main()
{
int i,j,n;
float s,h,xg,yg,x[10],y[10],dy[10][10],term=1;
clrscr();
printf("\n****************************************************");
printf("\n*************NEWTON BACKWARD DIFFERANCE*************");
printf("\n****************************************************");
printf("\nEnter N =");
scanf("%d",&n);
printf("Step Size H =");
scanf("%f",&h);
printf("X[0] =");
scanf("%f",&x[0]);
printf("Xg =");
scanf("%f",&xg);

for(i=0;i<=n-1;i++)
{
printf("Y[%d] =",i);
scanf("%f",&y[i]);
dy[i][0]=y[i];
x[i]=x[0]+i*h;
}

// Newton Backward Differance Table


for(j=1;j<=n-1;j++)
{
for(i=n-1;i>=0;i--)
{
dy[i][j]=dy[i][j-1]-dy[i-1][j-1];
}
}

// Print Backward Differance Table


printf("\n BACKWARD DIFFERANCE TABLE>>> ");
printf("\n X \t Y \t Differance Columns ");
for(i=0;i<=n-1;i++)
{
printf("\n %.2f",x[i]);
for(j=0;j<=i;j++)
{
printf("\t %.4f",dy[i][j]);
}
}

Prof. V. N. Chougule_CONM_Interpolation_01.10.2009  Page 7 

 
 

//Printing Extracted nos for difference table

printf("\n\n Last Row for Calculations>>>\n");


for(j=0;j<=n-1;j++)
{
printf("\t %.4f",dy[n-1][j]);
}
s=(xg-x[n-1])/h;
printf("\nS =%.4f \n",s);

// Calculation of Yg

yg=y[n-1];
for(i=1;i<=n-1;i++)
{
term=1;
for(j=1;j<=i;j++)
{
term=term*(s+j-1)/j;
printf("%f",term);
}
printf("\n");
yg=yg+term*dy[n-1][i];
}

printf("\nSolution is %f",yg);
getch();
}

Prof. V. N. Chougule_CONM_Interpolation_01.10.2009  Page 8 

You might also like