Lab manuals
Numerical Analysis
Date:
Department of Mechatronics Engineering Wah Engineering College Wah Cantt
List of Experiments
Subject: Numerical Analysis (GS-202)
Semester: 5th
Instructor Name: Sajid khan
Session: Spring 2015
Sr. #
List of Experiments
01
Introduction to MATLAB, Basic Concepts and Functions
02
Implementation of Matrices, Polynomials
03
Implementation of Graphs in MATLAB
04
Implementation of Solution of Transcendental Equations by Bisection Method
05
Implementation of Regula-Falsi Method
06
Implementation of Newton-Raphson Method
07
Implementation of Secant Method
08
Implementation of Fixed Point Iteration Method
09
Implementation of Gauss-Jacobi Method for Solution of System of Linear Equations
10
Implementation of Gauss- Seidel Method for Solution of System of linear Equations
11
Implementation of Interpolation by Newton Forward Difference Formula
12
Implementation of Lagrange Interpolation Formula
13
Implementation of Newtons Divided Difference Formula
14
Implementation of Simpsons Rule
15
Implementation of Solution of First Order Differential Equations by Runge-Kutta
Method
Experiment # 01
Title
Introduction to MATLAB basic concepts, and functions
Wah Engineering College
Page 1
Lab manuals
Numerical Analysis
Date:
Software
MATLAB
Learning objectives
Introduction
The most important elements of the MATLAB screen are the following:
The Command Window: This runs MATLAB functions.
The Command History: This presents a history of the functions introduced in the Command
Window and allows you to copy and execute them.
The Launch Pad: This runs tools and gives you access to documentation for all MathWorks
products currently installed on your computer.
The Current Directory: This shows MATLAB files and execute files (such as opening and search
for content operations).
Help (support): This allows you to search and read the documentation for the complete family
of MATLAB products.
The Workspace: This shows the present contents of the workspace and allows you to make
changes to it.
The Array Editor: This displays the contents of arrays in a tabular format and allows you to edit their
values.
The Editor/Debugger: This allows you to create, edit, and check M-files (files that contain
MATLAB functions).
List of commands
Wah Engineering College
Page 2
Lab manuals
Numerical Analysis
Experiment # 02
Title
Implementation of matrices, and polynomials
Software
MATLAB
Learning Objectives
Introduction
List of commands
Wah Engineering College
Page 3
Date:
Lab manuals
Numerical Analysis
Date:
Experiment # 03
Title
Implementation of Graphs in MATLAB
Software
MATLAB
Learning Objectives
Introduction
MATLAB provides a very good tools for two dimensional, three dimensional plots, 3-D volume visualization
functions, tools for interactively creating plots and the ability to export results to many popular graphics formats. It is
possible to customize plot by adding multiple axes, changing line colures and markers, adding annotations and
legends.
Vector data can be visualized with 3-D plotting functions such as line, area, bar and pie charts, direction and velocity
plots, histograms polygons and surfaces, scatter/bubble plots, and animations. MATLAB provides functions for 2-D
Wah Engineering College
Page 4
Lab manuals
Numerical Analysis
Date:
matrices, 3-D scaler and vector data. In MATLAB one can characterize plots, such as viewing angle, perspective,
lighting effect, light source location and transparency. 3-D plotting functions include surface, contour, mesh, image
plots etc., the graphics commands in MATLAB are very simple and easy to understand.
List of Commands
Command
plot
xlabel
ylabel
semilogx
title
semilogy
grid on
loglog
text
polar
gtext
area
axis
bar
subplot
barh
legend
hist
rose
pie
stairs
stem
compass
Description
To draw a 2-D plot
To label the x-axis
To label the y-axis
Plot
with
linear
scale
y-axis and logarithmic scale x-axis
To
give
a title
to the
figure
Plot
withthe
linear
logarithmic scale y-axis
To
show
grid scale
lines x-axis
on the and
graph
Plot
with
logarithmic
scale
x-axis
and y-axis
To write illustrative text on the graph
Toplace
drawtext
a plot
To
onon
thepolar
graphcoordinates
using the mouse
Same
as
plot
except
area
under
plot is filled with colour
To control the axis of plot
Tosub
draw
vertical
barwindow
graph into sub-graphs
To
divide
graph
Toproduce
draw horizontal
bar graph
To
a boxed legend
on the plot
To draw a histogram
To draw an angle histogram
To draw pie plot
To give staircase plot
To give stem plot
To plot the complex data
3-D plot functions
plot3
bar3
bar3h
pie3
To plot a simple 3-D plot
To plot a 3-D vertical bar graph
To plot a 3-D horizontal bar graph
To plot 3-D pie graph
Tasks
Task 1
Wah Engineering College
Page 5
2-D plot functions
Lab manuals
Numerical Analysis
Date:
Experiment # 04
Title
Implementation of Solution of Transcendental Equations by Bisection Method
Software
MATLAB
Theory
f ( x) 0
The steps to apply the bisection method to find the root of the equation
1. Choose
are
f ( x ) f ( xu ) 0
xu
and
as two guesses for the root such that
, or in other words,
xu
x
sign between
and .
xm
xu
x
f ( x) 0
2. Estimate the root,
, of the equation
as the mid-point between
and
as
x xu
xm =
2
3. Now check the following
f ( x ) f ( xm ) 0
xm
xu x m
x
x x
a) If
, then the root lies between
and ; then
and
.
Wah Engineering College
Page 6
f (x)
changes
Lab manuals
Numerical Analysis
f ( x ) f ( xm ) 0
b) If
xm
Date:
xu
x x m
xu xu
, then the root lies between
and ; then
and
xm
c) If
; then the root is . Stop the algorithm if this is true.
4. Find the new estimate of the root
x xu
xm =
2
f ( x ) f ( x m ) 0
Find the absolute relative approximate error as
xmnew - xmold
a =
100
xmnew
Where
xmnew
= estimated root from present iteration
xmold
= estimated root from previous iteration
5. Compare the absolute relative approximate error
with the pre-specified relative error tolerance . If
a s
, then go to Step 3, else stop the algorithm. Note one should also check whether the number of
iterations is more than the maximum number of iterations allowed. If so, one needs to terminate the
algorithm and notify the user about it.
Code
clc
x0 = -2.0 ;
x1 = -1.0 ;
n = 15
fx0 = x0^5 + x0^3 +4*x0^2 - 3*x0 - 2;
fx1 = x1^5 + x1^3 +4*x1^2 - 3*x1 - 2;
fprintf(' k
x0
xmid
x1 f(xmid)\n');
for k=1:n
xm = x0 + 0.5*(x1-x0);
fm = xm^5 + xm^3 +4*xm^2 - 3*xm - 2;
fprintf('%3d\t %.8f \t %.8f\t %.8f \t %.8e \n',k,x0,xm,x1,fm);
if sign(fm) == sign(fx0)
x0 = xm;
fx0 = fm;
Wah Engineering College
Page 7
Lab manuals
Numerical Analysis
else
x1 = xm;
fx1 = fm;
end
end
Result
n=
15
k
x0
xmid
1
-2.00000000 -1.50000000
2
-2.00000000 -1.75000000
3
-1.75000000 -1.62500000
4
-1.62500000 -1.56250000
5
-1.56250000 -1.53125000
6
-1.53125000 -1.51562500
7
-1.53125000 -1.52343750
8
-1.53125000 -1.52734375
9
-1.53125000 -1.52929688
10
-1.53125000 -1.53027344
11
-1.53027344 -1.52978516
12
-1.52978516 -1.52954102
13
-1.52954102 -1.52941895
14
-1.52941895 -1.52935791
15
-1.52941895 -1.52938843
Conclusions
From this we conclude that
x1
-1.00000000
-1.50000000
-1.50000000
-1.50000000
-1.50000000
-1.50000000
-1.51562500
-1.52343750
-1.52734375
-1.52929688
-1.52929688
-1.52929688
-1.52929688
-1.52929688
-1.52935791
f(xmid)
5.31250000e-001
-6.27246094e+000
-2.18447876e+000
-6.74798012e-001
-3.61247361e-002
2.56196528e-001
1.12228746e-001
3.86045517e-002
1.37858857e-003
-1.73383354e-002
-7.97119735e-003
-3.29413644e-003
-9.57232078e-004
2.10813694e-004
-3.73175328e-004
Bisection method is one of the simplest method.
It is more reliable.
It is not the fastest method for finding roots.
The Bisection Method will always work, once you have found starting points.
It is always possible to find the number of steps required for a given accuracy.
Wah Engineering College
Page 8
Date:
Lab manuals
Numerical Analysis
Date:
Experiment # 05
Title
Implementation of Regula-Falsi Method
Software
MATLAB
Theory
The steps to apply the false-position method to find the root of the equation
1. Choose
between
xL
xL
as two guesses for the root such that
xU
and
are as follows.
f x L f xU 0
xU
and
f x 0
Wah Engineering College
Page 9
, or in other words,
f x
changes sign
Lab manuals
2. Estimate the root,
xr
Numerical Analysis
xr
of the equation
f x 0
Date:
as
xU f x L x L f xU
f x L f xU
3. Now check the following
If
If
If
f x L f xr 0
f x L f xr 0
f x L f xr 0
, then the root lies between
, then the root lies between
, then the root is
xr
xL
xr
and
xr
; then
xU
and
; then
xL xL
x L xr
xU x r
and
xU xU
and
. Stop the algorithm.
4. Find the new estimate of the root
xr
xU f x L x L f xU
f x L f xU
Find the absolute relative approximate error as
a
x rnew x rold
100
x rnew
Where
xrnew
x rold
= estimated root from present iteration
= estimated root from previous iteration
a s
5. Compare the absolute relative approximate error
with the pre-specified relative error tolerance . If
,
then go to step 3, else stop the algorithm. Note one should also check whether the number of iterations is more than
the maximum number of iterations allowed. If so, one needs to terminate the algorithm and notify the user about it.
Note that the false-position and bisection algorithms are quite similar. The only difference is the formula used to
calculate the new estimate of the root
Wah Engineering College
xr
as shown in steps #2 and #4!
Page 10
Lab manuals
Numerical Analysis
Code
clc
clear all
N=15;
a=0;
b=2;
f=@(x)x^2-1;
fprintf('n
a
b
for n=1:N;
c=b-(b-a)/(f(b)-f(a))*f(b);
fprintf('%3d %8.15f %8.15f
if f(a)*f(b)<0
a=a;
b=c;
else
a=c;
b=b;
end
end
Result
n
c
%8.15f
Date:
f(c)\n' )
%8.15f\n',n,a,b,c,f(c))
f(c)
0.000000000000000
2.000000000000000
0.500000000000000
-0.750000000000000
0.000000000000000
0.500000000000000
2.000000000000000
3.000000000000000
2.000000000000000
0.500000000000000
0.800000000000000
-0.360000000000000
2.000000000000000
0.800000000000000
0.928571428571429
-0.137755102040816
2.000000000000000
0.928571428571429
0.975609756097561
-0.048185603807258
2.000000000000000
0.975609756097561
0.991803278688525
-0.016326256382693
2.000000000000000
0.991803278688525
0.997260273972603
-0.005471945956089
2.000000000000000
0.997260273972603
0.999085923217550
-0.001827318028535
2.000000000000000
0.999085923217550
0.999695214873514
-0.000609477358998
10
2.000000000000000
0.999695214873514
0.999898394635237
-0.000203200405876
11 2.000000000000000
0.999898394635237
0.999966130397968
-0.000067738056915
12
2.000000000000000
0.999966130397968
0.999988710005193
-0.000022579862149
13
2.000000000000000
0.999988710005193
0.999996236654235
-0.000007526677367
14
2.000000000000000
0.999996236654235
0.999998745549838
-0.000002508898750
Wah Engineering College
Page 11
Lab manuals
15
2.000000000000000
Numerical Analysis
0.999998745549838
0.999999581849771
Date:
-0.000000836300283
Conclusions
From this we conclude that
It is faster than bisection method.
Experiment # 06
Title
Implementation of Newton Raphson Method
Software
MATLAB
Theory
The steps of the Newton-Raphson method to find the root of an equation
Wah Engineering College
Page 12
f x 0
are
Lab manuals
1. Evaluate
Numerical Analysis
f x
Date:
symbolically
xi
2. Use an initial guess of the root,
f xi
xi 1 = xi
f xi
xi 1
, to estimate the new value of the root,
, as
a
3. Find the absolute relative approximate error
x xi
a = i 1
100
xi 1
as
4. Compare the absolute relative approximate error with the pre-specified relative error tolerance, . If
s
> , then go to Step 2, else stop the algorithm. Also, check if the number of iterations has exceeded the
maximum number of iterations allowed. If so, one needs to terminate the algorithm and notify the user.
Code
clc
clear all
N=10;
fprintf('initial guess=')
x0=1
f=@(x)1+x*tan(x);
fd=@(x)tan(x)+x*sec(x)*sec(x);
fprintf('n
c
f(c)\n')
for n=0:N
c=x0-f(x0)/fd(x0);
fprintf('%3d
%8.15f
%8.15f\n',n,c,f(c))
x0=c;
end
Result
initial guess=
x0 =
Wah Engineering College
Page 13
Lab manuals
n
Numerical Analysis
c
Date:
f(c)
0.486765919321041
1.257616530515499
-0.604585157132899
1.417701383255673
0.290394556555804
1.086782243712915
-1.476238939808956
16.565537805943002
-1.382193568080384
8.241494831553638
-1.197242781124118
4.054526184000185
-0.845944777816717
1.955127027360944
-0.205635654936590
1.042892315640791
2.258861634199093
-1.747710286541552
2.657445406325536
-0.397533477242050
10
2.796127661832952
-0.006319842260502
Conclusions
From this we conclude that
It is the most commonly used method.
The convergence of this method is faster than both Bisection and Regula falsi method.
It can be extended to system of nonlinear equations.
Probability of success is high, only if good starting value is assumed.
Experiment # 07
Title
Implementation of Secant method
Software
MATLAB
Wah Engineering College
Page 14
Lab manuals
Numerical Analysis
Date:
Theory
f ( x) 0
The Newton-Raphson method of solving a nonlinear equation
xi 1 = xi
is given by the iterative formula
f ( xi )
f ( xi )
.. (1)
One of the drawbacks of the Newton-Raphson method is that you have to evaluate the derivative of the function.
With availability of symbolic manipulators such as Maple, MathCAD, MATHEMATICA and MATLAB, this process
has become more convenient. However, it still can be a laborious process, and even intractable if the function is
f (x)
derived as part of a numerical scheme. To overcome these drawbacks, the derivative of the function,
is
approximated as
f ( xi )
f ( xi ) f ( xi 1 )
xi xi 1
. (2)
Substituting Equation (2) in Equation (1) gives
xi 1 xi
f ( xi )( xi xi 1 )
f ( xi ) f ( xi 1 )
. (3)
The above equation is called the secant method. This method now requires two initial guesses, but unlike the
bisection method, the two initial guesses do not need to bracket the root of the equation. The secant method is an
open method and may or may not converge. However, when secant method converges, it will typically converge
faster than the bisection method. However, since the derivative is approximated as given by Equation (2), it typically
converges slower than the Newton-Raphson method.
Code
clc
clear all
N=15;
x0=0.5;
x1=1.5;
fx0=cos(x0)-2*x0+3;
fx1=cos(x1)-2*x1+3;
format short
fprintf('n
x0
x1
x2
f(x2\n')
for n=1:N
x2=x0-(x1-x0)/(fx1-fx0)*fx0;
fx2=cos(x2)-2*x2+3;
fprintf('%3d %8.15f %8.15f %8.15f %8.15f\n',n,x0,x1,x2,fx2)
Wah Engineering College
Page 15
Lab manuals
Numerical Analysis
Date:
x0=x1;
x1=x2;
end
Result
n
x0
x1
x2
f(x2)
1 0.500000000000000 1.500000000000000 1.525201673975402 -0.004824491066630
2 1.500000000000000 1.525201673975402 1.525836798346565 -0.006729213273436
3 1.525201673975402 1.525836798346565 1.525852804543901 -0.006777215696788
4 1.525836798346565 1.525852804543901 1.525853207926867 -0.006778425438360
5 1.525852804543901 1.525853207926867 1.525853218092794 -0.006778455925873
6 1.525853207926867 1.525853218092794 1.525853218348992 -0.006778456694209
7 1.525853218092794 1.525853218348992 1.525853218355449 -0.006778456713573
8 1.525853218348992 1.525853218355449 1.525853218355611 -0.006778456714061
9 1.525853218355449 1.525853218355611 1.525853218355615 -0.006778456714073
10 1.525853218355611 1.525853218355615 1.525853218355615 -0.006778456714073
11 1.525853218355615 1.525853218355615 1.525853218355615 -0.006778456714073
12 1.525853218355615 1.525853218355615 1.525853218355615 -0.006778456714073
13 1.525853218355615 1.525853218355615 1.525853218355615 -0.006778456714073
14 1.525853218355615 1.525853218355615 1.525853218355615 -0.006778456714073
15 1.525853218355615 1.525853218355615 1.525853218355615 -0.006778456714073
Conclusions
From this we conclude that
It does not require evaluation of derivatives.
It is easier to implement than Newtons Raphson method.
It may be used to find complex roots.
Convergence rate is not faster than Newtons Raphson method.
Wah Engineering College
Page 16
Lab manuals
Numerical Analysis
Experiment # 08
Title
Implementation of Fixed Point Iteration Method
Software
MATLAB
Wah Engineering College
Page 17
Date:
Lab manuals
Numerical Analysis
Date:
Theory
In numerical analysis, fixed-point iteration is a method of computing fixed points of iterated functions. More
specifically, given a function defined on the real numbers with real values and given a point
in the domain of ,
the fixed point iteration is
Which gives rise to the sequence
Which is hoped to converge to a point . If
of , i.e.,
More generally, the function
is continuous, then one can prove that the obtained
.
can be defined on any metric space with values in that same space.
Code
clc
clear all
N=15;
x=1;
f=@(x)sin(x)-5*x+2;
fprintf('n
x
c
f(c)\n')
for n=1:N;
c=(sin(x)+2)/5;
fc=sin(c)-5*c+2;
fprintf('%3d %8.15f %8.15f %8.15f\n', n,x,c,f(c))
x=c;
end
Result
n
f(c)
1 1.000000000000000 0.568294196961579 -0.303275837719714
2 0.568294196961579 0.507639029417637 -0.052079782993259
3 0.507639029417637 0.497223072818985 -0.009128653730139
4 0.497223072818985 0.495397342072957 -0.001605448010901
5 0.495397342072957 0.495076252470777 -0.000282512675688
6 0.495076252470777 0.495019749935639 -0.000049719174508
7 0.495019749935639 0.495009806100738 -0.000008750193762
8 0.495009806100738 0.495008056061985 -0.000001539971918
9 0.495008056061985 0.495007748067602 -0.000000271024264
10 0.495007748067602 0.495007693862749 -0.000000047698375
Wah Engineering College
Page 18
is a fixed point
Lab manuals
Numerical Analysis
Date:
11 0.495007693862749 0.495007684323074 -0.000000008394581
12 0.495007684323074 0.495007682644157 -0.000000001477388
13 0.495007682644157 0.495007682348680 -0.000000000260010
14 0.495007682348680 0.495007682296678 -0.000000000045760
15 0.495007682296678 0.495007682287526 -0.000000000008054
Conclusions
From this we conclude that
It is applicable to a wide range of numerical problems.
It is more concrete and definite than other methods.
Its order of convergence beginning at one and increases method get more and more accurate.
It has the potential to find the roots quickly and accurately.
Experiment # 09
Title
Implementation of Gauss-Jacobi Method for Solution of System of Linear Equations
Software
MATLAB
Theory
Given a general set of
equations and
unknowns, we have
a11 x1 a12 x 2 a13 x3 ... a1n x n c1
Wah Engineering College
Page 19
Lab manuals
Numerical Analysis
Date:
a 21 x1 a 22 x 2 a 23 x3 ... a 2 n xn c 2
.
a n1 x1 a n 2 x 2 a n 3 x3 ... a nn x n c n
If the diagonal elements are non-zero, each equation is rewritten for the corresponding unknown, that is, the first
equation is rewritten with
so on as follows
x1
on the left hand side, the second equation is rewritten with
x1
c1 a12 x 2 a13 x3 a1n x n
a11
x2
c 2 a 21 x1 a 23 x3 a 2 n x n
a 22
x n 1
xn
c n 1 a n 1,1 x1 a n 1, 2 x 2 a n 1,n 2 x n 2 a n 1,n x n
a n 1,n 1
c n a n1 x1 a n 2 x 2 a n ,n 1 x n 1
a nn
These equations can be rewritten in a summation form as
n
c1 a1 j x j
x1
j 1
j 1
a11
n
c2 a2 j x j
x2
j 1
j2
a 22
.
.
Wah Engineering College
Page 20
x2
on the left hand side and
Lab manuals
Numerical Analysis
Date:
.
c n 1
x n 1
j 1
j n 1
n 1, j
xj
a n 1,n 1
n
c n a nj x j
j 1
jn
xn
a nn
i
Hence for any row ,
n
ci aij x j
j 1
j i
xi
aii
, i 1,2, , n.
xi
xi
Now to find s, one assumes an initial guess for the s and then uses the rewritten equations to calculate the new
estimates simultaneously. At the end of each iteration, one calculates the absolute relative approximate error for
xi
each
as
x inew x iold
x inew
100
xi
xinew
where
is the recently obtained value of
xi
xiold
, and
is the previous value of
When the absolute relative approximate error for each xi is less than the pre-specified tolerance, the iterations are
stopped.
Code
clc
clear all
N=10;
x0=0;
y0=0;
z0=0;
Wah Engineering College
Page 21
Lab manuals
Numerical Analysis
fprintf(' n
x
y
z\n');
for i=1:N
x=95/83-11/83*y0+4/83*z0;
y=104/52-7/52*x0-13/52*z0;
z=71/29-3/29*x0-8/29*y0;
x0=x;
y0=y;
z0=z;
fprintf('%3d
end
%8.15f
%8.15f
Result
n
x
1
1.144578313253012
2
0.997507270461155
3
1.066749436787128
4
1.052841339383336
5
1.058747659004925
6
1.057496281055922
7
1.058002319367357
8
1.057890257136219
9
1.057933711091246
10
1.057923713271082
%8.5e \n',i,x,y,z)
y
2.000000000000000
1.233853184621776
1.421183407369531
1.355221073661050
1.371803388929915
1.366099488805504
1.367564301124371
1.367070444937255
1.367199638974542
1.367156832195136
z
2.44828e+000
1.77815e+000
2.00471e+000
1.94587e+000
1.96551e+000
1.96032e+000
1.96202e+000
1.96157e+000
1.96172e+000
1.96168e+000
Conclusions
From this we conclude that
It convergence rate is slower than Gauss Seidal method.
It is easy to implement.
Experiment # 10
Title
Implementation of Gauss- Seidel Method for Solution of System of linear Equations
Wah Engineering College
Page 22
Date:
Lab manuals
Numerical Analysis
Date:
Software
MATLAB
Theory
Given a general set of
equations and
unknowns, we have
a11 x1 a12 x 2 a13 x3 ... a1n x n c1
a 21 x1 a 22 x 2 a 23 x3 ... a 2 n x n c 2
.
a n1 x1 a n 2 x 2 a n 3 x3 ... a nn xn c n
If the diagonal elements are non-zero, each equation is rewritten for the corresponding unknown, that is, the first
equation is rewritten with
so on as follows
x1
on the left hand side, the second equation is rewritten with
x1
c1 a12 x 2 a13 x3 a1n x n
a11
x2
c 2 a 21 x1 a 23 x3 a 2 n x n
a 22
x n 1
xn
c n 1 a n 1,1 x1 a n 1, 2 x 2 a n 1,n 2 x n 2 a n 1,n x n
a n 1,n 1
c n a n1 x1 a n 2 x 2 a n ,n 1 x n 1
a nn
These equations can be rewritten in a summation form as
Wah Engineering College
Page 23
x2
on the left hand side and
Lab manuals
Numerical Analysis
Date:
c1 a1 j x j
j 1
j 1
x1
a11
n
c2 a2 j x j
j 1
j2
x2
a 22
.
.
.
c n 1
x n 1
j 1
j n 1
n 1, j
xj
a n 1,n 1
n
c n a nj x j
j 1
jn
xn
a nn
i
Hence for any row ,
n
ci aij x j
j 1
j i
xi
aii
, i 1,2, , n.
xi
Now to find
xi
s, one assumes an initial guess for the
s and then uses the rewritten equations to calculate the new
xi
estimates. Remember, one always uses the most recent estimates to calculate the next estimates,
xi
each iteration, one calculates the absolute relative approximate error for each
Wah Engineering College
Page 24
as
. At the end of
Lab manuals
a
Numerical Analysis
x inew x iold
x inew
100
xi
xinew
where
Date:
is the recently obtained value of
xi
xiold
, and
is the previous value of
When the absolute relative approximate error for each xi is less than the pre-specified tolerance, the iterations are
stopped.
Code
83x+11y-4z=95
7x+52y+13z=104
3x+8y+29z=71
clc
clear all
N=15;
x0=0;
y0=0;
z0=0;
fprintf('apprpxi.
x
y
z\n')
for i=1:N
x(i)=1/83*(95-11*y0-4*z0);
y(i)=1/52*(104-7*x(i)-13*z0);
z(i)=1/29*(71-3*x(i)-8*y(i));
fprintf('%3d %8.15f %8.15f %8.15f\n',i,x(i),y(i),z(i))
x0=x(i);
y0=y(i);
z0=z(i);
end
Result
apprpxi.
1.144578313253012
1.845922150139018
1.820651305487201
0.812195796705084
1.435503124071746
1.968254745424510
0.859475260885696
1.392237720832336
1.975299050023594
0.864869745430722
1.389750464070735
1.975427139694550
0.865193208872816
1.389674898497330
1.975414523634584
0.865203831590253
1.389676622531128
1.975412949137249
Wah Engineering College
Page 25
Lab manuals
Numerical Analysis
0.865203678983236
1.389677036698714
1.975412850671054
0.865203628838915
1.389677068065460
1.975412847205503
0.865203624848891
1.389677069468966
1.975412847231089
10
0.865203624661651
1.389677069487775
1.975412847245271
11 0.865203624658475
1.389677069484657
1.975412847246459
12
0.865203624658830
1.389677069484312
1.975412847246517
13
0.865203624658874
1.389677069484292
1.975412847246519
14
0.865203624658876
1.389677069484291
1.975412847246519
15
0.865203624658876
1.389677069484291
1.975412847246519
Date:
Conclusions
From this we conclude that
It is easy to implement compare to Gauss Jacobi method.
It is much closer to the solution after one iteration compared to Gauss Jacobi method.
It will require less iteration to reach to the convergence.
Wah Engineering College
Page 26
Lab manuals
Numerical Analysis
Date:
Experiment # 11
Title
Implementation of Interpolation by Newton Forward Difference Formula
Software
MATLAB
Theory
Newton's forward difference formula is a finite difference identity giving an interpolated value between tabulated
points
in terms of the first value
and the powers of the forward difference . For
, the formula states
(1)
xi
fi
x0
f0
x1
f1
fi
2fi
3f0 = 2f
2
1- f0
2f1 =
f2 - f1
f2
2f2 =
f3 - f2
f3
f5
Wah Engineering College
Page 27
5f0 = 4
f1- 4f0
4f1 = 3f
3
2 - f1
3f2 = 2f
2
3 - f2
2f3 =
f4 - f3
f4
f4 = f5
- f4
x5
4f0 = 3f
3
1- f0
3f1 = 2f
2
2 - f1
f3 = f4
- f3
x4
5fi
2f0 =
f1- f0
f2 = f3
- f2
x3
4fi
f0 = f1
- f0
f1 = f2
- f1
x2
3fi
Lab manuals
Numerical Analysis
Code
clc
tic
n = 4;
x = [8, 10, 12, 14, 16];
fx = [1000, 1900, 3250, 5400, 8950];
x0=8;
xp=9;
h=2;
p=(xp-x0)/h
F = zeros(n+1, n+1);
F(:, 1) = fx;
for i=1:n
for j=1:i
F(i+1,j+1) = (F(i+1,j)-F(i,j)) ;
end
if i==j
gx=F(i,j)+p*F(i,j)+0.5*p*(p-1)*F(i,j)
break;
end
end
disp('Matrix diagonal entries represents the FORWARD Difference table')
a = diag(F)
b=F
Y=gx
Result
p=
0.5000
gx =
1375
Matrix diagonal entries represents the FORWARD Difference table
a=
1000
900
Wah Engineering College
Page 28
Date:
Lab manuals
Numerical Analysis
0
0
0
b=
1000
1900
3250
5400
8950
0
900
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Y=
1375
Conclusions
Wah Engineering College
Page 29
Date:
Lab manuals
Numerical Analysis
Date:
Experiment # 12
Title
Implementation of Lagrange Interpolation Formula
Software
MATLAB
Theory
n
Polynomial interpolation involves finding a polynomial of order
that
n 1
passes through the
data points. One of the methods used to find this
polynomial is called the Lagrangian method of interpolation. Other
methods include Newtons divided difference polynomial method and the
direct method. We discuss the Lagrangian method in this chapter.
The Lagrangian interpolating polynomial is given by
n
f n ( x) Li ( x ) f ( xi )
i 0
Where
f n (x )
in
stands for the
n th
y f (x)
order polynomial that approximates the function
given at
x0 , y 0 , x1 , y1 ,......, xn1 , y n1 , xn , y n
points as
, and
n
Li ( x)
j 0
j i
x xj
xi x j
Li (x )
is a weighting function that includes a product of
Code
clc
clear all
X=[0 1 2 4];
Y = [1 1 2 5];
Wah Engineering College
Page 30
n 1
ji
terms with terms of
omitted?
n 1
data
Lab manuals
Numerical Analysis
x=3;
n = length(X);
if n ~= length(Y)
error('X and Y must be the same length.');
end
y=0;
% initialise sum
for i = 1:n
% loop over sum index
for j = 1:i-1
L = (x-X(j))/(X(i)-X(j)) % multiply next factor
end
for k=1+i:n
L =(x-X(k))/(X(i)-X(k)) % multiply next factor
end
y =y+L*Y(i)
% add next term
end
y
polyfit(X,Y,3)
Result
L=
-2
L=
-0.5000
L=
Wah Engineering College
Page 31
Date:
Lab manuals
Numerical Analysis
0.2500
y=
0.2500
L=
L=
-1
L=
0.3333
y=
Wah Engineering College
Page 32
Date:
Lab manuals
Numerical Analysis
0.5833
L=
1.5000
L=
L=
0.5000
y=
1.5833
L=
0.7500
Wah Engineering College
Page 33
Date:
Lab manuals
Numerical Analysis
L=
0.6667
L=
0.5000
y=
4.0833
y=
4.0833
ans =
-0.0833
0.7500 -0.6667
1.0000
Conclusions
Wah Engineering College
Page 34
Date:
Lab manuals
Numerical Analysis
From this we conclude that
Lagrange interpolation are used to find polynomial expression.
Experiment # 13
Title
Implementation of Newtons Divided Difference Formula
Wah Engineering College
Page 35
Date:
Lab manuals
Numerical Analysis
Date:
Software
MATLAB
Theory
For Newtons divided difference method let us revisit the quadratic polynomial interpolation formula
f 2 ( x) b0 b1 ( x x0 ) b2 ( x x0 )( x x1 )
where
b0 f ( x0 )
b1
f ( x1 ) f ( x0 )
x1 x0
f ( x 2 ) f ( x1 ) f ( x1 ) f ( x0 )
x 2 x1
x1 x 0
b2
x 2 x0
b0 , b1 ,
b0 , b1 ,
b2
Note that
and
are finite divided differences.
and
differences, respectively. We denote the first divided difference by
f [ x0 ] f ( x0 )
the second divided difference by
f [ x1 , x0 ]
f ( x1 ) f ( x0 )
x1 x0
and the third divided difference by
f [ x 2 , x1 , x0 ]
f [ x 2 , x1 ] f [ x1 , x0 ]
x2 x0
f ( x 2 ) f ( x1 ) f ( x1 ) f ( x 0 )
x 2 x1
x1 x0
x2 x0
Wah Engineering College
Page 36
b2
are the first, second, and third finite divided
Lab manuals
Numerical Analysis
f [ x0 ], f [ x1 , x0 ],
where
brackets.
Date:
f [ x 2 , x1 , x0 ]
and
are called bracketed functions of their variables enclosed in square
Rewriting,
f 2 ( x) f [ x0 ] f [ x1 , x0 ]( x x0 ) f [ x 2 , x1 , x0 ]( x x0 )( x x1 )
This leads us to writing the general form of the Newtons divided difference polynomial for
x0 , y0 , x1 , y1 ,......, xn1 , y n1 , xn , y n
, as
f n ( x) b0 b1 ( x x 0 ) .... bn ( x x0 )( x x1 )...( x x n 1 )
where
b0 f [ x0 ]
b1 f [ x1 , x0 ]
b2 f [ x 2 , x1 , x0 ]
bn 1 f [ x n 1 , x n 2 ,...., x0 ]
bn f [ x n , x n 1 ,...., x0 ]
where the definition of the
m th
divided difference is
bm f [ x m ,........, x0 ]
f [ xm ,........, x1 ] f [ xm 1 ,........, x0 ]
xm x0
From the above definition, it can be seen that the divided differences are calculated recursively.
Wah Engineering College
Page 37
n 1
data points,
Lab manuals
Numerical Analysis
Date:
Code
clear all
clc
n = 4;
xa = [8 11 14 17 19];
fx = [1000, 1900, 3250, 5400, 8950];
F = zeros(n+1, n+1);
F(:, 1) = fx
X= zeros(5);
X(:,1)=xa
x=9;
x0=xa(1);
x1=xa(2);
x2=xa(3);
x3=xa(4);
x4=xa(5);
% Compute the Newton divided differences.
for i=1:n
h(1)=xa(i+1)-xa(1);
for j=1:i
F(i+1,j+1) = (F(i+1,j)-F(i,j))/(h(1));
if i==j
gx=F(i,j)+(x-8)*F(i,j)+(x-8)*(x-10)*F(i,j)+(x-8)*(x-10)*(x-12)*F(i,j)+(x-8)*(x-10)*(x-12)*(x-14)*F(i,j);
break;
end
end
end
a = diag(F)
b=gx
Result
F=
1000
1900
3250
5400
Wah Engineering College
Page 38
Lab manuals
8950
Numerical Analysis
X=
11
14
17
19
a=
1.0e+003 *
1.0000
0.3000
-0.0125
0.0016
-0.0001
b=
-17.1639
Wah Engineering College
Page 39
Date:
Lab manuals
Numerical Analysis
Conclusions
Experiment # 14
Title
Implementation of Simpsons Rule
Wah Engineering College
Page 40
Date:
Lab manuals
Numerical Analysis
Date:
Software
MATLAB
Theory
Just like in multiple-segment trapezoidal rule, one can subdivide the interval
Simpsons 1/3 rule repeatedly over every two segments. Note that
equal segments, so that the segment width is given by
h
ba
n
a, b
into
needs to be even. Divide interval
Now
b
xn
x0
f ( x)dx f ( x)dx
where
x0 a
xn b
b
f ( x)dx
x2
f ( x)dx
x0
x4
f ( x)dx ......
x2
xn 2
f ( x)dx
xn 4
xn
f ( x)dx
xn 2
Apply Simpsons 1/3rd Rule over each interval,
b
f ( x )dx ( x
f ( x0 ) 4 f ( x1 ) f ( x2 )
f ( x 2 ) 4 f ( x3 ) f ( x4 )
( x4 x2 )
...
6
6
x0 )
f ( x n 4 ) 4 f ( x n 3 ) f ( x n 2 )
f ( x n 2 ) 4 f ( x n 1 ) f ( x n )
( x n xn 2 )
6
6
( xn2 xn4 )
Since
x i x i 2 2h
i 2, 4, ..., n
Then
Wah Engineering College
Page 41
segments and apply
a, b
into
Lab manuals
Numerical Analysis
Date:
f ( x0 ) 4 f ( x1 ) f ( x 2 )
f ( x 2 ) 4 f ( x3 ) f ( x 4 )
2h
...
6
6
f ( x )dx 2h
a
f ( x n 4 ) 4 f ( x n 3 ) f ( x n 2 )
f ( x n 2 ) 4 f ( x n 1 ) f ( x n )
2h
6
6
2h
h
f ( x0 ) 4 f ( x1 ) f ( x3 ) ... f ( xn1 ) 2 f ( x2 ) f ( x4 ) ... f ( xn2 ) f ( xn )
3
n 1
n2
h
f ( x 0 ) 4 f ( x i ) 2 f ( xi ) f ( x n )
3
i 1
i2
i odd
i even
n 1
n 2
ba
f ( x )dx
f ( x 0 ) 4 f ( x i ) 2 f ( xi ) f ( x n )
3n
i 1
i 2
i odd
i even
Simpson 1/3 Code
clc
clear all
f=@(x)x^3;
a=1;
b=5;
n=b-a;
h=(b-a)/n;
p=0;
for i=a:b
p=p+1;
x(p)=i;
y(p)=i^3;
end
x
y
gx=(h/3)*((y(1)+y(5))+2*(y(3))+4*((y(2)+y(4))))
Result
x=
y=
Wah Engineering College
Page 42
Lab manuals
1
27
Numerical Analysis
64 125
gx =
156
Simpson 3/8 Code
clc
clear all
f=@(x)x^3;
a=1;
b=5;
n=6;
h=(b-a)/n;
p=0;
for i=a:h:b
p=p+1;
x(p)=i;
y(p)=i^3;
end
x
y
gx=((3*h/8)*((y(1)+y(7))+2*(y(4))+3*(y(2)+y(3)+y(5)+y(6) )))
Result
x=
1.0000
1.6667
2.3333
3.0000
3.6667
4.3333
5.0000
y=
1.0000
4.6296 12.7037 27.0000 49.2963 81.3704 125.0000
Wah Engineering College
Page 43
Date:
Lab manuals
Numerical Analysis
Date:
gx =
156.0000
Conclusions
Experiment # 15
Title
Implementation of Solution of First Order Differential Equations by Runge-Kutta Method
Software
MATLAB
Wah Engineering College
Page 44
Lab manuals
Numerical Analysis
Date:
Theory
Runge-Kutta 2nd order method
Eulers method is given by
yi 1 yi f xi , yi h
where
x0 0
y 0 y ( x0 )
h xi 1 xi
To understand the Runge-Kutta 2nd order method, we need to derive Eulers method from the Taylor series.
y i 1 y i
dy
dx
xi 1 xi 1 d
2! dx
xi , yi
y i f ( xi , y i ) xi 1 xi
3! dx
xi 1 xi 2 1 d
2
xi , yi
xi 1 xi 3 ...
xi , yi
1
1
2
3
f ' ( xi , y i ) xi 1 xi f ' ' ( xi , y i ) xi 1 xi ...
2!
3!
As you can see the first two terms of the Taylor series
yi 1 yi f xi , y i h
are Eulers method and hence can be considered to be the Runge-Kutta 1st order method.
The true error in the approximation is given by
Et
f xi , yi 2 f xi , yi 3
h
h ...
2!
3!
So what would a 2nd order method formula look like. It would include one more term of the Taylor series as
follows.
y i 1 y i f xi , y i h
1
f xi , y i h 2
2!
Wah Engineering College
Page 45
Lab manuals
Numerical Analysis
However, we already see the difficulty of having to find
was write the 2nd order method as
f x, y
Date:
in the above method. What Runge and Kutta did
y i 1 y i a1 k1 a 2 k 2 h
where
k1 f x i , y i
k 2 f xi p1 h, y i q11k1 h
This form allows one to take advantage of the 2nd order method without having to calculate
f x, y
Runge-Kutta 4th order method
The formula described in this chapter was developed by Runge. This formula is same as Simpsons 1/3 rule, if
f x, y
x
were only a function of . There are other versions of the 4th order method just like there are several
versions of the second order methods. The formula developed by Kutta is
yi 1 yi
1
k1 3k 2 3k 3 k 4 h
8
where
k1 f x i , y i
1
1
k 2 f xi h, y i hk1
3
3
2
1
k 3 f xi h, y i hk1 hk 2
3
3
k 4 f xi h, y i hk1 hk 2 hk 3
Wah Engineering College
Page 46
Lab manuals
Numerical Analysis
This formula is the same as the Simpsons 3/8 rule, if
f x, y
Date:
x
is only a function of .
Code
clc
clearall
closeall
fx='y*x^2-1.2*y' ;
f=inline(fx) ;
% x0, initial condition
x0=0 ;
% y0, corresponding value of y at x0
y0=1 ;
% xf, x location at where you wish to see the solution to the ODE
xf=2 ;
a2=0.5 ;
% n, number of steps to take
n=5 ;
% Calculates constants used in the method
h=(xf-x0)/n ;
a1=1-a2 ;
p1=1/2/a2 ;
q11=p1 ;
xa(1)=x0 ;
ya(1)=y0 ;
fori=1:n
% Adding Step Size
xa(i+1)=xa(i)+h
% Calculating k1 and k2
k1 = f(xa(i),ya(i))
k2 = f(xa(i)+p1*h,ya(i)+q11*k1*h)
% Using 2nd Order Runge-Kutta formula
ya(i+1)=ya(i)+(a1*k1+a2*k2)*h
end
% The following finds what is called the 'Exact' solution
xspan = [x0 xf]
[x,y]=ode45(f,xspan,y0)
[x' y']
plot(x, y,'r*')
holdon
plot(ya)
Result
xa =
Wah Engineering College
Page 47
Lab manuals
0
Numerical Analysis
0.4000
k1 =
-1.2000
k2 =
-0.5408
ya =
1.0000
0.6518
xa =
0
0.4000
0.8000
k1 =
-0.6779
k2 =
-0.2132
ya =
1.0000
0.6518
0.4736
xa =
0
0.4000
0.8000
1.2000
k1 =
-0.2652
k2 =
0.0882
ya =
1.0000
0.6518
0.4736
0.4382
xa =
0
0.4000
0.8000
1.2000
Wah Engineering College
1.600
Page 48
Date:
Lab manuals
Numerical Analysis
Conclusion
Wah Engineering College
Page 49
Date: