Numerical Technique Lab Manual (VERSI Ces512)
Numerical Technique Lab Manual (VERSI Ces512)
Numerical Technique Lab Manual (VERSI Ces512)
7
8 Solution of Differential Equations.
9 Solution of Difference Equations using Euler Method.
10 Solution of differential equation using 4th order Runge- Kutta method.
LAB 1
2. FIGURE WINDOW:
The output of all the commands written on the command window or
executed by writing in M-file, whose output is a graph , appears on
the window. The user can create as many figure windows as the
system memory allows. A figure window is showing sine curve is
shown in figure.
3. EDITOR WINDOW:
This is where we can write, create, edit and save programs in a file.
The file is known as M-file. To select editor window, go to file and
then select M-file. The programmes written on the file are first
saved and then run to get the results. To save and run the
programme, go to debug and select save and run. The result appear
on the command window. The figure appear on the figure window. A
editor window is shown as in figure:
2.2 LANGUAGES:
MATLAB is a high-level language that includes matrix-based data
structures, its own internal data types, an extensive catalog of functions
and scripts, the ability to import and export to many types of data files,
object-oriented programming capabilities, and interfaces to external
technologies such as COM, Java, program written in C and Fortran, and
serial port devices.
2.3 INPUT-OUTPUT:
MATLAB takes input from the screen and rushes output to the screen
i.e. it supports interactive computation. It can read input files and write
output files.
2.3.1 Data Type: There is no need to declare a data to be real or
complex. When a real number is entered as a variable, MATLAB
automatically sets the variable to be real. Fundamental data type is
the array.
2.3.2 Dimension: Dimension state is not required in the MATLAB. It is
automatic.
2.3.3 Case Sensitivity: It differentiates between lower and upper cases.
It is case sensitive.
2.3.4 Output Display: The output of every command appears on the
screen unless MATLAB is directed otherwise. A semi-colon(;)
suppress the output when used at the end of a command except for
the graphics.
2.4 GETTING STARTED
3. ACCESSORIES
3.1 TOOLBOXES: MATLAB features a family of add-on-applicationspecific solutions called toolboxes. Very important to most users of
MATLAB, toolboxes allow you to learn and apply specialized
technology. Toolboxes are comprehensive collections of MATLAB
environment to solve particular classes of problems. Areas in which
toolboxes are available include signal processing, control systems,
neural networks, fuzzy logic, wavelets, simulation, and many others.
LABORATORY NO. 2
OBJECTIVE: STUDY OF BASIC MATRIX OPERATIONS
APPARATUS/SOFTWARE
SPECIFICATION
QUANTITY
GIVEN NUMERICAL:
A= 3 2 1
0 3 4
-1 1 -1
B= 1 3 0
2 6 4
-1 0 2
Where A and B are two 3 X 3 matrix
COMMANDS:
>> A= [3 2 1; 0 3 4; -1 1 -1]
>> B= [1 3 0; 2 6 4; -1 0 2]
Sum:
Subtraction:
Multiplication:
Division:
Inverse of A:
A+ B
A B
A* B
A/B or A\B
inv(A)
RESULTS:
>> A + B
ans:
>> A B
ans:
>> A * B
ans:
>> A/B
ans:
>> A\B
ans:
>> inv(A)
ans:
% inverse of A
ans:
>>a= rand(3)
ans:
>> a= ones(3)
ans:
>> b= 2*ones(3)
ans:
>> a+2
ans:
>> a(2,2)
ans:
>>a(2:3, :)
ans:
>> a(2:3,1:2)
ans:
>>a(:,2)
ans:
>>a(:,1:2)=[]
ans:
>>eye(3)
ans:
>> diag(a)
ans:
>> b = a
ans:
CONCLUSION:
We have studied various commands to solve the basic matrix operations.
EXPERIMENT NO. 9
OBJECTIVE:
SOLUTION
BISECTION METHOD
OF
APPARATUS/SOFTWARE
SPECIFICATION
QUANTITY
Theory
The steps to apply the bisection method to find the root of the equation
1. Choose
and
other words,
xu
f (x)
xu
as
xm =
xm
, of the equation
and
f ( x) 0
xu
f ( x) 0
f ( x ) f ( xu ) 0
are
, or in
x xu
2
f ( x ) f ( x m ) 0
xu x m
b) If
and
xm
; then
f ( x ) f ( xm ) 0
xu xu
f ( x ) f ( xm ) 0
c) If
; then the root is
4. Find the new estimate of the root
xm =
xm
and
xu
; then
x x m
x xu
2
xmnew - xmold
a =
100
xmnew
xm
x x
and
and
Where
xmnew
xmold
a s
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;
else
x1 = xm;
fx1 = fm;
end
end
Result
x = fsolve(fun,x0)
Example of usage
Results
CONCLUSIONS
EXPERIMENT NO. 3
OBJECTIVE: TO SOLVE LINEAR EQUATION
______________________________________________________________________________
APPARATUS/SOFTWARE
SPECIFICATION
QUANTITY
THEORY:
LINEAR EQUATION:
Solving a linear algebraic equation is easy in MATLAB. It is, perhaps, also the
most used computation in science and engineering. We will solve a set of linear
algebraic equations given below:
5x= 3y-2z+10
8y+4z= 3x+20
2x+4y-9z=9
PROCEDURE:
STEP 1: Rearrange equations: Write each equation with all unknown quantities on
the left hand side and all known quantities on the right hand side. Thus, for the
equations given above, rearrange them such that all terms involving x, y, and z are
on the left hand side of the equal sign:
5x-3y+2z = 10
-3x+8y+4z = 20
2x+4y-9z = 9
STEP 2: Write the equations in matrix form: To write the equation in the matrix
form [A]{x}={b} where {x} is the vector of unknowns, we have to arrange the
unknowns in matrix A and the constants on the right hand side of the equations in
vector b.
In this particular example, the unknown vector is
[]
x
x= y
z
[ ]
532
3 8 4
2 49
[]
10
b= 20
9
Note that the columns of A are simply the coefficients of each unknown from all
the three equations.
STEP 3: Solve the matrix equation in MATLAB: Enter the matrix A and vector b,
and solve for vector x with x=A\b (note that the \ is different from the division /):
>> A= [5 -3
>> b=
[]
10
20
9
>> x= A\b
x=
.;
2; -3
4; 2
-9];
% enter matrix A
% enter column vector b
% solve for x
RESULT:
CONCLUSION:
EXPERIMENT NO. 6
APPARATUS/SOFTWARE
SPECIFICATION
QUANTITY
THEORY:
DIFFERENTIAL EQUATIONS:
A differential equation is a mathematical equation for an unknown function of
one or several variables that relates the values of the function itself and
its derivatives of various orders. Differential equations play a prominent role
in engineering, physics, economics, and other disciplines.
GIVEN NUMERICAL:
The function dsolve computes symbolic solutions to ordinary differential
equations. The equations are specified by symbolic expressions containing the
letter D to denote differentiation. The symbols D2, D3,.DN, correspond to the
second, third,.,Nth derivative, respectively. Thus, D2y is
d /dt
. The
dependent variables are those preceded by D and the default independent variable
is t. Note that names of symbolic variables should not contain D. The key issues in
this example are the order of the equation and the initial conditions.
To solve the ordinary differential equation, simply type:
y = dsolve(D3y=y, y(0)=1, Dy(0)=-1, D2y(0)=pi, x)
3
3
2
2
where D3y represents d y /dx and D2y(0) represents d y / dx at x = 0.
Examples
(i)
(ii)
(iii)
dy/dt = -ay
dy/dt = -ay and y(0) =1
d 2 y /dt 2 = - a2 y and y(0)= 1, dy/dt(/a)=0
(iv)
(v)
2
2
dy/dx = (xy - y )/ x
dy/dx = tan(y/x) +y/x
COMMANDS:
(i)
>> y=dsolve(Dy=-a*y)
(ii)
>> y=dsolve(Dy=-a*y, y(0)=1) %write ODE in inverted comma
followed by initial condition, separated by comma
(iii)
>> y=dsolve(D2y=-a^2*y, y(0)=1, Dy(pi/a)=0)
%write ODE in inverted comma followed
by initial condition, separated by comma
Note: In all the above cases, the independent variable is t by default.
(iv)
>> y = dsolve (Dy=(x*y y^2)/ x^2, x)
% define independent variable as x
(v)
>> y = dsolve (Dy=tan(y/x) + y/x), x)
RESULTS:
(i)
>> y=dsolve(Dy=-a*y)
y=
C1/exp(a*t)
(ii)
>>y=dsolve(Dy=-a*y, y(0)=1)
y=
exp(-a*t)
(iii)
>>y=dsolve(D2y=-a^2*y, y(0)=1, Dy(pi/a)=0)
y=
cos(a*t)
(iv)
>> y = dsolve (Dy=(x*y y^2)/ x^2, x)
y=
-x/(C12 - log(x))
(v)
>> y = dsolve (Dy=tan(y/x) + (y/x), x)
y=
asin(x*C1)*x
POST-EXPERIMENTAL QUESTIONS
Q3. What command is used to solve differential equations using
MATLAB? Explain with examples.
Q4. Write programs to solve :
2
2
i)
dy/dx = -( y x /2 xy )
ii)
dy/dx = (2y - x)/(2x -y)
2
iii) dy/dx = ytanx - y sec x
iv)
x 3
dy/dx = 3 e y - y
v)
d 2 y 8 dy
+15 y=0
dx
dt 2
EXPERIMENT NO. 7
OBJECTIVE: SOLUTION OF DIFFERENTIAL EQUATION USING
EULER METHOD
__________________________________________________________________
APPARATUS/ SOFTWARE REQUIRED:
SR.NO
APPARATUS/SOFTWARE
SPECIFICATION
THEORY:
Lets consider a first-order differential equation:
y ' ( t ) +ay ( t )=r with y ( 0 )= y 0
(1)
QUANTITY
r
y (t )=( y 0 )eat
a
r
a
(2)
(t) =
dy
dt
in the differential
And solve this difference equation step-by-step with increasing t by h each time
from t = 0.
y ( h )=( 1ah ) y ( 0 )+ hr=( 1ah ) y 0 + hr
y (2 h )=( 1ah ) y ( h ) +hr =(1ah)2 y 0 + ( 1ah ) hr+ hr
2
m=0
y 0=0.
y (t )=1eat
PROGRAM:
at
% Euler method to solve a 1st order differential equation of y (t )=1e %
a = 1; r = 1; y 0=0 ; tf = 2;
t = [0:0.01:tf];
yt = 1-exp(-a *t);
plot (t, yt, k), hold on
klasts = [8 4 2]; hs = tf. /klasts;
y(1) = y_0;
for int = 1: 3
klast = klasts (int) ; h = hs(int) ; y(1) = y_0;
for k = 1: klast
y(k+1) = (1-a*h)*y(k) + h* r ;
plot ([k-1 k]*h, [y(k) y(k+1)], b , k* h , y(k+1), r)
if k< 4, pause ; end
end
end
2.GIVEN NUMERICAL:
dy/dx = (y - x)/(y + x)
COMMANDS:
>> b = 3; a = 0;m = 4; x = 0; y = 1;
>> h = (b-a)/m;
>>x = a:h:b;
>> for j = 1:m;
RESULTS:
E=[x y]
E= 0
1.0000
0.7500
1.7500
1.5000
2.0500
2.2500
2.1662
3.0000
2.1520
CONCLUSION:
EXPERIMENT NO. 8
OBJECTIVE: SOLUTION OF DIFFERENTIAL EQUATIONS USING 4th
ORDER RUNGE-KUTTA METHOD
__________________________________________________________________
APPARATUS/ SOFTWARE REQUIRED:
SR.NO
APPARATUS/SOFTWARE
SPECIFICATION
QUANTITY
THEORY:
4
The fourth-order Runge -Kutta (RK4) method having a truncation error of O( h )
is one of the most widely used methods for solving differential equations. Its
algorithm is described below:
h
y k+1 = y k + ( f k 1 +2 f k 2 +2 f k3 + f k 4 )
6
(1)
Where
f k1 =f (t k , y k )
(2)
h
h
t k+ , yk+ f k 1
2
2
f k2 =f
h
h
t k + , y k +f k 2
2
2
f k3 =f
t k +h , y k + f k 3 h
f k 4 =f
(3)
(4)
(5)
PROGRAM:
function [t, y] = ode_RK4(f , tspan , y0, N, varargin)
% Runge- Kutta method to solve vector differential equation
% for tspan = [t0, tf] and with the initial value y0 and N time steps
if nargin <
4|
for k = 1: N
f1 = h* feval (f , t(k), y(k, :) , varargin{:}); f1 = f1(:);
f2 = h* feval (f , t(k) + h/2 , y(k, :) + f1/2 , varargin{:}); f2 = f2(:);
f3 = h* feval (f , t(k) + h/2 , y(k, :) + f2/2 , varargin{:}); f3 = f3(:);
f4 = h* feval (f , t(k) + h , y(k, :) + f3 , varargin{:}); f4 = f4(:);
y(k + 1 , :) = y(k, :) + (f1 + 2*(f2 +f3) + f4 )/6 ;
end
CONCLUSION:
EXPERIMENT NO. 10
OBJECTIVE:
APPARATUS/SOFTWARE
SPECIFICATION
QUANTITY
THEORY:
Curve fitting is a technique of finding an algebraic relationship that best (in least
squares sense) fits a given set of data. Unfortunately, there is no magical function
(in MATLAB or otherwise) that can give the relationship if we simply supply the
data. We have to have an idea of what kind of relationship might exist between the
input data and the output data. However, if we do not have the firm idea but have
data that we trust, MATLAB can help us in exploring the best possible fit.
MATLAB includes Basic Fitting in its Figure windows Tools menu that lets us fit
a polynomial curve (upto 10th order) to the data on the fly. It also gives us options
of displaying the residual at the data points and computing the norm of the
residuals. This can help in comparing different fits and then selecting the one that
best fits.
1. GIVEN NUMERICAL:
Fit a straight line y = a+ bx to the following data:
x: 1
y: 14 27
40
55
68
COMMANDS:
i)
>>x = [1 2 3 4 5];
>> y = [14
ii)
27
55
68];
>> polyfit(x,y,1)
RESULTS:
i)
ii)
13.6000 -0.0000
The constants a and b are 0 and 13.6 respectively.
Therefore, equation of the straight line is:
y=13.6x
2.2143 0.2429 1.4286
2
The constants of quadratic equation y = a+bx+c x
are a = 2.2143, b =
data is to be fitted.
You may also select show equation. On selection, the equation
appears on the figure window.
CONCLUSION:
PRE-EXPERIMENTAL QUESTIONS
Q1. What is Curve fitting ?
POST-EXPERIMENTAL QUESTIONS
Q3. By the method of least squares, find the straight line that fits the following
data:
x: 1
y:14 27
40
55
68
2
Q4. Fit a parabola y=a x +bx +c in least square sense to the data
x: 10 12
15
23
20
y:14 17
23
25
21