Matlab Manual
Matlab Manual
Matlab Manual
HANDOUT
On
Mission
2. Pre-Requisites
3. Course Objectives:
5. Program Outcomes:
a B c d e f g h i j k l
CO1 M H H H H
CO2 H H H H
CO3 H H H H
CO4 H H H H
CO5 H H H H
Sonet CDs & IIT CDs on some of the topics are available in the digital
library.
Course Objectives:
To familiarize with the environment of MATLAB and its components.
To introduce various data types and variables in MATLAB.
To distinguish various aspects of MATLAB scalars, arrays-vectors,
matrices.
To get acquainted with the built-in functions on scalars, arrays-vectors,
matrices.
To learn how to create and execute script and function files in MATLAB.
To learn about plot functions using MATLAB.
Course Outcomes:
Introduction:
Features of MATLAB
Scalars :
Any number that represents magnitude (quantity or measure) is known
as scalar.
This includes integers, complex number and floating point numbers
For example : 5, -7, 4 + 5i, -45.18 etc.
Characters :
The character constant is an alphanumeric symbol enclosed in a single
quote.
The character that is not represented in single quote is numeric or sign.
For example : ‘H’, ‘4’, ‘*’, ‘+’ etc.
Arrays :
An array is a list of homogenous data placed in rows and/or columns
form
The elements of an array can be numeric or character or strings, but not
mixed up.
An array is written using square brackets enclosing its elements
separated by commas or spaces
For Example : [4, 8, 3, -5], [cleve, moler] etc.
Strings :
Any two or more alphanumeric symbols enclosed in a single quote is
known as string data
For example : ‘INDIA’, ‘MATLAB’ etc.
A string is an array of characters, i.e., ‘INDIA’ is equivalent to [‘I’, ‘N’, ‘D’,
‘I’, ‘A’]
Cell arrays :
A cell array is a special type of arrays, where the elements can be of
different types
The elements of a cell array are enclosed using braces
For example : {‘south’, 544, ‘+’, ‘book’}
COMMAND HANDLING
SCALARS
In MATLAB a scalar is a variable with one row and one column.
Scalars are the simple variables that we use and manipulate in simple
algebraic equations.
Creating scalars
To create a scalar you simply introduce it on the left hand side of an equal
sign.
>> x = 1;
>> y = 2;
>> z = x + y;
Scalar operations
MATLAB supports the standard scalar operations like addition, subtraction,
multiplication and division.
>> u = 5;
>> v = 3;
VECTORS
In MATLAB a vector is a matrix with either one row or one column.
Creating vectors
Examples:
>> x = 0:10:100
x = [0 10 20 30 40 50 60 70 80 90 100]
>> a = 0:pi/50:2*pi
a = [0 pi/50 2*pi/50 . . . . . .2*pi]
>> u = 3:10
u = [3 4 5 6 7 8 9 10]
To create a column vector, append the transpose operator to the end of
the vector-creating expression
>> y = (1:5)'
y= 1
2
3
4
5
>> x = linspace(21,25,5)
x = 21 22 23 24 25
>> x(7) = -9
x = 21 22 23 24 25 0 -9
Vector Operations:
>> x = [1 2 3] >> y = [3 4 6]
The following MATLAB functions operate on vectors and return a scalar value.
MATRICES
Creating Matrices
» eye(3,4) 1 0 0 0
0 1 0 0
0 0 1 0
zeros matrix of zeros » zeros(2) 0 0
0 0
» zeros(2,3) 0 00
0 00
Ones matrix of ones » ones(2) 1 1
1 1
» ones(2,3) 1 1 1
1 1 1
diag extract diagonal of a x= 1 3 4
matrix or create 4 5 7
diagonal matrices 5 9 0 1
>> diag(x) 5
0
triu upper triangular part of >> triu(x) 1 3 4
a matrix 0 5 7
0 0 0
tril lower triangular part of >> tril(x) 1 0 0
a matrix 4 5 0
5 9 0
rand randomly generated >> rand(3) 0.9501 0.4860
matrix 0.4565
0.2311 0.8913
0.0185
0.6068 0.7621
0.8214
The above (vector) commands can also be applied to a matrix.
In this case, they act in a column-by-column fashion to produce a row vector
containing the results of their application to each column.
Matrix operations
Example:
A = [ 1 2 3 ; 4 5 6; 7 8 9];
B = [ 7 5 6 ; 2 0 8; 5 7 1];
Operation Oper Description Example
ator
Addition + cij = aij + bij >> C=A+B
C= 8 7 9
6 5 14
12 15 10
Subtraction - cij = aij - bij >> C=A-B
C = -6 -3 -3
2 5 -2
2 1 8
Multiplication * n >> C=A*B
C = 26 26 25
cij = ∑ aik * bkj 68 62 70
k=1 110 98 115
Right division / C = A/B >> C=A/B
=> A*inv(B) C = -0.5254 0.6864 0.6610
-0.4237 0.9407 1.0169
-0.3220 1.1949 1.3729
Left division \ C = A\B A = [4,5,3; 2,1,4; 3,2,6]
=> inv(A)*B B = [1,2,3; 4,5,6; 7,8,9]
>>C = A\B
C = -4.2000 -2.4000 -0.6000
2.0000 1.0000 -0.0000
2.6000 2.2000 1.8000
Exponentiation ^ n >> C=A^2
C = 30 36 42
cij = ∑ aik * akj
k=1
66 81 96
102 126 150
Element-by- .* cij = aij * bij >>C = A .* B
element
multiplication C = [10, 40, 90 ; 160, 250, 360; 490,
640, 810]
Element-by- ./ cij = aij / bij >>C = A ./ B
element left
division C = [ 10, 10, 10; 10, 10, 10; 10, 10, 10
]
Element-by- .^ cij = aij ^ x >>C = A .^ 2
element
exponentiation C = [ 1, 4, 9; 16, 25, 36; 49, 64, 81 ]
A = [1 3; 2 4]
Example:
The vectors
x = (1; 2; 3; 4; 5; 6)
y = (3;-1; 2; 4; 5; 1)
>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot(x,y)
Multiple (x; y) pairs arguments create multiple graphs with a single call to plot.
For example,
>> x = 0:pi/100:2*pi;
>> y1 = 2*cos(x);
>> y2 = cos(x);
>> y3 = 0.5*cos(x);
>> plot(x,y1,'--',x,y2,'-',x,y3,':')
>> xlabel('0 \leq x \leq 2\pi')
>> ylabel('Cosine functions')
>>
legend('2*cos(x)','cos(x)','0.5*cos(x)')
>> title('Typical example of multiple
plots')
>> axis([0 2*pi -3 3])
Specifying line styles and colors
Using plot command, we can specify line styles, colors, and markers
(e.g., circles, plus signs, . . )
plot(x, y, 'style_color_marker')
Example:
Time and Temp
55
y = [48];
Temperature
plot(x, y, 'r*') 45
xlabel('Time')
ylabel('Temperature')
% Put a title on the plot
35
9 10 11 12 13 14 15 16 17 18 19 20
Time
x = [11,15,18];
y = [48,50,51]; 50
plot(x, y, ':gx')
% Change the axes and label them
Temperature
axis([9 20 35 55]) 45
40
35
9 10 11 12 13 14 15 16 17 18 19 20
Time
xlabel('Time')
ylabel('Temperature')
% Put a title on the plot
title('Time and Temp')
Introduction to programming in MATLAB
Script files: A script file is an external file that contains a sequence of Matlab
statements. By typing the filename, subsequent Matlab input is obtained from
the file. Script files have a filename extension of .m and are often called M-files.
Example:
Solution:
Use the MATLAB editor to create a file: File -> New -> M-file.
Enter the following statements in the file:
A = [1 2 3; 3 3 4; 2 3 3];
b = [1; 1; 2];
x = A\b
Save the file, for example, example1.m.
Run the file, at the command prompt , by typing:
>> example1
x=
-0.5000
1.5000
-0.5000
Example:
Function files: Function file is a script file (M-file) that adds a function
definition to Matlab’s list of functions.
Syntax:
function [output variables] = function_name ( input variables) ;
Example 1:
function y = myfunc (x)
y = 2*x^2 - 3*x + 1;
end
Save this file as: myfunc.m in your working directory.
This file can now be used in the command window just like any predefined
Matlab function. In the command window enter:
x = -2:.1:2; % Produces a vector of x values
y = myfunc(x); % Produces a vector of y values
plot (x,y)
Example 2:
Functions can have multiple inputs, which are separated by commas.
For example:
Functions can have multiple outputs, which are collected into a vector
Course Objectives:
Course Outcomes:
Note: (1) The roots of an equation are the abscissas of the points where the
graph y = f(x) cuts the x-axis.
(2) A polynomial equation of degree n will have exactly n roots, real or
complex, simple or multiple. A transcendental equation may have
one root or infinite number of roots depending on the form of f(x).
Direct method:
We know the solution of the polynomial equations such as linear equation
ax b 0 and quadratic equation ax 2 bx c 0 , will be obtained using direct
methods or analytical methods. Analytical methods for the solution of cubic
and quadratic equations are also well known to us.
There are no direct methods for solving higher degree algebraic equations or
equations involving transcendental functions. Such equations are solved by
numerical methods. In these methods we find an interval in which the root lies.
In this unit we will study some important methods of solving algebraic and
transcendental equations.
Bisection method:
Bisection method is a simple iteration method to solve an equation. This
method is also known as "Bolzano method of successive bisection". Sometimes
it is referred to as "Half-interval method". Suppose we know an equation of the
form f x 0 has exactly one real root between two real numbers x0 , x1 . The
number is chosen such that f x0 and f x1 will have opposite sign. Let us
bisect the interval x0 , x1 into two half intervals and find the midpoint
x x1
x2 0 . If f x2 0 then x2 is a root.
2
If f x1 and f x2 have same sign then the root lies between x0 and x2. The
interval is taken as x0 , x2 Otherwise the root lies in the interval x2 , x1 .
Repeating the process of bisection , we obtain successive subintervals which
are smaller. At each iteration, we get the mid-point as a better approximation of
the root. This process is terminated when interval is smaller than the desired
accuracy.
Consider x0 0 and x1 1
By bisection method the next approximation is
x x 1
x2 0 1 0 1 0.5
2 2
f x2 f 0 : 5 1.375 0 and f 0 0
We have the root lies between 0 and 0.5
0 0.5
Now x3 0.25
2
We find f x3 0.234375 0 and f 0 0
Since f 0 0 , we conclude that root lies between x0 and x3
The third approximation of the root is
x x 1
x4 0 3 0 0.25
4 2
0.125
We have f x4 0.37495 0
Since f x4 0 and f x3 0 , the root lies between
x4 0.125 and x3 0.25
Considering the 4th approximation of the roots
x x4 1
x5 3 0.125 0.25 0.1875
2 2
f x5 0.06910 0 ,
since f x5 0 and f x3 0 the root must lie between x5 0.18758 and
x3 0.25
Here the fifth approximation of the root is
1
x6 x5 x3
2
1
0.1875 0.25
2
0.21875
We are asked to do up to 5 stages. We stop here and 0.21875 is taken as an
approximate value of the root and it lies between 0 and 1
function c = bisection(f,a,b)
if f(a)*f(b)>0
disp('Interval has no root')
else
c = (a + b)/2;
while abs(f(c)) > 1e-7
if f(a)*f(c)> 0
a = c;
else
b = c;
end
c = (a + b)/2;
end
end
Output
>> f=@(x)x^3-5*x+1;
>> bisect(f,0,1)
ans =
0.2016
False Position Method ( Regula – Falsi Method)
In the false position method we will find the root of the equation f x 0 .
Consider two initial approximate values x0 and x1 near the required root so
that f x0 and f x1 have different signs. This implies that a root lies between
x0 and x1 . The curve f x crosses x- axis only once at the point x2 lying
between the points x0 and x1 , Consider the point A x0 , f x0 and B x1 , f x1
on the graph and suppose they are connected by a straight line, Suppose this
line cuts x-axis at x2 , We calculate the values of f x2 at the point. If
f x0 and f x2 are of opposite sign, then the root lies between x0 and x2 and
value x1 is replaced by x2
Otherwise the root lies between x2 and x1 and the value of x0 is replaced by x2 .
Another line is drawn by connecting the newly obtained pair of values. Again
the point here the line cuts the x-axis is a closer approximation to the root.
This process is repeated as many times as required to obtain the desired
accuracy. It can be observed that the points x2 , x3 , x4 .....obtained converge to
the expected root of the equation y f x
x2
1 2 2 4
2 4
2 8 10
1.666
6 6
f 1.666 1.666 1.666 4
3
1.042
Now, the root lies between 1.666 and 2
1.666 2 2 1.042
x3 1.780
2 1.042
f 1.780 1.780 1.780 4
3
0.1402
Now, the root lies between 1.780 and 2
1.780 2 2 0.1402
x4 1.794
2 0.1402
f 1.794 1.794 1.794 4
3
0.0201
Now, the root lies between 1.794 and 2
1.794 2 2 0.0201
x5 1.796
2 0.0201
f 1.796 1.796 1.796 4 0.0027
3
function c=falsePos(f, a, b)
%False Position method for nonlinear equation
if f(a)*f(b) > 0
disp('Interval has no root')
else
c = (a*f(b)-b*f(a))/(f(b)-f(a));
while abs(f(c)) > 1e-7
if f(a)*f(c) > 0
a=c;
else
b=c;
end
c = (a*f(b)-b*f(a))/(f(b)-f(a));
end
Output:
>> f=@(x)x^3-x-4;
>> falsePos(f,1,2)
ans =
1.7963
f xn
xn 1 xn
f 1 xn
Problem:-
Find by Newton’s method, the real root of the equation xe x 2 0 Correct to
three decimal places.
Sol: Let f x xe 2 1
x
By Newton’s Rule
f x0
First approximation x1 x0
f 1 x0
0.7183
1 0.8679
5.4366
f x1 0.0672 f 1 x1 4.4491
f x1
The second approximation x2 x1
f 1 x1
0.0672
0.8679
4.4491
0.8528
Required root is 0.853 correct to 3 decimal places.
function c=newtonR(f,df,a)
Output:
>> f=@(x)x^3-x-4;
>> df=@(x)3*x^2-1;
>> newtonR(f,df,1)
ans =
1.7963
UNIT-III
INTERPOLATION
Objectives:
Develop an understanding of the use of numerical methods in modern
scientific computing.
To gain the knowledge of Interpolation
Syllabus:
Pre-requisite : Commands of MATLab
Interpolation – Introduction - Finite differences - Forward Differences -
Backward differences - Central differences - Newton Interpolation formulae -
Gauss Interpolation formulae - Lagrange’s interpolation.
Learning Outcomes:
Student should be able to
Know about the Interpolation, and Finite Differences.
Utilize the Newton’s formulae for interpolation.
Utilize the Gauss formulae for interpolation.
Operate Lagrange’s Interpolation formula.
Introduction:
Consider the statement y =f(x) , x0 x xn we understand that we can find
the value of y, corresponding to every value of x in the range x 0 x xn. If the
function f(x) is single valued and continuous and is known explicitly then the
values of f(x) for certain values of x like x0, x1,…….xn can be calculated.
Now the problem is, if we are given the set of tabular values
Satisfying the relation y = f(x) and the explicit definition of f(x) is not
known, is it possible to find a simple function say (x) such that f(x) and (x)
agree at the set of tabulated points. This process of finding (x) is called
interpolation. If (x) is a polynomial then the process is called polynomial
interpolation and (x) is called interpolating polynomial. In our study we are
concerned with polynomial interpolation.
Interpolation is the process of deriving a simple function from a set of
discrete data points so that the function passes through all the given data
points (i.e. reproduces the data points exactly) and can be used to estimate
data points in-between the given ones.
Finite Differences:
Here we introduce forward, backward and central differences of a
function y = f(x). These differences play a fundamental role in the study of
differential calculus, which is an essential part of numerical applied
mathematics.
1. Forward Differences:
Consider a function y = f(x) of an independent variable x. let
y0,y1,y2…….yn be the values of y corresponding to the values x0,x1,x2…….xn of x
respectively. Then the differences y1-y0, y2 – y1.............. are called the first
forward differences of y, and we denote them by y0 , y1 ,........
that is y0 y1 y0 , y1 y2 y1 , y2 y3 y2 ,...........
In general yr yr 1 yr where r 0,1, 2,3,......n 1
Here the symbol is called the forward difference operator.
The second forward differences and are denoted by 2 y0 , 2 y1 , 2 y2 ,...........
that is
2. Backward Differences:
Consider a function y = f(x) of an independent variable x. let
y0,y1,y2…….yn be the values of y corresponding to the values x0,x1,x2…….xn of x
respectively. Then the differences y1-y0, y2 – y1.............. are called the first
backward differences of y, and we denote them by y1 , y2 ,........
that is y1 y1 y0 , y2 y2 y1 , y3 y3 y2 ,...........
In general yr yr yr 1 where r 1, 2,3,......n
Here the symbol is called the backward difference operator.
The second backward differences and are denoted by 2 y2 , 2 y3 , 2 y4 ,...........
That is
3.Central Differences:
Consider a function y = f(x) of an independent variable x. let
y0,y1,y2…….yn be the values of y corresponding to the values x0,x1,x2…….xn of x
respectively. We define the first central differences
as follows
The symbol is called the central differences operator. This operator is a linear
operator
Comparing expressions (1) above with expressions earlier used on forward and
backward differences we get
In general
The first central differences of the first central differences are called the
second central differences and are denoted by
Thus
Higher order central differences are similarly defined. In general the nth
central differences are given by
for odd
for even
while employing for formula (4) for , we use the notation
If y is a constant function, that is if a constant, then
If f(x) is a function of x and h be the increment in x then forward
h h
difference of f(x) is defined as f ( x) f x f x
2 2
X 0 1 2 3 4
Y 1 3 9 - 81
Why this value is not equal to . Explain
Sol. Consider
From the given data we can conclude that the given function is . To
find , we have to assume that y is a polynomial function, which is not so.
Thus we are not getting
Q.Evaluate
This polynomial passes through all the points (for i = 0 to n. Therefore, we can
obtain the yi ' s by substituting the corresponding xi ' s as
This implies
From (1) and (2), we get
Solving the above equations for , we get
x Y
50 205
20
60 225 3
23 0
70 248 3
26
80 274
Let temperature =
and impose the condition that y and should agree at the tabulated points
We obtain
Where
This uses tabular values of the left of yn. Thus this formula is useful
formula is for interpolation near the end of the tabular values.
Q. The sales for the last five years is given in the table below. Estimate the
sales for the year
1979
Sol.
Q. Consider the following table of values
Sol.
MatLab Code for Newton’s Backward Interpolation Formula:
function yval=nbdi(xd,yd,xval)
n=length(xd);
bdt(:,1)=xd';
bdt(:,2)=yd';
for j=3:n+1
for i=n:-1:j-1
bdt(i,j)=bdt(i,j-1)-bdt(i-1,j-1);
end
end
bdt
h=xd(2)-xd(1)
p=(xval-xd(n))/h
c=ones(1,n);
for r=1:n-1
c(r+1)=prod(p:1:p+r-1)/factorial(r);
end
terms=bdt(n,2:n+1).*c;
yval=sum(terms);
fprintf('\n The approximated y value at given x=%f is: %f',xval,yval)
end
Gauss Forward Interpolation Formula:
Gauss forward interpolation formula is given by
p( p 1) 2 ( p 1) p( p 1) 3 ( p 1) p( p 1)( p 2) 4
y p y0 py0 y1 y1 y2
2! 3! 4!
( p 2)( p 1) p( p 1)( p 2) 5
y2 .....
4!
Where
Q. Find f(30) from the following table values using Gauss forward difference
formula:
x: 21 25 29 33 37
Sol:
x f f 2f 3f 4f
21 18.4708
-0.6564
25 17.8144 -0.0510
-0.7074 -0.0054
29 17.1070 0.0564 -0.0022
-0.7638 -0.0076
33 16.3432 -0.0640
-0.8278
37 15.5154
p ( p 1) 2 ( p 1) p ( p 1) 3 ( p 1) p( p 1)( p 2) 4
y p y0 py0 y1 y1 y2 .....
2! 3! 4!
Therefore f (30) = 16.9217
p=(xp-xd(k))/h;
pt=cumprod([1,p-(0:n-3)]);
dt=[tbl(k,1),tbl(k,2),tbl(k-1,3:n-1)+tbl(k-1,4:n)]./factorial(0:n-2);
yval=sum(pt.*dt);
end
The above formula involves odd differences above the central horizontal
line and even differences on the line.
Q. From the following data find y when x=38 by using Gauss backward
interpolation formula
x 30 35 40 45 50
y 15.9 14.9 14.1 13.3 12.5
x y y 2 y 3 y 4 y
30 15.9
-1
35 14.9 0.2
-0.8 -0.2
40 14.1 0 0.2
-0.8 0
45 13.3 0
-0.8
50 12.5
points be in
the following form
Sol. Given
Here then
Assignment-Cum-Tutorial Questions
Section-A
Objective Questions:
a) b)
c) d)
X 1 2 3 4
4. f(x) 1 4 27 64
If x=2.5 then p= [ ]
a) 1.5 b) 1 c) 2.5 d) 2
5. X 0.1 0.2 0.3 0.4
f(x) 1.005 1.02 1.045 1.081
When p=0.6, x= [ ]
a) 0.16 b) 0.26 c) 0.1 d) 3.0 2.
9. [ ]
a) b) c) d)
10. [ ]
a) b) c) d)
11.
X 0 1 2
F(x) 7 10 13
12. Using Lagrange’s interpolation, find the polynomial through (0,0),(1,1) and
(2,2).
Section-B
Subjective Questions
1. Prepare a MATLAB code to construct a forward difference table for the
given data.
2. Prepare a MATLAB code to construct a backward difference table for the
given data.
3. Certain corresponding values of x and log x are (300, 2.4771), (304,
2.4829), (305, 2.4843) and (307, 2.4871). Find log 301.
4. If the interval of differencing unity prove that
5. Find a cubic polynomial in x which takes on the values -3, 3, 11, 27, 57
and 107, when x=0, 1, 2, 3, 4 and 5 respectively.
6. Using Newton’s forward interpolation formula, for the given table of values
X 1.1 1.3 1.5 1.7 1.9
0.21 0.69 1.25 1.89 2.61
Obtain the value of f(x) when x= 1.4.
20. Find the number of students who got marks between 40 and 45
Marks : 30-40 40-50 50-60 60-70 70-80
No. of students : 31 42 51 35 31
Objectives:
To understand the concepts of numerical differentiation and integration.
Syllabus:
Pre-requisite : Commands of MATLab
Approximation of derivative using Newton’s forward and backward formulae -
Integration using Trapezoidal and Simpson’s rules.
Learning Outcomes:
At the end of the unit, Students will be able to
Utilize numerical techniques to evaluate derivatives at a point.
Utilize numerical techniques to evaluate definite integrals.
Calculate the area and slope of a given curve.
Introduction:
dy
dy
1
2 p 1 2
3 p2 6 p 2 3
4 p3 18 p 2 22 p 6 4
y0 y0 y0 y0 ...
dx x 51 dx p 0.1 h 2! 3! 4!
1
16.69 2.188 2.1998 1.9863 1.0316
10
d2 y 1 2
6 p 2 18 p 11 4
2 y0 p 1 3
y0 y0 ...
dx p 0.1 h
2
12
6 .12 18 .1 11
5.47 0.1 1 9 23 11.99
1
100 12
1
5.47 8.307 9.2523
100
=0.2303.
Q. The population of a certain town is shown in the following table
Year x 1931 1941 1951 1961 1971
Population y 40.62 60.80 79.95 103.56 132.65
dy 1
y4
2 p 1 2
y4
3 p2 6 p 2 3
y4
2 p3 9 p 2 11 p 3 4
y4 ...
dx h 2 6 12
x x0 1961 1971
Where p 1
10 10
The backward difference table
x Year y Population y 2 y 3 y 4 y
1931 40.62
20.18
1941 60.80 -1.03
19.15 5.49 -4.47
1951 79.95 4.46
23.61 1.02
1961 103.56 5.48
29.09
1971 132.65
Numerical Integration :
Simpson’s 1/3rd Rule:
Memorise:
Trapezoidal Rule:
b
h
f ( x)dx 2 y
a
0 yn 2( y1 y2 y3 ....... yn 1 )
h
( sum of first and last ordinates 2( sum of remaining ordinates))
2
h
( sum of first and last ordinates 2( sum of even ordinates) 4( sum of odd ordinates ))
2
h
( sum of first and last ordinates 2(sum of multiples of 3 ordinates) 3( sum of remaining ordinates ))
2
1
Simpson’s three eight rule. Take h for all cases.
6
1 1
Solutions: Here h , Let y f ( x) . The values of f(x) for the points of
6 1 x
subdivisions are as follows:
1 2 3 4 5
x 0 1
6 6 6 6 6
1
y 1 0.8571 0.75 0.6667 0.6 0.5455 0.5
1 x
dx h
1 x 3 y y6 2 y2 y4 ) 4( y1 y3 y5
1
0 0
1
; 1 0.5 2 0.75 0.6) 4(0.8571 0.6667 0.5455
18
= 0.6932.
dx 3h
y0 y6 3 y1 y2 y4 y5 2 y3
1
1 x
0 8
1 0.5 3 0.8571 0.75 0.6 0.5455 2 0.6667
1
;
16
= 0.6932.
1
MATLAB Code to numerical integration by using Simpson’s rd Rule:
3
function intval=simpsons1(f,a,b,n)
if rem(n,2)==0
h=(b-a)/n;
x=linspace(a,b,n+1);
y=f(x);
intval=(h/3)*(y(1)+y(n+1)+2*sum(y(3:2:n-1))+4*(sum(y(2:2:n))));
fprintf('\nthe approximate value of given integral is %f',intval)
else
error('number of subintervals n must be even')
end
3
MATLAB Code to numerical integration by using Simpson’s rd Rule:
8
function intval=simpsons2(f,a,b,n)
if rem(n,3)==0
h=(b-a)/n;
x=linspace(a,b,n+1);
y=f(x);
intval=(3*h/8)*(y(1)+y(n+1)+2*sum(y(4:3:n-2))+3*(sum(y)-y(1)-y(n+1)-
sum(y(4:3:n-2))));
fprintf('\n the approximate value of given integral is %f',intval)
else
error('number of subintervals n must be even')
end
Assignment-Cum-Tutorial Questions
Section-A
Objective Questions:
Section-B
Subjective Questions
Time(sec) 0 1 2 3 4 5 6
Distance(meter) 0 2.5 8.5 15.5 24.5 36.5 50
Determine the speed of the athlete at t = 5 sec. correct to two decimals.
6. A curve is drawn to pass through the points given by following table:
x 1 1.5 2.0 2.5 3 3.5 4.0
y 2 2.4 2.7 2.8 3 2.6 2.1
Find the slope of the curve at x=1.25.
7. Write a MATLAB code to find derivatives at a point by using Newtons
forward interpolation formula.
8. Prepare a MATLAB code to find derivatives at a point by using Newtons
backward interpolation formula.
2
9. Evaluate e
x 3
dx using Simpson’s rule taking h=0.25
0
10. A river is 80 meters wide. The depth 'd’ in meters at a distance x from the
bank is given in the following table. Calculate the cross section of the
river using Trapizoidal rule.
x 10 20 30 40 50 60 70 80
d(x) 4 7 9 12 15 14 8 3
5.2 5.2
11. Compute the value of the definite integral log xdx or ln xdx
4 4
using
i.Trapezoidal Rule ii. Simpson’s 1/3rd Rule and iii. Simpson’s 3/8th Rule.
12. Create a MATLAB function file to find a definite integral by using
Trapezoidal rule.
13. Design a MATLAB code to find a definite integral by using Simpson’s
1/3rd rule.
14. Write a MATLAB function file to find a definite integral by using
Simpson’s 3/8th rule.
Section-C
GATE/IES/Placement Tests/Other competitive examinations
by taking 6 sub-intervals.
2
1
3. Minimum number of subintervals required to evaluate the integral x dx
1
t (seconds) 0 2 4 6 8 10 12
v meters per 4 6 16 34 60 94 136
second
Find (i) the distance moved by the particle in 12 seconds and also (ii) the
acceleration at t = 2 sec.
t sec 0 10 20 30 40 50 60 70 80
f(cm/sec2) 30 31.63 33.34 35.47 37.75 40.33 43.25 46.69 50.67
-----------------@-@-@-@-@----------------
UNIT–V: Numerical Solutions of Ordinary Differential Equations
Pre-requisite : Commands of MATLab
Objectives:
• To the numerical solutions of a first ordered Ordinary Differential Equation together with initial
condition.
Syllabus:
Taylor’s Series Method - Euler Method - Modified Euler Method - Runge – Kutta Fourth order Method.
Subject Outcomes:
At the end of the unit, Students will be able to
• implement the numerical methods in Matlab in order to solve Ordinary Differential equations.
The important methods of solving ordinary differential equations of first order numerically are as
follows
Euler’s method
To describe various numerical methods for the solution of ordinary differential equations we consider
the general 1st order differential equation
The methods will yield the solution in one of the two forms:
i) A series for y in terms of powers of x, ,from which the value of y can be obtained by direct
substitution.
ii ) A set of tabulated values of y corresponding to different values of x.
dy
f ( x, y ) (1)
dx
( x x0 ) ( x x0 ) 2 ( x x0 ) n n
y ( x) y ( x0 ) y( x0 ) y( x0 ) ............ y ( x0 ) (3)
1 2! n!
x2 xn n
y ( x) y (0) x. y(0) y(0) ...... y (0) ........ (4)
2! n!
Note: We know that the Taylor’s expansion of y(x) about the point x0 in a power of (x – x0)is.
( x x0 ) I ( x x0 ) 2 II ( x x0 )3 III
y(x) = y(x0) + y (x0) + y (x0) + y (x0) + … (1)
1! 2! 3!
Or
( x x0 ) I ( x x0 ) 2 II ( x x0 )3 III
y(x) = y0 + y0 + y0 + y0 + …..
1! 2! 3!
h I h 2 II h3 III h 4 IV
y(x) = y(x1) = y0 + y + y0 + y0 + y0 + ….
1! 0 2! 3! 4!
h I h 2 II h3 III h IV IV
i.e. y1 = y0 + y + y0 + y0 + y0 + ….. (2)
1! 0 2! 3! 4!
Similarly expanding y(x) in a Taylor’s series about x = x1. We will get.
h I h 2 II h3 III h 4 IV
y2 = y1 + y1 + y1 + y1 + y1 + ……. (3)
1! 2! 3! 4!
h I h 2 II h3 III h 4 IV
y 3 = y2 + y2 + y2 + y2 + y2 + …... (4)
1! 2! 3! 4!
h I h 2 II h3 III h 4 IV
yn+1 = yn + yn + yn + yn + yn + ….. (5)
1! 2! 3! 4!
Example 1. Using Taylor’s expansion evaluate the integral of y 2 y 3e x , y (0) 0 at x 0.2 . Hence
compare the numerical solution obtained with exact solution .
x2 x3 x4 x5
y ( x) y (0) xy(0) y(0) y(0) y(0) y(0) ....
2! 3! 4! 5!
9 2 21 3 45 4 93 5
y ( x) 0 3 x x x x x ........
2 6 24 120
9 2 7 3 15 4 31 5
y ( x) 3x x x x x ........ equ1
2 2 8 40
Now put x 0.1 in equ1
9 7 15 31
y (0.1) 3(0.1) (0.1) 2 (0.1)3 (0.1) 4 (0.1) 5 0.34869
2 2 8 40
9 7 15 31
y (0.2) 3(0.2) (0.2) 2 (0.2) 3 (0.2) 4 (0.2) 5 0.811244
2 2 8 40
9 7 15 31
y (0.3) 3(0.3) (0.3) 2 (0.3) 3 (0.3) 4 (0.3) 5 1.41657075
2 2 8 40
Analytical Solution:
dy
The exact solution of the equation 2 y 3e x with y (0) 0 can be found as follows
dx
dy
2 y 3e x Which is a linear in y.
dx
Here P 2, Q 3e x
pdx 2 dx
I.F =
e
e
e 24
2 x x 2 x x
General solution is y.e 3e .e dx c 3e c
y 3e x ce 2 x where x 0, y 0 0 3 c c 3
syms x y(x);
xd=x0:h:xn;
n=length(xd);
yd=zeros(1,n);
yd(1)=y0;
for i=1:n-1
fold=f;
d=ones(1,not-1);
for j=1:not-1
d(j)=subs(fold,{x,y},{xd(i),yd(i)});
fold=subs(diff(fold,x),diff(y(x),x),fold);
end
yd(i+1)=yd(i)+sum(d.*(h.^(1:not-1)./factorial(1:not-1)));
end
soltable=[xd' yd'];
end
EULER’S METHOD
It is the simplest one-step method and it is less accurate. Hence it has a limited application.
dy
Consider the differential equation = f(x,y) (1)
dx
Similarly at x = x2 , y2 = y1 + h f(x1,y1),
dy
Example 1. Using Euler’s method solve for x = 2 from = 3x2 + 1,y(1) = 2,taking step size
dx
(I) h = 0.5 and (II) h=0.25
y1 = y0 + h f(x0,y0)
y(1.5) = 4 = y1
y2 = y1 + h f(x1,y1)
y(2) = 7.875
h = 0.25 x1 = 1.25, x2 = 1.50, x3 = 1.75, x4 = 2
Taking n = 0 in (1), we have
y1 = y0 + h f(x0,y0)
y(x2) = y2 = y1 + h f(x1,y1)
= 3 + (0.25)[3(1.25)2 + 1]
= 4.42188
y(1.5) = 5.42188
= 6.35938
y(x4) = y4 = y3 + h f(x3,y3)
= 7.35938 + (0.25)[3(1.75)2 + 1]
= 8.90626
Note that the difference in values of y(2) in both cases
(i.e. when h = 0.5 and when h = 0.25).The accuracy is improved significantly when h is reduced to 0.25
(Exact solution of the equation is y = x3 + x and with this y(2) = y2 = 10.
dy
f ( x, y ) with the initial condition y ( x0 ) y0
dx
x=x0:h:xn;
n=length(x);
y=zeros(1,n);
y(1)=y0;
for i=1:n-1
y(i+1)=y(i)+h*f(x(i),y(i));
end
soltable=[x' y'];
end
i 1
It is given by y
i
yk h / 2 f xk , yk f xk 1 ,1k 1 , i 1, 2....., ki 0,1.....
k 1
Working rule :
We get y1
1
y0 h / 2 f x0 , y0 f x1 , y1i 1 ……………………… (3)
y11 y0 h / 2 f x0 , y0 f x1 , y1 0
y1 2 y0 h / 2 f x0 , y0 f x1 , y11
------------------------
y1 k 1 y0 h / 2 f x0 , y0 f x1 , y1 k
k k 1
If two successive values of y1 , y1 are sufficiently close to one another, we will take the common
value as y2 y x2 y x1 h
Example1. Using modified Euler’s method, find the approximate value of x when x 0.3 given that
dy / dx x y and y 0 1
Here f x, y x y, x0 0, and y0 1
yk 1i yk h / 2 f xk yk f xk 1 , yk 1i 1 1
Step1: To find y1= y(x1) = y (0.1)
Taking k = 0 in eqn(1)
yk 1i y0 h / 2 f x0 y0 f x1 , y1i 1 2
when i = 1 in eqn (2)
y1i y0 h / 2 f x0 , y0 f x1 , y1 0
(0)
First apply Euler’s method to calculate y 1
= y1
y1 0 y0 h f x0 , y0
= 1+(0.1)f(0.1)
= 1+(0.1)
= 1.10
1
y1 y0 0.1/ 2 f x0 , y0 f x1 , y1
0
= 1+0.1/2[f(0,1) + f(0.1,1.10)
= 1+0.1/2[(0+1)+(0.1+1.10)]
= 1.11
y1 2 y0 h / 2 f x0 , y0 f x1 , y11
= 1+0.1/2[f(0.1)+f(0.1,1.11)]
= 1 + 0.1/2[(0+1)+(0.1+1.11)]
= 1.1105
y13 y0 h / 2 f x0 , y0 f x1 , y1 2
= 1+0.1/2[f(0,1)+f(0.1 , 1.1105)]
= 1+0.1/2[(0+1)+(0.1+1.1105)]
= 1.1105
2
Since y1 y13
y1 = 1.1105
y2i y1 h / 2 f x1 , y1 f x2 , y2i 1 3
I = 1,2,3,4,…..
For i = 1
y21 y1 h / 2 f x1 , y1 f x2 , y2 0
y2 0 y1 h f x1 , y1
= 1.1105+(0.1)[0.1+1.1105]
= 1.2316
= 1.1105 +0.1/2[0.1+1.1105+0.2+1.2316]
= 1.2426
y2 2 y1 h / 2 f x1 , y1 f x2 y2 1
= 1.1105 + 0.1/2[f(0.1 , 1.1105) , f(0.2 . 1.2426)]
= 1.1105 + 0.1(1.3266)
= 1.2432
y23 y1 h / 2 f x1 , y1 f x2 y2 2
= 1.1105+0.1/2[f(0.1,1.1105)+f(0.2 , 1.2432)]
= 1.1105+0.1/2[1.2105+1.4432)]
= 1.1105 + 0.1(1.3268)
= 1.2432
3
Since y2 y23
Hence y2 = 1.2432
Step:3
y31 y2 h / 2 f x2 , y2 f x3 , y3i 1 4
For i = 1 ,
y31 y2 h / 2 f x2 , y2 f x3 , y3 0
y3 0 is to be evaluated from Euler’s method .
y3 0 y2 h f x2 , y2
= 1.2432+(0.1)(1.4432)
= 1.3875
= 1.2432 + 0.1/2[1.4432+1.6875]
= 1.2432+0.1(1.5654)
= 1.3997
y3 2 y2 h / 2 f x2 , y2 f x3 , y31
= 1.2432+0.1/2[1.4432+(0.3+1.3997)]
= 1.4003
y33 y2 h / 2 f x2 , y2 f x3 , y3 2
= 1.2432 + 0.1(1.5718)
= 1.4004
y3 4 y2 h / 2 f x2 , y2 f x3 , y33
= 1.2432 + 0.1/2[1.4432+1.7004]
= 1.2432+(0.1)(1.5718)
= 1.4004
3
Since y3 y3 4
x=x0:h:xn;
n=length(x);
y=zeros(1,n);
y(1)=y0;
for i=1:n-1
yp=y(i)+h*f(x(i),y(i));
y(i+1)=y(i)+h*(f(x(i),y(i))+f(x(i),yp))/2;
while abs(yp-y(i+1)>tol)
yp=y(i+1);
y(i+1)=y(i)+h*(f(x(i),y(i))+f(x(i),yp))/2;
end
end
soltable=[x' y'];
end
K3 = h (xi+h, yi+2k2-k1)
For i= 0,1,2-------
K2 = h (xi+h/2, yi+k1/2)
K3 = h (xi+h/2, yi+k2/2)
K4 = h (xi+h, yi+k3)
For i= 0,1,2-------
Example 1. Apply the 4th order R-K method to find an approximate value of y when x=1.2 in stepsof 0.1,
given that y1 = x2+y2, y (1)=1.5
Step1:
To find y1 :
y1=y0+1/6 (k1+2k2+2k3+k4)
and
= 0.39698
Hence
1
y1 1.5 0.325 2 0.3866 2 0.39698 0.48085
6
1.8955
Step2:
y2 = y1+(1/6) (k1+2k2+2k3+k4)
= 0.611715
Hence
dy
f ( x, y ) with the initial condition y ( x0 ) y0
dx
n=length(x);
y=zeros(1,n);
y(1)=y0;
for i=1:n-1
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h/2,y(i)+k1/2);
k3=h*f(x(i)+h/2,y(i)+k2/2);
k4=h*f(x(i)+h,y(i)+k3);
y(i+1)=y(i)+(k1+2*(k2+k3)+k4)/6;
end
soltable=[x' y'];
end
Assignment-cum-Tutorial Questions
I Objective Questions
dy
1. If = f(x,y),y(x0) = y0,the formula for fourth order Runge – Kutta method is _______
dx
4. Among the following, which is the best for solving initial value problem?
dy y x
2. If = , y(0) = 1 and h = 0.02, using Euler’s method the value of y1 = _________
dx y x
dy
3. If = x2+y2, y(0) = 0 using Taylor’s series method, the value of y(0.4)=___________
dx
4. The value of y at x= 0.1 using Runge – Kutta method of fourth order for the differential
dy
equation = x – 2y, y(0) = 1 taking h = 0.1 is __________
dx
5. The value of y at x = 0.1 using modified Euler’s method up to second approximation for
dy
6 If = 1 + y2, f(x0,y0) =1,h – 0.2,K1 = 0.2,K2 = 0.202,K3 = 0.20204,K4 = 0.20216, then the
dx
dy
7. Using Runge – Kutta method, the approximate value of y(0.1) if = x + y2,y = 1
dx
where x =0 and f(x0,y0) = 1 K1 = 0.1, K2 = 0.115, K3 = 0.116, K4 = 0.134 is
dy
8. Using Runge-kutta method, to solve the differential equation x y , h=0.1 and
dx
y(0) = 1.
II Problems
1. Solve y1 = x-y2, y(0) = 1 using Taylor’s series method and evaluate y(0.1), y(0.2).
2. Given y1 = x+ sin y, y(0)=1 compute y(0.2) and y(0.4) with h=0.2 using modified Euler’s
method
3. Using R-K method, find y(0.2) for the equation dy/dx=y-x , y(0)=1,take h=0.22.
dy
given that y=1 when x=0 and x y
dx
dy
4. Using Taylor’s series method, solve the equation x 2 y 2 for x 0.4 given that y0
dx
when x 0 4.
dy
5. Find the solution of = x-y , y(0)=1 at x =0.1 , 0.2 ,0.3 , 0.4 and 0.5 using modified
dx
Euler’s method.
6. Write the R-K method of 4th order formula for the solution of
7. Using R-K method, estimate y(0.2) and y(0.4) for the equation dy/dx=y2-x2/ y2+x2,y(0)=1,h=0.2.
8. Explain the Modified Euler's method.
dy
9. Find y(0.1) & y(0.2) using Euler's modified formula given that dx = x2 – y, y(0) = 1
10. Find y(0.1) & y(0.2) using Runge - Kutta 4th order formula given that y1 = x2 – y & y(0) =1.
11. Write a code in MATLAB to find the numerical solution of first order ordinary differential equation
using R-K Method of fourth order.
12. Write a code in MATLAB to find the numerical solution of first order ordinary differential equation
using Euler’s Method.
dy
1. Solve by Taylor’s series method, the equation log xy for y(1.1) and y(1.2), given
dx
y(1) = 2.
dy 2 xy e x
2. Using R – K method, solve for y at x = 1.2, 1.4 from given y(1) = 0.
dx x 2 xe x
GATE QUESTIONS
Objectives:
To understand the application of 'Least Square Method'
Learning Outcomes:
At the end of the unit, Students will be able to
Fit a straight line to the given data.
Fit a Parabola to the given data.
Fit an exponential curve and a power curve the given data.
The principle of least squares is one of the popular methods for finding a curve fitting a given
data. Say be n observations from an experiment. We are interested
in finding a curve
Closely fitting the given data of size 'n'. Now at while the observed value of y is , the
expected value of y from the curve (1) is . Let us define the residual by
....................(3)
...........................
Some of the residuals may be positive and some may be negative. We would like to find the
curve fitting the given data such that the residual at any is as small as possible. Now since
some of the residuals are positive and others are negative and as we would like to give equal
importance to all the residuals it is desirable to consider sum of the squares of these residuals,
say and thereby find the curve that minimizes . Thus, we consider
and find the best representative curve (1) that minimizes (4).
Note that is a function of parameters a and b. We need to find a,b such that is minimum.
The necessary condition for to be minimum is given by:
Equations (5) and (6) are called as normal equations,which are to be solved to get desired values
for a and b.
The expression for i.e (3) can be re-written in a convenient way as follows:
Example: Using the method of least squares, find an equation of the form
Solution: Consider the normal equations of least square fit of a straight line i.e
Here n=5.
From the given data, we have,
10a+5b=76.......................(4)
a = 9.1 , b= - 3 ................................................................(5)
Remarks:
(1) Experimental data may not be always linear. One may be interested in fitting either a curve of
the form However, both of these forms can be linearized by
taking logarithms on both the sides. Let us look at the details:
On taking logarithms on both the sides we get:
Say
which is linear in X, Y.
we get
which is linear in Y, x.
Example: By the method of least square fit a curve of the form to the following data:
Solution.
Consider ----(1)
n=length(x);
B=[sum(ln(y));sum(ln(y).*x)];
coef=A\B;
b=coef(1);
end
n=length(x);
A=[sum(x) n;sum(x.*x) sum(x)];
B=[sum(log10(y));sum(log10(y).*x)];
coef=A\B;
a=10^coef(2);
b=10^coef(1);
end
n=length(x);
B=[sum(ln(y));sum(ln(y).*ln(x))];
coef=A\B;
b=coef(1);
end
i.e
Similarly yields
i.e
Finally yields
Equations (4), (5) and (6) are called as normal equations whose solution yields the values of the
constants a, b and c and thus the desired parabola.
(1)
The normal equations are:
(2)
function [coef]=parabolafit(x,y)
% equation is y=ax^2+bx+c
n=length(x);
B=[sum(y);sum(y.*x);sum(y.*(x.^2))];
disp([A,B]);
coef=A\B;
end
Assignment-Cum-Tutorial Questions
Section-A
Objective Questions:
1. Write the normal equation in method of least squares to fit a straight line.
2. Write the normal equation in method of least squares to fit a parabola.
3. Is it necessary that the curve due to method of least squares agree at the points in the given
data?
4. In method of least squares to fit a straight line, is the following statement is ture?
"The mean of the data points is always a point on the straight line."
Section-B
Subjective Questions
1. Derive the normal equations in fitting a straight line in Method of least squares.
3. If p is the pull required to lift the weight by means of a ulley block, find a linear law of the
form p=a+bw, connecting p and w, using the following data:
5. Write a code in MATLAB to implement Least Squares Method to fit a straight line.
7. Write a code in MATLAB to implement Least Squares Method to fit a parabola curve.
x: 2 4 6 8 10
y: 3.07 12.85 31.47 57.38 91.29
9. The following table gives the results of the measurements of train resistances; V is the velocity
in miles per hour. R is the resistance in pounds per ton:
V: 20 40 60 80 100 120
R: 5.5 9.1 14.9 22.8 33.3 46.0
2
If R is related to V by the relation R=a+bV+cV , find a, b and c.
x: 2 4 6 8
y: 25 38 56 84
11. Predict y at x=3.75 , by fitting a power curve to the given data:
x: 1 2 3 4 5 6
y: 298 4.26 5.21 6.10 6.80 7.50
12. Write a code in MATLAB to implement Least Squares Method to fit a power curve.