Matlab Manual 4.4.24
Matlab Manual 4.4.24
2. Seminar Topics:
1. Newton – Raphson method
2. Lagranges Interpolation formula
3. Trapezoidal, Simpson 1/3 rule.
4. Derive Normal equations to fit a parabola by Method of least squares.
3. List of Lab Experiments
S.No Title of Lab Experiments Periods
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
cij = ∑ aik * bkj C = 26 26 25
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
% A simple plot of just one point!
% Create coordinate variables and plot a red ‘*’
x = [11];
50
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
Example: Ti me and Te mp
55
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
Ti me
xlabel('Time')
ylabel('Temperature')
% Put a title on the plot
title('Time and Temp')
Introduction to programming in MATLAB
The commands entered in the Command Window cannot be saved and executed
again for several times. To execute the commands repeatedly with MATLAB is:
1. to create a file with a list of commands,
2. save the file, and
3. run the file.
These files are called script files or scripts
They must have the file extension “.m”
Corrections or changes can be made to the commands in the file
There are two types of m-files: script files and function files.
M-files can be scripts that simply execute a series of MATLAB
statements, or they can be functions that can accept arguments and can
produce one or more outputs.
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:
n is a non-negative integer.
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 ax2 bx c 0 , will be obtained using direct
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
x0, x1 into two half intervals and find the midpoint
bisect the interval
x0 x1
x . If f x2 0 then x2 is a root.
2
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 x 2 , x1 .
Consider x0 0 and x1 1
x3 0.25
Here the fifth approximation of the root is
1
x x x
6
2 5 3
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
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. Againthe
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
y f x0 f x1 f x0
equation to the chord AB is 1
x x0 x x0
At the point C where the line AB crosses the x – axis, we have f(x) =0 i.e. y =0
x1 x0
From (1), we get x x0 f x0 2
f x1 f x0
x is given by (2) serves as an approximated value of the root, when the interval
in which it lies is small. If the new values of x is taken as x2 then (2) becomes
x x x1 x0 f x
0
f x1 f x0
2 0
x0 f x1 x1 f x0
3 (3)
f x1 f x0
We name that interval as x1, x2 . The line joining x1, y1 x2 , y2 meets x – axis at
x is given by x x1 f x2 x2 f x1
f x f x
3 3
2 1
This will in general, be nearest to the exact root we continue this procedure till
the root is found to the desired accuracy.
The iteration process based on (3) is known as the method of false
position. The successive intervals where the root lies, in the above procedure
are named as x0, x1 , x1, x2 , x2, x3 etc
Where xi xi 1 and f x0 , f xi 1 are of opposite signs
f xi f xi1
i1
Problem:-
Find out the roots of the equation x3 x 4 0 using false position method
Sol: Let f x x3 x 4 0
f 0 4, f 1 4, f 2 2
Since f 1 and f 2 have opposite signs the root lies between 1 and 2
f x1 f x0
2
x
1 2 2 4
2 2 4
28 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
x 1.780
3 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
x 1.794
4 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 20.0201
x 1.796
5 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 1 x 0
Substituting this in x1 we get
x1 x0 h
x f x0
0 f 1 x 0
x1 is a better approximation than x0
Successive approximations are given by
x x f xn
n
f 1 x
n1 n
Problem:-
Find by Newton’s method, the real root of the equation xe 2 0 Correct to
x
First approximation x x f x0
1 0 f 1 x 0
f x 0.0672
0.7183 5.4366
1 0.8679 f 1 x 4.4491
1 1
2 1 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 x0 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
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,........
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,........
3. Central Differences:
Consider a function y = f(x) of an independent variable x. lety0,y1,y2…….yn
be the values of y corresponding to the values x0,x1,x2…….xn of xrespectively. 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 beh the increment
h in x then forward
difference of f(x) is defined as f (x) f x f x
2 2
Note: As n f (x) is a constant, it follows that n1 f (x) 0, n2 f (x) 0, ......... The
converse of above result is also true that is, if n f (x) is tabulated at equal
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
Q. Find f(30) from the following table values using Gauss forward difference
formula:
x: 21 25 29 33 37
Sol:
x f f 2 f 3 f 4 f
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
𝑥−𝑥0 30−29
Let 𝑥 = 29 given x=30 and h=4 then 𝑝 = = = 0.25
0 ℎ 4
y y py p( p 1) 2 y
( p 1) p( p 1)
3 y
( p 1) p( p 1)( p 2)
4 y .....
p 0 0 1 1 2
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
𝑥−𝑥 0 38−40
Let 𝑥 = 40 given x=38 and h=5 then 𝑝 = = = −0.4
0 ℎ 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).
13. cosx = .
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 3 p2 6 p 2 4 p3 18 p2 22 p 6
y 2 y 3 y 4 y ...
dx dx h
0 0 0 0
2! 3! 4!
x51 p0.1
24
p0.1
1
16.69 2.188 2.1998 1.9863 1.0316
10
d 2 y
1 2 y p 1 y
3 6 p2 18 p 114 y ...
0 0 0
2
dx p0.1 h2 12
6 .1 2 18 .1 11
1
5.47 0.1 19 23 11.99
100 12
1 100
5.47 8.307 9.2523
=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 2 p 1 3 p2 6 p 2 2 p3 9 p2 11p 3
y
2 y 3 y 4 y ...
dx h
4 4 4 4
2 6 12
Where p x x0 1961 1971 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
3 1 6 1 2 1.02 2 1 9 1 11 1 3 4.47
1 1 2
3 2
dy
29.09
5.48
2
dx p1 10 6 12
1
29.09 2.74 0.17 0.3725
10
1
26.5525 2.6553
10
Numerical Integration :
Simpson’s 1/3rd Rule:
Memorise:
Trapezoidal Rule:
b
h
a f (x)dx 2 y0 yn 2( y1 y2 y3 ........ yn1)
h
(sumof first and last ordinates 2(sumof 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
(i) Tapezoidal
h rule
1 dx y y 2 y y y y y
1 x
2
0 0 6 1 2 3 4 5
1
; 1 0..5 2 0.8571 0.755 0.6667 0.6 0.5455
12
=0.6949.
3
0 0 6 2 4 1 3 5
1
; 1 0.5 2 0.75 0.6) 4(0.8571 0.6667 0.5455
18
= 0.6932.
8
0 0 6 1 2 4 5 3
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:
at [ ]
(a) end of the tabulated set (b) middle of the tabulated set
(c) Beginning of tabulated set (d) none of these
10. To increase the accuracy in evaluating a definite integral by Trapezoidal
rule, we should take
11. Values of y = f(x) are known as x = x0, x1 and x2. Using Newton’s forward
dy is
integration formula, the approximate value of dx
x x 0
equals to [ ]
14. For n =1in quadrature formula, f (x) dx
x0
(a)
h
f 0 f1 (b) f0 f1 (c)
h
f 0 f1 (d)
h
f 0 f1
2 2 4
15. To apply, Simpson’s 1/3rd rule, always divide the given range of integration
into ‘n’ subintervals, where n is [ ](a) even (b)
odd (c) 1,2,3,4 (d) 1,3,5,7
16. The process of calculating derivative of a function at some particular value
of the independent variable by means of a set of given values of that
function is []
(a) Numerical value (b) Numerical differentiation
(c) Numerical integration (d) quadrature
17. While evaluating definite integral by Trapezoidal rule, the accuracy can be
increased by []
(a) h =4 (b) even number of sub-intervals
(c) multiples of 3 (d) large number of sub-intervals
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
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
2. Using Simpson’s 1/3rd rule, find the value of the integr (sin x log x e x ) dx
0.2
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 x ) (x x )2 (x x )n n
y(x) y(x0 ) 0
y (x0 ) 0
y (x0 ) ............ 0
y (x0 ) (3)
1 2! n!
y(x 0), y (x 0),.........yn (x )0etc are obtained by successively differentiating equation1 and evaluating at
x0 . Substituting these values in equation3, y(x) at any point can be calculated from equation3.
Provided h x x0 is small.
Note: We know that the Taylor’s expansion of y(x) about the point x0 in a power of (x – x0)is.
(x x ) (x x )2 (x x )3
y(x) = y(x0) + 0
yI(x0) + 0
yII(x0) + 0
yIII(x0) + … (1)
1! 2! 3!
Or
(x x0 ) (x x )2 (x x )3
y(x) = y0 + y0I + 0
y0II + 0 y0III + …..
1! 2! 3!
h h 2 II h 3 III h 4 IV
y(x) = y(x1) = y0 + y0I + y0 + y0 + y + ….
1! 2! 3! 4!
0
h h2 h3 hIV
i.e. y1 = y0 + yI + yII + yIII + IV + ….. (2)
0 0 0 0
1! 2! 3! 4!
Similarly expanding y(x) in a Taylor’s series about x = x1. We will get.
h I h2 II h3 III h4 IV
y2 = y1 + y1 + ……. (3)
+ y1 y1 y1
1! 2! +
3! +
4!
y3 = y2 + y2 + y2 + y2 + y2 + …... (4)
1! 2! 3! 4!
Example 1. Using Taylor’s expansion evaluate the integral of y 2 y 3ex , y(0) 0 at x 0.2 . Hence
In general, y(n1) (x) 2.y(n) (x) 3ex or y(n1) (0) 2.y(n) (0) 3e0
2 3 4 5
xy (0) x y (0) x y (0) x y (0) x y (0) ....
y(x) y(0)
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 3ex with y(0) 0 can be found as follows
dx
dy
2 y 3ex Which is a linear in y.
dx
pdx 2dx
I.F =
e
e24
e
General solution is y.e2x 3ex.e2xdx c 3ex c
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
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.
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
Suppose consider dy/dx=f(x, y) -------- (1) with y(x0) =y0 ----------- (2)2
We get y y h / 2 f x , y
1
f x , y i1 ............................(3)
1 0
0 0 1 1
Taking i=1, 2, 3...k+1 in equation (3), we get
y 0 y h / 2 f x , y (By Euler’s method)
1 0 0 0
y k 1 y
h / 2 f x , y
f x , y k
1 0
0 0 1 1
value as y2 y x2 y x1 h
We use the above procedure again
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
y i
y h / 2 f x y f x ,y i1
1
k 1 k
k k k 1 k 1
Step1: To find y1= y(x1) = y (0.1)
Taking k = 0 in eqn(1)
y i
y h / 2 f x y
f x , y i1 2
k 1 0
0 0 1 1
when i = 1 in eqn (2)
y 0 y h f x , y
1 0 0 0
= 1+(0.1)f(0.1)
= 1+(0.1)
= 1.10
= 1+0.1/2[(0+1)+(0.1+1.10)]
= 1.11
= 1 + 0.1/2[(0+1)+(0.1+1.11)]
= 1.1105
y 3 y h / 2 f x , y
f x , y 2
1 0
0 0 1 1
= 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 3
Since y1 y1
y1 = 1.1105
For i = 1
y 1
y h / 2 f x , y f x , y 0
2 1
1 1 2 2
y 0
y h f x , y
2 1 1 1
= 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
y 2
y h / 2 f x , y f x y 1
2 1
1 1 2 2
= 1.1105 + 0.1/2[f(0.1 , 1.1105) , f(0.2 . 1.2426)]
= 1.1105 + 0.1(1.3266)
= 1.2432
y 3
y h / 2 f x , y f x y 2
2 1
1 1 2 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 3
Since y2 y 2
Hence y2 = 1.2432
Step:3
y 1 y h / 2 f x , y f x ,y i1
4
3 2
2 2 3 3
For i = 1 ,
y 1 y h / 2 f x , y f x ,y 0
3 2
2 2 3 3
y30 y 2 h f x 2, y 2
= 1.2432 +(0.1) f(0.2 , 1.2432)
= 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
y 2 y h / 2 f x , y f x ,y 1
3 2
2 2 3 3
= 1.2432+0.1/2[1.4432+(0.3+1.3997)]
= 1.4003
y 3 y h / 2 f x , y f x , y 2
3 2
2 2 3 3
= 1.2432+0.1/2[f(0.2 , 1.2432)+f(0.3 , 1.4003)]
= 1.2432 + 0.1(1.5718)
= 1.4004
y 4 y h / 2 f x , y f x , y 3
3 2
2 2 3 3
= 1.2432 + 0.1/2[1.4432+1.7004]
= 1.2432+(0.1)(1.5718)
= 1.4004
3
Since y y 4
3 3
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
y 1.5 0.325 2 0.3866 2 0.39698 0.48085
1
6
1.8955
Step2:
Here x1=0.1,y1=1.8955 and h=0.1
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 yx
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/dx = x – y, y(0) = 1 is
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 x2 y2 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 y1 of
dy = f(x, y), y(x 0 ) = y0.
dX
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
dx = x2 – y, y(0) = 1
9. Find y(0.1) & y(0.2) using Euler's modified formula given that
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 2xy e x
2. Using R – K method, solve for y at x = 1.2, 1.4 from
dx given y(1) = 0.
x 2 xex
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(𝑥1, 𝑦1), (𝑥2, 𝑦2), … , (𝑥𝑛, 𝑦𝑛) be n observations from an experiment. We are interested
in finding a curve
Closely fitting the given data of size 'n'. Now at 𝑥 = 𝑥1while the observed value of y is 𝑦1, the
expected value of y from the curve (1) is ƒ( 𝑥1). Let us define the residual by
Likewise, the residuals at all other points 𝑥2, ..., 𝑥𝑛are given by
....................(3)
...........................
Some of the residuals 𝑒i′𝑠 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 𝑥i 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).
Suppose that we are given a data set (𝑥1, 𝑦1), (𝑥2, 𝑦2), … , (𝑥𝑛, 𝑦𝑛) of 'n' observations from an
experiment. Say that we are interested in fitting a straight line
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,
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.
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
Given a data set of n observations (𝑥1, 𝑦1), (𝑥2, 𝑦2), … , (𝑥𝑛, 𝑦𝑛) of an experiment .Now we try to
fit a best possible parabola
following the principle of least square. Finding the appropriate parabola amounts to determining
the constants a,b,c that minimize the sum of the squares of the residuals 𝑒′i𝑠, i = 1,2, … 𝑛 given
by
i.e
Similarly 6𝐸 = 0 yields
6𝑏
i.e
Finally 6𝐸 = 0 yields
6𝑐
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.
fit a parabola in the form 𝑦 = 𝑎𝑥2 + 𝑏𝑥 + 𝑐 following the principle of least square.
(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
If R is related to V by the relation R=a+bV+cV2, 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.