NL Lab Ivsem Ece
NL Lab Ivsem Ece
IV SEMESTER
LIST OF EXPERIMENTS
SR. PAGE
NAME OF EXPERIMENT
NO. NO.
TO FIND THE ROOTS OF NON-LINEAR EQUATION USING
1 BISECTION METHOD.
3-5
TO FIND THE ROOTS OF NON-LINEAR EQUATION USING
2 NEWTON’S METHOD.
6–9
CURVE FITTING BY LEAST – SQUARE APPROXIMATIONS.
3
10-15
TO SOLVE THE SYSTEM OF LINEAR EQUATIONS USING
4 GAUSS - ELIMINATION METHOD.
16-20
TO SOLVE THE SYSTEM OF LINEAR EQUATIONS USING
5
GAUSS - SEIDAL ITERATION METHOD 21-25
TO SOLVE THE SYSTEM OF LINEAR EQUATIONS USING
6 GAUSS - JORDEN METHOD.
26-27
7 TO INTEGRATE NUMERICALLY USING TRAPEZOIDAL RULE. 28-30
8 TO INTEGRATE NUMERICALLY USING SIMPSON’S RULES. 31-33
TO FIND THE LARGEST EIGEN VALUE OF A MATRIX BY
9
POWER - METHOD. 34-37
TO FIND NUMERICAL SOLUTION OF ORDINARY
10
DIFFERENTIAL EQUATIONS BY EULER’S METHOD. 38-40
TO FIND NUMERICAL SOLUTION OF ORDINARY
11
DIFFERENTIAL EQUATIONS BY RUNGE- KUTTA METHOD. 41-43
TO FIND NUMERICAL SOLUTION OF ORDINARY
12
DIFFERENTIAL EQUATIONS BY MILNE’S METHOD. 44-47
LAB MANUAL ( IV SEM ECE) Page 2
NM LAB (MATH‐204‐F)
Program 1
OBJECTIVES: To find the roots of non linear equations using
Bisection method.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double ffunction(double x)
{
return (x * sin(x) - 1);
}
/* -------------------------------------------------------- */
void main()
{
double Delta = 1E-6; /* Tolerance for width of interval */
int Satisfied = 0; /* Condition for loop termination */
double A, B; /* Endpoints of the interval [A,B] */
double YA, YB; /* Function values at the interval-borders */
int Max; /* Calculation of the maximum number of iterations */
int K; /* Loop Counter */
double C, YC; /* Midpoint of interval and function value there */
printf("-----------------------------------------------------\n");
printf("Please enter endpoints A and B of the interval [A,B]\n");
printf("EXAMPLE : A = 0 and B = 2. Type: 0 2 \n");
scanf("%lf %lf", &A, &B);
printf("The interval ranges from %lf to %lf\n", A,B);
LAB MANUAL ( IV SEM ECE) Page 3
NM LAB (MATH‐204‐F)
if( ( (YA >= 0) && (YB >=0) ) || ( (YA < 0) && (YB < 0) ) ) {
printf("The values ffunction(A) and ffunction(B)\n");
printf("do not differ in sign.\n");
exit(0); /* exit program */
}
if(Satisfied == 1) break;
C = (A + B) / 2; /* Midpoint of interval */
YC = ffunction(C); /* Function value at midpoint */
} /* end of 'for'-loop */
printf("----------------------------------------------\n");
printf("The maximum number of iterations is : %d\n",Max);
printf("The number of performed iterations is : %d\n",K - 1);
printf("----------------------------------------------\n");
printf("The computed root of f(x) = 0 is : %lf \n",C);
printf("----------------------------------------------\n");
printf("The accuracy is +- %lf\n", B-A);
printf("----------------------------------------------\n");
printf("The value of the function f(C) is %lf\n",YC);
LAB MANUAL ( IV SEM ECE) Page 4
NM LAB (MATH‐204‐F)
Quiz
LAB MANUAL ( IV SEM ECE) Page 5
NM LAB (MATH‐204‐F)
Program 2
OBJECTIVES: To find the roots of non linear equations using Newton’s method.
Sorce code:
f(p_(k-1))
p_k = p_(k-1) - ----------- for k = 1, 2, ...
f'(p_(k-1))
-----------------------------------------------------------------------
----
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double ffunction(double x)
{
return ( pow(x,3) - 3 * x + 2 );
}
double dffunction(double x)
{
return ( 3 * pow(x,2) - 3 );
}
/* -------------------------------------------------------- */
LAB MANUAL ( IV SEM ECE) Page 6
NM LAB (MATH‐204‐F)
void main()
{
double Delta = 1E-6; /* Tolerance */
double Epsilon = 1E-6; /* Tolerance */
double Small = 1E-6; /* Tolerance */
printf("----------------------------------------------\n");
printf("Please enter initial approximation of root !\n");
scanf("%lf",&P0);
printf("----------------------------------------------\n");
printf("Initial value for root: %lf\n",P0);
Y0 = dffunction(P0);
if(Cond) break;
else Dp = Y0/Df;
if( (RelErr < Delta) && (fabs(Y1) < Epsilon) ) { /* Check for
*/
LAB MANUAL ( IV SEM ECE) Page 7
NM LAB (MATH‐204‐F)
P0 = P1;
Y0 = Y1;
}
printf("----------------------------------------------\n");
printf("The current %d -th iterate is %lf\n",K-1, P1);
printf("Consecutive iterates differ by %lf\n",Dp);
printf("The value of f(x) is %lf\n",Y1);
printf("----------------------------------------------\n");
printf("----------------------------------------------\n");
LAB MANUAL ( IV SEM ECE) Page 8
NM LAB (MATH‐204‐F)
Quiz
Q.1)What is the use of Newton’s Raphsons method?
Answer: Newton Raphsons method is used to find the root of non Linear equation.
Q2)Explain Newton Raphson method in detail.
Answer: The Newton-Raphson method uses an iterative process to approach one root of a function.
The specific root that the process locates depends on the initial, arbitrarily chosen x-value.
Here, xn is the current known x-value, f(xn) represents the value of the function at xn, and
f'(xn) is the derivative (slope) at xn. xn+1 represents the next x-value that you are trying to
find. Essentially, f'(x), the derivative represents f(x)/dx (dx = delta-x). Therefore, the term
f(x)/f'(x) represents a value of dx.
The more iterations that are run, the closer dx will be to zero (0).
Q.4) Which of the following method converges faster-Regula Falsi method or Newton-
Raphson method?
Answer: Newton Raphson method.
LAB MANUAL ( IV SEM ECE) Page 9
NM LAB (MATH‐204‐F)
Program 3
OBJECTIVES: Curve fitting by least square approximations
2 3 M-1 M
P_M(x) = c_1 + c_2*x + c_3*x + c_4*x +...+ c_M*x + c_(M+1)*x
that fits the N data points (x_1, y_1), ... , (x_N, y_N).
-----------------------------------------------------------------------
----
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/* -------------------------------------------------------- */
void main(void)
{
extern void FactPiv();
LAB MANUAL ( IV SEM ECE) Page 10
NM LAB (MATH‐204‐F)
do /* force proper input */
{
printf("Please enter degree of polynomial [Not more than
%d]\n",DMAX);
scanf("%d", &M);
} while( M > DMAX);
printf("------------------------------------------\n");
do /* force proper input */
{
printf("Please enter number of points [Not more than
%d]\n",NMAX);
scanf("%d", &N);
} while( N > NMAX);
P[0] = N;
LAB MANUAL ( IV SEM ECE) Page 11
NM LAB (MATH‐204‐F)
x = X[K-1];
p = X[K-1];
FactPiv(M+1, A, B);
} /* end main */
/*--------------------------------------------------------*/
void FactPiv(N, A, B)
int N;
double A[DMAX][DMAX];
double *B;
{
int T;
/* Start LU factorization */
LAB MANUAL ( IV SEM ECE) Page 12
NM LAB (MATH‐204‐F)
for (K = P + 1; K <= N; K++)
{
if ( fabs(A[Row[K-1]][P-1]) > fabs(A[Row[P-1]][P-1]) )
{
/* Switch the index for the p-1 th pivot row if necessary */
T = Row[P-1];
Row[P-1] = Row[K-1];
Row[K-1] = T;
DET = - DET;
}
if (A[Row[P-1]][P-1] == 0)
{
printf("The matrix is SINGULAR !\n");
printf("Cannot use algorithm --> exit\n");
exit(1);
}
/* Form multiplier */
/* Eliminate X_(p-1) */
Y[0] = B[Row[0]];
for ( K = 2; K <= N; K++)
{
SUM =0;
for ( C = 1; C <= K -1; C++) SUM += A[Row[K-1]][C-1] * Y[C-1];
Y[K-1] = B[Row[K-1]] - SUM;
}
LAB MANUAL ( IV SEM ECE) Page 13
NM LAB (MATH‐204‐F)
if( A[Row[N-1]][N-1] == 0)
{
printf("The matrix is SINGULAR !\n");
printf("Cannot use algorithm --> exit\n");
exit(1);
}
/* Output */
printf("---------------------------------------------------:\n");
printf("The components of the vector with the solutions are:\n");
for( K = 1; K <= N; K++) printf("X[%d] = %lf\n", K, X[K-1]);
/*
-----------------------------------------------------------------------
----
");
LAB MANUAL ( IV SEM ECE) Page 14
NM LAB (MATH‐204‐F)
Quiz
Q.1) Write down working procedure of Least square method
Answer: (a) To fit the straight line y=a+bx
i) Substitute the observed the set of n values in this equation.
ii) Form normal equation for each constant i.e. ∑y=na =b∑x, ∑xy=a∑x+b∑x2
iv) Substitute the values of a & b in y=a+bx, which is required line of best fit
LAB MANUAL ( IV SEM ECE) Page 15
NM LAB (MATH‐204‐F)
Program 4
OBJECTIVES: To solve the system of linear equations using
gauss elimination method
Source Code:
(Gauss-Elimination-Iteration).
-----------------------------------------------------------------------
----
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/* -------------------------------------------------------- */
#define N 4
void main(void)
{
Float a[N][N+1],x[N],t,s;
Int i,j,k;
Printf(“Enter the element of the argumented matrix row
wise\n”);
For(i=0;i<N;i++)
For(j=0;j<N+1;j++)
Scanf(“%f”,&a[i][j]);
For(j=0;j<N-1;j++)
For(i=j+1;i<N;i++)
{
t=a[i][j]/a[j][j];
for(k=0;k<N+1;k++)
a[i][k]=a[j][k]*t;
}
For(i=0;i<N;i++)
LAB MANUAL ( IV SEM ECE) Page 16
NM LAB (MATH‐204‐F)
For(j=0;j<N+1;j++)
printf(“%8.4f%”,a[i][j]);
Printf(“\n”);
For(i=N-1;i>=0;i--)
{
s=0;
for(j=i+1;j<N;j++)
s+=a[i][j]*x[j];
x[i]=(a[i][N]-s)/a[i][i];
For(i=0;i<N;i++)
Printf(“x[%3d]=%7.4f\n”, i+1,x[i]);
LAB MANUAL ( IV SEM ECE) Page 17
NM LAB (MATH‐204‐F)
LAB MANUAL ( IV SEM ECE) Page 18
NM LAB (MATH‐204‐F)
We use elementary row operations to transform this matrix into a triangular one. We keep
the first row and use it to produce all zeros elsewhere in the first column. We have
Next we keep the first and second row and try to have zeros in the second column. We
get
Next we keep the first three rows. We add the last one to the third to get
x = 2 + y + z - w - v.
Using algebraic manipulations, we get
LAB MANUAL ( IV SEM ECE) Page 19
NM LAB (MATH‐204‐F)
x=- - s - t.
LAB MANUAL ( IV SEM ECE) Page 20
NM LAB (MATH‐204‐F)
Program 5
OBJECTIVES: To Solve The System Of Linear Equations Using
Gauss - Seidal Iteration Method
void main()
{
double x1=0,x2=0,x3=0,y1,y2,y3;
int i=0;
clrscr();
printf("\n__________________________________________\n");
printf("\n x1\t\t x2\t\t x3\n");
printf("\n__________________________________________\n");
printf("\n%f\t%f\t%f",x1,x2,x3);
do
{
y1=X1(x2,x3);
y2=X2(y1,x3);
y3=X3(y1,y2);
if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP )
{
printf("\n__________________________________________\n");
printf("\n\nx1 = %.3lf",y1);
printf("\n\nx2 = %.3lf",y2);
printf("\n\nx3 = %.3lf",y3);
i = 1;
}
else
{
x1 = y1;
x2 = y2;
x3 = y3;
printf("\n%f\t%f\t%f",x1,x2,x3);
}
}while(i != 1);
getch();
}
/*
OUT PUT
___________
__________________________________________
x1 x2 x3
LAB MANUAL ( IV SEM ECE) Page 21
NM LAB (MATH‐204‐F)
__________________________________________
x1 = 2.110
x2 = -1.173
x3 = 0.863
*/
LAB MANUAL ( IV SEM ECE) Page 22
NM LAB (MATH‐204‐F)
Quiz
Q.1)Write down converges property of Gauss Seidal method.
Answer: The convergence properties of the Gauss–Seidel method are dependent on the
matrix A. Namely, the procedure is known to converge if either:
• A is symmetric positive-definite, or
• A is strictly or irreducibly diagonally dominant.
The Gauss–Seidel method sometimes converges even if these conditions are not satisfied.
where:
Then A can be decomposed into a lower triangular component L*, and a strictly upper
triangular component U:
The Gauss–Seidel method is an iterative technique that solves the left hand side of this
expression for x, using previous value for x on the right hand side. Analytically, this may
be written as:
LAB MANUAL ( IV SEM ECE) Page 23
NM LAB (MATH‐204‐F)
However, by taking advantage of the triangular form of L*, the elements of x(k+1) can be
computed sequentially using forward substitution:
Note that the sum inside this computation of xi(k+1) requires each element in x(k) except
xi(k) itself.
The procedure is generally continued until the changes made by an iteration are below
some tolerance.
Suppose we choose (0, 0, 0, 0) as the initial approximation, then the first approximate
solution is given by
LAB MANUAL ( IV SEM ECE) Page 24
NM LAB (MATH‐204‐F)
Using the approximations obtained, the iterative procedure is repeated until the desired
accuracy has been reached. The following are the approximated solutions after four
iterations.
LAB MANUAL ( IV SEM ECE) Page 25
NM LAB (MATH‐204‐F)
Program 6
OBJECTIVES: To Solve The System Of Linear Equations Using
Gauss - Jorden Method.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{float a[6][6],b[6],x[6],t,s;
int i,j,n,k;
clrscr();
cout<<"Enter the maximum no. of matrix"<<endl;
cin>>n;
cout<<"Enter th elements of matrix"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the right constant"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i][n];
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
if(i!=k)
{
for(j=k+1;j<n+1;j++)
{
a[i][j]=a[i][j]-(a[i][k]/a[k][k])*a[k][j]);
}}}
cout<<"the solution is"<<endl;
for(i=0;i<n;i++)
{
x[i]=(a[i][n]/a[i][i]);
cout<<"x["<<i<<"]="<<x[i]<<endl;
}
getch();
}
LAB MANUAL ( IV SEM ECE) Page 26
NM LAB (MATH‐204‐F)
QUIZ
Q.3) What are the numerical method of gauss elimination and gauss Jordan method?
Answer: In Computer Programming, Algebra, Linear Algebra
LAB MANUAL ( IV SEM ECE) Page 27
NM LAB (MATH‐204‐F)
Program 7
OBJECTIVES: To integrate numerically using trapezoidal rule.
(Trapezoidal Rule).
Composite Trapezoidal
b
/ h M-1
| f(x)dx = - * [ f(A) - f(B)] + h * sum f(x_k)
/ 2 k=1
a
-----------------------------------------------------------------------
----
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double ffunction(double x)
{
return ( 1 / ( 1 + pow(x,2) ) );
}
/* -------------------------------------------------------- */
void main()
{
int K; /* loop counter */
int M; /* INPUT : number of subintervals */
LAB MANUAL ( IV SEM ECE) Page 28
NM LAB (MATH‐204‐F)
double A,B; /* INPUT : boundaries of integral */
double H; /* Subinterval width */
double SUM = 0; /* approx. integral value */
double X;
printf("----------------------------------------------\n");
printf("Please enter the boundaries of the integral [A,B]\n");
printf("EXAMPLE: A = -1 and B = 1, so type: -1 1\n");
scanf("%lf %lf",&A, &B);
printf("The boundaries of the integral are: %lf %lf\n",A, B);
printf("----------------------------------------------\n");
printf("Please enter the number of SUBINTERVALS.\n");
scanf("%d",&M);
printf("----------------------------------------------\n");
printf("----------------------------------------------\n");
LAB MANUAL ( IV SEM ECE) Page 29
NM LAB (MATH‐204‐F)
Quiz
Q.1)What is use of Trapezoidal rule?
Answer: Trapezoidal rule is used to solve numerical differentiation.
LAB MANUAL ( IV SEM ECE) Page 30
NM LAB (MATH‐204‐F)
Program 8
OBJECTIVES: To Integrate Numerically Using Simpson’s Rules.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],sum=0,h,temp;
int i,n,j,k=0;
float fact(int);
clrscr();
printf("\nhow many record you will be enter: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n\nenter the value of x%d: ",i);
scanf("%f",&x[i]);
printf("\n\nenter the value of f(x%d): ",i);
scanf("%f",&y[i]);
}
h=x[1]-x[0];
n=n-1;
sum = sum + y[0];
for(i=1;i<n;i++)
{
if(k==0)
{
sum = sum + 4 * y[i];
k=1;
}
else
{
sum = sum + 2 * y[i];
k=0;
}
}
sum = sum + y[i];
sum = sum * (h/3);
printf("\n\n I = %f ",sum);
getch();
}
/*
______________________________________
OUT PUT
______________________________________
LAB MANUAL ( IV SEM ECE) Page 31
NM LAB (MATH‐204‐F)
enter the value of x0: 0
I = 0.693250
*/
LAB MANUAL ( IV SEM ECE) Page 32
NM LAB (MATH‐204‐F)
QUIZ
Q.1) What is use of Simpsons one-third rule?
Answer: Simpsons one-third rule is used to find numerical integration of given equation.
LAB MANUAL ( IV SEM ECE) Page 33
NM LAB (MATH‐204‐F)
Program 9
OBJECTIVES: To find the largest eigen value of matrix by
power method.
|Lambda_1| > |Lambda_2| >= |Lambda_3| >= ... >= |Lambda_n| > 0
-----------------------------------------------------------------------
----
*/
Source code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MaxOrder 50
/* -------------------------------------------------------- */
void main(void)
{
int i, j; /* Loop counter */
int N; /* Order of Matrix */
double Epsilon = 1E-7; /* Tolerance */
double Max = 100; /* Maximum number of iterations */
double X[MaxOrder], Y[MaxOrder];
double A[MaxOrder][MaxOrder]; /* Matrix */
double C1, DC, DV, Lambda = 0;
int Count = 0, Iterating = 1;
double Sum, Err = 1;
double MaxElement;
printf("--------------Power Method---------------\n");
printf("--------- Example 11.5 on page 550 -------------\n");
printf("-----------------------------------------------\n");
LAB MANUAL ( IV SEM ECE) Page 34
NM LAB (MATH‐204‐F)
printf(" Number of steps must be less than %d\n",MaxOrder+1);
printf(" Terminating. Sorry\n");
exit(0);
}
printf("Please enter elements of matrix A row by row:\n");
printf("\n");
/* Initialize vector X */
MaxElement = 0;
for ( j = 0; j < N; j++ )
{
if( fabs(Y[j]) > fabs(MaxElement) ) MaxElement = Y[j];
}
C1 = MaxElement;
DC = fabs(Lambda - C1);
Sum = 0;
for ( i = 0; i < N; i++) Sum += pow( ( Y[i] - X[i] ), 2.0);
DV = sqrt(Sum);
Err = MAX(DC,DV);
LAB MANUAL ( IV SEM ECE) Page 35
NM LAB (MATH‐204‐F)
/* Update vector X and scalar Lambda */
Iterating = 0;
Count++;
printf("---------------------------------------\n");
printf("---------------------------------------\n");
printf("Lambda = %lf\n", Lambda);
LAB MANUAL ( IV SEM ECE) Page 36
NM LAB (MATH‐204‐F)
Quiz
LAB MANUAL ( IV SEM ECE) Page 37
NM LAB (MATH‐204‐F)
Program 10
OBJECTIVES: TO FIND NUMERICAL SOLUTION OF ORDINARY
DIFFERENTIAL EQUATIONS BY EULER’S METHOD.
/*
OUT PUT
---------
y1 = 5.000
LAB MANUAL ( IV SEM ECE) Page 38
NM LAB (MATH‐204‐F)
x = 1.400 => y6 = 8.808
*/
LAB MANUAL ( IV SEM ECE) Page 39
NM LAB (MATH‐204‐F)
QUIZ
Q.5)Write the equation for Eulers method for findind approximate solution?
LAB MANUAL ( IV SEM ECE) Page 40
NM LAB (MATH‐204‐F)
Program 11
OBJECTIVES: To Find Numerical Solution Of Ordinary
Differential Equations By Runge- Kutta Method.
#include <stdio.h>
#include <math.h>
void main()
{
double t, y[N];
int j;
fclose(output);
}
LAB MANUAL ( IV SEM ECE) Page 41
NM LAB (MATH‐204‐F)
for (i=0;i<N;i++)
{
t1[i]=y[i]+0.5*(k1[i]=step*f(x,y,i));
}
for (i=0;i<N;i++)
{
t2[i]=y[i]+0.5*(k2[i]=step*f(x+h, t1, i));
}
for (i=0;i<N;i++)
{
t3[i]=y[i]+ (k3[i]=step*f(x+h, t2, i));
}
for (i=0;i<N;i++)
{
k4[i]= step*f(x+step, t3, i);
}
for (i=0;i<N;i++)
{
y[i]+=(k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;
}
}
if (i==0)
x=y[1]; /* derivative of first equation */
if (i==1)
x= -0.2*y[1]-y[0]; /* derivative of second equation */
return x;
}
LAB MANUAL ( IV SEM ECE) Page 42
NM LAB (MATH‐204‐F)
QUIZ
LAB MANUAL ( IV SEM ECE) Page 43
NM LAB (MATH‐204‐F)
Program 12
OBJECTIVES: To find the numerical solution of differential
equation using mline method.
(Milne-Simpson Method)
4h
p_(k+1) = y_(k-3) + --- [2*f_(k-2) - f_(k-1) + 2*f_k]
3
h
y_(k+1) = y_(k-1) + --- [f_(K-1) + 4*f_k + f_(K+1)].
3
-----------------------------------------------------------------------
----
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
{
return ( (t - y) / 2.0 );
}
/* -------------------------------------------------------- */
void main(void)
LAB MANUAL ( IV SEM ECE) Page 44
NM LAB (MATH‐204‐F)
int I, K; /* Loop counter */
int N; /* Number of steps > 3 */
double A, B, Y[MAX]; /* Endpoints and initial value */
double H; /* Compute the step size */
double T[MAX];
double F1, F2, F3, F4; /* Function values */
double Hmin, Hmax; /* Minimum and maximum step size */
double Pold, Yold; /* Predictor and Corrector */
double Pmod, Pnew; /* Modifier */
printf("------------Milne-Simpson Method----------------\n");
printf("--------- Example 9.13 on page 468 -------------\n");
printf("-----------------------------------------------\n");
printf("Please enter endpoints of the interval [A,B]:\n");
printf("For used Example type : 0, 3.0\n");
scanf("%lf, %lf", &A, &B);
printf("You entered [%lf, %lf]\n", A, B);
printf("-----------------------------------------\n");
printf("Please enter number of steps: ( > 3 and < %d !)\n", MAX+1);
scanf("%d",&N);
if( (N > MAX) || (N < 4) )
{
printf(" Number of steps must be greater than 3 and less than
%d\n",MAX+1);
printf(" Terminating. Sorry\n");
exit(0);
}
printf("-----------------------------------------\n");
H = (B - A) / N;
T[0] = A;
F1 = ffunction(T[1],Y[1]);
F2 = ffunction(T[2],Y[2]);
F3 = ffunction(T[3],Y[3]);
Pold = 0;
Yold = 0;
LAB MANUAL ( IV SEM ECE) Page 45
NM LAB (MATH‐204‐F)
{
/* Milne Predictor */
Pnew = Y[K-3] + 4.0 * H * (2.0*F1 - F2 + 2.0*F3) / 3.0;
Pmod = Pnew + 28.0 * (Yold - Pold) / 29.0;
/* Next mesh point */
T[K+1] = A + H * (K+1);
/* Evaluate f(t,y) */
F4 = ffunction(T[K+1],Pmod);
/* Corrector */
Y[K+1] = Y[K-1] + H * (F2 + 4.0 * F3 + F4) / 3.0;
/* Update the values */
F1 = F2;
F2 = F3;
F3 = ffunction(T[K+1], Y[K+1]);
}
/* Output */
LAB MANUAL ( IV SEM ECE) Page 46
NM LAB (MATH‐204‐F)
QUIZ
LAB MANUAL ( IV SEM ECE) Page 47
NM LAB (MATH‐204‐F)
Program 13
OBJECTIVES: To find the numerical solution of differential
equation using laplace equation.
2
To approximate the solution of u_(xx)(x,y) + u_(yy) (x,y) = 0
-----------------------------------------------------------------------
---*/
Source code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define Max 50
/* Global Variables */
/* Prototypes */
double F1i(int i)
{
extern double H;
double arg;
LAB MANUAL ( IV SEM ECE) Page 48
NM LAB (MATH‐204‐F)
arg = H * ( i - 1.0 );
double F2i(int i)
{
extern double H;
double arg;
arg = H * ( i - 1.0 );
double F3i(int i)
{
extern double H;
double arg;
arg = H * ( i - 1.0 );
double F4i(int i)
{
extern double H;
double arg;
arg = H * ( i - 1.0 );
LAB MANUAL ( IV SEM ECE) Page 49
NM LAB (MATH‐204‐F)
}
/* -------------------------------------------------------- */
void main(void)
{
int i, j; /* Loop Counters
*/
double A, B; /* INPUT : Width and height of Rectangle
*/
double Ave; /* INPUT : Initial approximation
*/
int N, M; /* dimensions of the grid
*/
double U[Max][Max]; /* Grid-Amplitudes
*/
int Count; /* counter for while loop
*/
double Tol; /* stop criteria
*/
double w, Pi;
double H; /* Grid spacing
*/
double Relax, temp;
printf("-------------------------------------------------------
\n");
printf("------- Dirichlet Method for Laplace's Equation -------
\n");
printf("--------- Try Example 10.6 on page 528/529-------------
\n");
printf("-------------------------------------------------------
\n");
LAB MANUAL ( IV SEM ECE) Page 50
NM LAB (MATH‐204‐F)
printf("For present example type 9\n");
scanf("%d",&M);
printf("You entered N = %d and M = %d\n", N, M);
printf("------------------------------------------------\n");
printf("\n");
printf("Please enter the initial approximation :\n");
printf("For present example type 70\n");
scanf("%lf",&Ave);
printf("You entered = %lf\n", Ave);
printf("------------------------------------------------\n");
printf("\n");
Pi = 3.1415926535;
Tol = 1.0;
Count = 0.0;
LAB MANUAL ( IV SEM ECE) Page 51
NM LAB (MATH‐204‐F)
Tol = 0.0;
for ( j = 2; j <= M - 1; j++ )
{
for ( i = 2; i <= N - 1; i++ )
{
Relax = w * ( U[i][j+1] + U[i][j-1] + U[i+1][j] +
U[i-1][j] - 4.0 * U[i][j] ) / 4.0;
U[i][j] += Relax;
if( fabs(Relax) > Tol ) Tol = fabs(Relax);
}
Count++;
}
}
LAB MANUAL ( IV SEM ECE) Page 52
NM LAB (MATH‐204‐F)
QUIZ
Q.1)What is the use of Laplace method?
Answer: Laplace method is used to find out the solution of partial differential equation.
Q.2)What is the boundary of Rectangular region R.
Answer: u(x,y) is the boundary of rectangular region R.
Q.3)What is the first step in Laplace equation?
Answer:Divide the given into network of square mesh of side h.
Q.4)What is the four mesh point?
Answer:The four mesh point is the average of its values at four neighbouring point to the
left, right, above and below.
Q.5)How can we find the interior points?
Answer: Interior points are computed by the standard five point formula.
LAB MANUAL ( IV SEM ECE) Page 53
NM LAB (MATH‐204‐F)
Program 14
OBJECTIVES: To find the numerical solution of differential
equation using wave equation
2
To approximate the solution of u_(tt)(x,t) = c u_(xx)(x,t)
Souce code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define Max 50
/* Global Variables */
/* Prototypes */
double Fi(int i)
{
extern double H;
double arg;
arg = H * (i - 1);
if( (arg >= 0) && (arg <= 1.0) ) return ( sin ( 3.1415926 * arg)
);
else
{
printf(" Fi() :Argument not in range ! Exiting !\n");
LAB MANUAL ( IV SEM ECE) Page 54
NM LAB (MATH‐204‐F)
printf(" arg : %lf\n", arg);
exit(0);
}
}
double Gi(int i)
{
extern double H;
double arg;
arg = H * (i - 1);
/* -------------------------------------------------------- */
void main(void)
{
int i, j; /* Loop Counters
*/
double A, B; /* INPUT :Width and height of Rectangle
*/
double C; /* INPUT : Wave equation const.
*/
int N, M; /* Dimensions of the grid
*/
double K, R, R2, R22, S1, S2;
double U[Max][Max]; /* Grid-Amplitudes
*/
LAB MANUAL ( IV SEM ECE) Page 55
NM LAB (MATH‐204‐F)
printf("-------------------------------------------------------
\n");
printf("\n");
printf("Please enter dimension of grid in x-direction :\n");
printf("For present example type : 6\n");
scanf("%d",&N);
printf("Please enter dimension of grid in y-direction :\n");
printf("For present example type : 6\n");
scanf("%d",&M);
printf("You entered N = %d and M = %d\n", N, M);
printf("-------------------------------------------------------
\n");
printf("\n");
printf("Please enter the wave equation constant C :\n");
printf("For present example type : 2\n");
scanf("%lf",&C);
printf("You entered C = %lf\n", C);
printf("------------------------------------------------\n");
printf("\n");
H = A / ( N - 1 );
K = B / ( M - 1 );
R = C * K / H;
R2 = R * R;
R22 = R * R / 2.0;
S1 = 1.0 - R * R;
S2 = 2.0 - 2.0 * R * R;
/* Boundary conditions */
LAB MANUAL ( IV SEM ECE) Page 56
NM LAB (MATH‐204‐F)
printf("\n");
}
LAB MANUAL ( IV SEM ECE) Page 57
NM LAB (MATH‐204‐F)
Quiz
LAB MANUAL ( IV SEM ECE) Page 58
NM LAB (MATH‐204‐F)
Program 15
OBJECTIVES: To find the numerical solution of differential
equation using heat equation.
Source code:
(Forward-Difference Method for the Heat Equation).
-----------------------------------------------------------------------
----
*/
/*
-----------------------------------------------------------------------
----
2
To approximate the solution of u_(t)(x,t) = c u_(x)(x,t)
-----------------------------------------------------------------------
----
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define Max 50
/* Global Variables */
/* Prototypes */
double Fi(int i)
{
LAB MANUAL ( IV SEM ECE) Page 59
NM LAB (MATH‐204‐F)
extern double H;
double arg;
arg = H * (i - 1);
if( (arg >= 0) && (arg <= 1.0) ) return (4.0 * arg - 4.0 * arg *
arg);
else
{
printf(" Fi() :Argument not in range ! Exiting !\n");
printf(" arg : %lf\n", arg);
exit(0);
}
}
/* -------------------------------------------------------- */
void main(void)
{
int i, j; /* Loop Counters
*/
double A, B; /* INPUT :Width and height of Rectangle
*/
double C; /* INPUT : Wave equation const.
*/
int N, M; /* Dimensions of the grid
*/
double K, S, C1, C2, R;
double U[Max][Max]; /* Grid-Amplitudes
*/
LAB MANUAL ( IV SEM ECE) Page 60
NM LAB (MATH‐204‐F)
printf("Please enter dimension of grid in x-direction ( < 50
):\n");
printf("For present example type : 6\n");
scanf("%d",&N);
if ( N >= 50 )
{
printf("MUST BE SMALLER THAN 50. ... terminating.\n");
exit(0);
}
printf("Please enter dimension of grid in y-direction ( < 50
):\n");
printf("For present example type : 11\n");
scanf("%d",&M);
if ( M >= 50 )
{
printf("MUST BE SMALLER THAN 50. ... terminating.\n");
exit(0);
}
printf("You entered N = %d and M = %d\n", N, M);
printf("-------------------------------------------------------
\n");
printf("\n");
printf("Please enter the heat-equation constant C :\n");
printf("For present example type : 1\n");
scanf("%lf",&C);
printf("You entered C = %lf\n", C);
printf("------------------------------------------------\n");
printf("\n");
printf("Please enter constant C1 = u(0,t) :\n");
printf("For present example type : 0\n");
scanf("%lf",&C1);
printf("You entered C1 = %lf\n", C1);
printf("------------------------------------------------\n");
printf("\n");
printf("Please enter constant C2 = u(A,t) :\n");
printf("For present example type : 0\n");
scanf("%lf",&C2);
printf("You entered C2 = %lf\n", C2);
printf("------------------------------------------------\n");
printf("\n");
H = A / ( N - 1 );
K = B / ( M - 1 );
R = C * C * K / ( H * H );
S = 1.0 - 2.0 * R;
/* Boundary conditions */
LAB MANUAL ( IV SEM ECE) Page 61
NM LAB (MATH‐204‐F)
/* First row */
printf("\n");
}
LAB MANUAL ( IV SEM ECE) Page 62
NM LAB (MATH‐204‐F)
QUIZ
REFRENCES:
1. Numerical methods by B.S.Grewal
LAB MANUAL ( IV SEM ECE) Page 63