0% found this document useful (0 votes)
241 views56 pages

Numerical Technique Laboratory PDF

The document provides instructions for students on how to use MATLAB for a numerical techniques laboratory course. It discusses the different MATLAB windows, creating folders and m-files, MATLAB commands and functions, arithmetic operations, matrices, graphics, and loops and conditionals. Examples are given for plotting functions, matrix operations, using if/elseif statements, and a while loop. Students are instructed to work through exercises on calculations, matrices, and systems of equations using MATLAB.

Uploaded by

RAMEYA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
241 views56 pages

Numerical Technique Laboratory PDF

The document provides instructions for students on how to use MATLAB for a numerical techniques laboratory course. It discusses the different MATLAB windows, creating folders and m-files, MATLAB commands and functions, arithmetic operations, matrices, graphics, and loops and conditionals. Examples are given for plotting functions, matrix operations, using if/elseif statements, and a while loop. Students are instructed to work through exercises on calculations, matrices, and systems of equations using MATLAB.

Uploaded by

RAMEYA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Ahsanullah University of Science and Technology

Department of Electrical and Electronic Engineering

E E
LABORATORY MANUAL
FOR
/E
ELECTRICAL AND ELECTRONIC SESSIONAL COURSE
ST
AU

Student Name :
Student ID :

Course no : EEE 3110


Course Title : Numerical Technique Laboratory

For the students of


Department of Electrical and Electronic Engineering
3rd Year, 1st Semester
Experiment 1: Introduction to MATLAB
(Part 1)

# MATLAB works with three types of windows on your computer screen. These are the
Command window, the Figure window and the Editor window. The Figure window only
pops up whenever you plot something. The Editor window is used for writing and editing
MATLAB programs (called M-files) and can be invoked in Windows from the pull-down
menu after selecting File | New | M-file.

E
# Create a folder of your group name in C drive.

E
# Open MATLAB 6.5. In "current directory'' select your folder.
# From the "file" menu start a new m-file. Always write your program in m-file & save it.
/E
The file/function/variable name must start with a letter and cannot contain space. The
name can be a mix of letters, digits, and underscores. (e.g., vector_A, but not vector-A
(since "-" is a reserved char). must not be longer than 31 characters.
ST

# You can also write any command or program in "command window".


# Function "clear all" removes all variables, globals, functions and MEX links. Write
clear all at the beginning of your m-file.
# Write "clc" in command window to clear the command window, "clg" to clear graphics
window.
AU

# MATLAB is case sensitive. e.g., NAME, Name, name are 3 distinct variables.
# Write "help function_name" in command window after the ">>" sign to see the
description of the function. For example, type "help sin" to know about sin functions in
MATLAB. You can also use help in the menubar.
Explore MATLAB‟s lookfor and help capability by trying the following:

>> lookfor keywords


>> help
>> help plot
>> help ops
>> help arith

Special Characters:

2
There are a number of special reserved characters used in MATLAB for various purposes.
Some of these are used as arithmetic operators, namely, +, -, *, / and \. While others
perform a multitude of purposes:
 % -- anything after % (and until end of line) is treated as
comments, e.g.,

>> x = 1:2:9; % x = [1 3 5 7 9];

 ; -- delimits statements; suppresses screen output, e.g.,

>> x = 1:2:9; y = 2:10; % two statements on the same line

 ... -- statement continuation, e.g.,

E
>> x = [ 1 3 5 ...
7 9]; % x = [1 3 5 7 9] splitted into 2 lines

E
 : -- range delimiter, e.g.,
/E
>> x = [1:2:9]; % x=[1,3,5,7,9]
 ' -- matrix transposition, e.g.,

>> x = [1:2:9]'; % x changed from row vector to column vector


ST

If the vector/matrix is complex, “ ’ ” results in complex conjugation and


matrix transposition.

 , -- command delimiter, e.g.,


AU

>> x = [1:2:9], y = [1:9] % two statements on the same line

 . -- precede an arithmetic operator to perform an elemental


operation, instead of matrix operation, e.g.,

>> x = 3:3:9

x=

3 6 9

>> y = 3*ones(3,1)'

y=

3 3 3

3
>> z =x./y

z=

1 2 3

 * -- "wild card", e.g.,

>> clear A* % clears all variables that start with A.


Note that many of these characters have multiple functionalities (function overloading)
depending on the context, e.g., "*" is used for scalar multiply, matrix multiply and "wild
card" as seen above.

Arithmetic Operations & Built in functions:

E
#Example1
Find y  exp(52 / 3 pi) .

E
Solution: In m-file write the following command
/E
clear all;
a=5^2;
b=3*pi;
ST

y=exp(a/b);
disp(y)

#Save the file and “run” the program from “Debug” menu. Type “y” in command
window and press “enter”.
AU

# Remove “;” from all the lines and run the program.
# Write each line in command window and press “enter” after each line.
# Variable names are assigned to expressions by using equal sign. For example, a=5^2;
here “a” is the variable that store the value of 5^2 or 25.
# See the list of built in functions from “help” menu. Some built in functions are
abs() cos() sin() exp() log() real() sqrt() floor() ceil()

Matrices:

# Write A= [1 2 3; 4 5 6; 7 6 3] in command window and press “enter”. It is a 33 matrix.


# Write A(1,3) in command window to view the 3rd element in 1st row. The first
parameter within bracket denotes row and the second parameter denotes column.
# Z=zeros(2,3) creates a 23 matrix of zeros. Similarly ones(), eye() create special types
of matrices.
# Write A=0:0.3:3 in command window. 0 is the starting value, 3 is the end value and 0.3
is the step value.
# Write “help size”, “help prod” and “help length” in command window.

4
Matrix Operations:
# All arithmetic operations can be performed on matrices.
# Operations can be performed on each element or on whole matrix. For example,

>> x = 3:3:9

>> y = 3*ones(3,1)'

>> z =x./y

# Some operations are performed on square matrices only.

E
# + - * / ^ (algebraic/matrix definitions)

E
.+ .- .* ./ .^ (element by element operation)
Additionally,
/E
" ' " performs matrix transposition; when applied to a complex matrix, it includes
elemental conjugations followed by a matrix transposition
\ and .\ perform matrix and elemental left division
ST

Report:
AU

#Exercise 1.
Find the value of y  ln(sinh(exp(543 / 6* pi)))

# Exercise 2:
Find the size, and length of following matrices
A=[1 2 3; 4 5 6;7 6 54; 65 23 45]
B=7:1:13.5
# Write A(1:2,2:3) in command window. Write A([1 2],[2 3]). These are different ways
to select a submatrix of a matrix.
#A(1,1)=sin(5); assign a new value to an element of A.

#Exercise 3.
A=[2 3; 4 5]; B=[3 4; 6 7];
Find A+B, A*B, A.*B,A/B,A\B, A.^2,A./B

# Exercise 4 .
Define the matrices

5
A=[17 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
B=[ 2 3 4 5 ; 6 7 8 9 ; 10 11 12 13 ; 14 15 16 17 ]
C=[ 1 2 3 ; 4 5 6 ; 7 8 9 ]
y=[ 4 3 2 1 ]'
Note the transpose ' on the y-vector which makes y a column vector.
a) Compute AB and BA. Is matrix multiplication commutative?
b) Compute AC. Why do you get an error message?
c) Solve the following system of equations:
17x1+2x2+3x3+4x4 = 4
5x1+6x2+7x3+8x4 = 3
9x1+10x2+11x3+12x4 = 2
13x1+14x2+15x3+16x4 = 1

E E
/E
ST
AU

6
Experiment 2: Applications of MATLAB

Graphics:

# MATLAB can produce 2 and 3 dimensional plots.

MATLAB is an interactive environment in which you can program as well as visualize


your computations. It includes a set of high-level graphical functions for:

E
Line plots(plot, plot3, polar)
 Bar graphs(bar, barh, bar3, bar3h, hist, rose, pie, pie3)
 Surface plots(surf, surfc)

E
 Mesh plots(mesh, meshc, meshgrid)
 Contour plots(contour, contourc, contourf)
/E
 Animation(moviein, movie)

#Example 2.
ST

x=0:0.1:pi;
y=cos(x);
plot(y);
plot(x,cos(x),‟r‟);
AU

plot(x,y,x,y.^2);

#Example 3.

x=0:0.1:pi;
y=cos(x);
plot(y);
hold on
plot(x,cos(x),‟r‟);

#Example 4.

x=linspace(0,7);
y=exp(x);
subplot(2,1,1), plot(x,y);
subplot(2,1,2), semilogy(x,y);

# Example 5.

7
x = magic(3);
bar(x);
grid

Loops & Conditionals:

#MATLAB has the following flow control constructs:


• if statements
• switch statements
• for loops
• while loops
• break statements

The if, for, switch and while statements need to terminate with an end statement.

E
Example:

E
#Example 6. IF:

x=-3;
/E
if x>0
a=10;
elseif x<0
ST

a=11;
elseif x= = 0
a=12;
else
a=14;
AU

end

What is the value of „a‟ after execution of the above code?

#Example 7. WHILE:

x=-10;
while x<0
x=x+1;
end

What is the value of x after execution of the above loop?

#Example 8. FOR loop:

8
x=0;
for i=1:10
x=x+1;
end

What is the value of x after execution of the above loop?

Defining matrices via the vector technique


Using the for loop in MATLAB is relatively expensive. It is much more efficient to
perform the same task using the vector method. For example, the following task
for j=1:n
for i=1:m

E
A(i,j) = B(i,j) + C(i,j);

E
end
end
/E
can be more compactly and efficiently represented (and computed) by the vector method
as follows:
ST

A(1:m,1:n) = B(1:m,1:n) + C(1:m,1:n);


If the matrices are all of the same size (as is the case here), then the above can be more
succinctly written as
AU

A = B + C;
For sufficiently large matrix operations, this latter method is vastly superior in
performance.

#Example 9. BREAK:

The break statement lets you exit early from a for or a while loop:

x=-10;
while x<0
x=x+2;
if x = = -2
break;
end
end

9
What is the value of x after execution of the above loop?
Relational Operators

Symbol Meaning
<= Lessthanequal
< Less than
>= Greater than equal
> Greater than
== Equal
˜= Not equal

Logical Operators
Symbol Meaning
& AND

E
| OR
˜ NOT

Defining functions:

E
/E
# In MATLAB there is scope for user-defined functions.
Suggestion: Since MATLAB distinguishes one function from the next by their file names,
name files the same as function names to avoid confusion. Use only lowercase letter to be
ST

consistent with MATLAB's convention.


# To define a function, start a new M-file
The first line of M-file should be
function variable_name=function_name(parameters);
AU

variable_name is the name of variable whose value will be returned.


function_name is user defined according to the rules stated previously.

#Example 10:

function y=cal_pow(x);

y=1+x^2;

end

# Save this function as cal_pow.


# Start another new M-file .This will be our main file.
# Write the following commands and run the file:

10
clear all;
x=0:1:3;
t=length(x);

for i=1:t
val(i)=cal_pow(x(i));
end

plot(x,val);

Report:

#Exercise1.
Plot the following functions in the same window y1=sin x, y2=sin 2x, y3=sin 3x, y4=sin

E
4x where x varies from 0 to pi.

# Exercise 2.

E
Write a program to compute the variance of an array x . The variance  is defined to
/E
be:
2
1 N
   ( xi  x )
N i 1
ST

where x is the average of the array x .

For x , use all the integers from 1 to 1000.


AU

# Exercise 3 .
Solve the following circuit to find i1, i2, and i3.

11
R1 R2
1o hm i2 2o hm

R3
i1
V1
V2 3o hm
7V dc
6V dc

R5
i3
R4 1o hm

2o hm

Ans: i1= 3 amp, i2= 2 amp, i3= 3 amp.

E E
/E
ST
AU

12
Experiment 3: Curve Fitting

Introduction:

Data is often given for discrete values along a continuum. However we may require
estimates at points between the discrete values. Then we have to fit curves to such data to
obtain intermediate estimates. In addition, we may require a simplified version of a
complicated function. One way to do this is to compute values of the function at a
number of discrete values along the range of interest. Then a simpler function may be
derived to fit these values. Both of these applications are known as curve fitting.

E
There are two general approaches of curve fitting that are distinguished from each other
on the basis of the amount of error associated with the data. First, where the data exhibits

E
a significant degree of error, the strategy is to derive a single curve that represents the
general trend of the data. Because any individual data may be incorrect, we make no
effort to intersect every point. Rather, the curve is designed to follow the pattern of the
/E
points taken as a group. One approach of this nature is called least squares regression.

Second, where the data is known to be very precise, the basic approach is to fit a curve
ST

that passes directly through each of the points. The estimation of values between well
known discrete points from the fitted exact curve is called interpolation.
AU

13
E E
Figure 1: (a) Least squares linear regression (b) linear interpolation (c) curvilinear
/E
interpolation
ST

Least squares Regression:

Where substantial error is associated with data, polynomial interpolation is inappropriate


and may yield unsatisfactory results when used to predict intermediate values. A more
AU

appropriate strategy for such cases is to derive an approximating function that fits the
shape or general trend of the data without necessarily matching the individual points.
Now some criterion mush be devised to establish a basis for the fit. One way to do this is
to derive a curve that minimizes the discrepancy between the data points and the curve.
A technique for accomplishing this objective is called least squares regression, where the
goal is to minimize the sum of the square errors between the data points and the curve.
Now depending on whether we want to fit a straight line or other higher order
polynomial, regression may be linear or polynomial. They are described below.

Linear regression:

The simplest example of least squares regression is fitting a straight line to a set of paired
observations: (x1, y1), (x2, y2), , , ,(xn, yn). The mathematical expression for straight line
is
ym=a0+a1x

14
Where a0 and a1 are coefficients representing the intercept and slope and ym is the model
value. If y0 is the observed value and e is error or residual between the model and
observation then

e=y0-ym=y0 - a0 - a1x

Now we need some criteria such that the error e is minimum and also we can arrive at a unique
solution (for this case a unique straight line). One such strategy is to minimize the sum of the
square errors. So sum of square errors

n n n
Sr   ei2  ( yi ,observed  yi ,mod el )2  ( yi  a0  a1 xi )2 ……………………………..1
i 1 i 1 i 1

To determine the values of a0 and a1, equation (1) is differentiated with respect to each
coefficient.

E
Sr
 2 ( yi  a0  a1 xi )

E
a0
Sr
 2 ( yi  a0  a1 xi )xi
/E
a1

Setting these derivatives equal to zero will result in a minimum Sr. If this is done, the
ST

equations can be expressed as

0   yi   a0   a1 xi
0   yi xi   a0 xi   a1 xi2
AU

Now realizing that a0  na0 , we can express the above equations as a set of two
simultaneous linear equations with two unknowns a0 and a1.

na0  ( xi )a1   yi

( xi )a0  ( xi2 )a1   xi yi

from where

n xi yi   xi  yi
a1 
n xi2  ( xi )2
a0  y  a1 x

Where y and x are the means of y and x respectively

15
Example 1:
Fit a straight line to the x and y values of table 1

Table 1:
x y
1 0.5
2 2.5
3 2.0
4 4.0
5 3.5
6 6.0
7 5.5

Ans: a0=0.071142857, a1=0.83928

E
%program for fitting a straight line

E
%entering no. of observed points
n=input('How many points ');
/E
%taking input
for i=1:n
x(i)=input('');
ST

y(i)=input('');
end

%calculating coefficients
sumx=0;
AU

sumy=0;
sumxy=0;
sumxsq=0;
for i=1:n
sumx=sumx+x(i);
sumy=sumy+y(i);
sumxy=sumxy+x(i)*y(i);
sumxsq=sumxsq+x(i)^2;
end

format long ;

%calculating a1 and a0
a1=(n*sumxy-sumx*sumy)/(n*sumxsq-sumx^2)
a0=sumy/n-a1*sumx/n

%plotting observed data

16
plot(x,y,'o')
hold on;

%plotting fitted data


ym=a0+a1.*x;
plot(x,ym);

Polynomial Regression:

In some cases, we have some engineering data that cannot be properly represented by a straight
line. We can fit a polynomial to these data using polynomial regression.

E E
/E
ST
AU

Figure 2: (a) Data that is ill-suited for linear least squares regression (b) indication that a
parabola is preferable
The least squares procedure can be readily extended to fit the data to a higher order polynomial.
For example, we want to fit a second order polynomial

ym=a0 + a1x+ a2x2

For this case the sum of the squares of residuals is

n
Sr   ( yi  a0  a1 xi  a2 xi2 )2 ……………………………2
i 1

Taking derivative of equation (2) with respect to unknown coefficients a0, a1 and a2

17
Sr
 2 ( yi  a0  a1 xi  a2 xi2 )
a0
Sr
 2 xi ( yi  a0  a1 xi  a2 xi2 )
a1
Sr
 2 xi2 ( yi  a0  a1 xi  a2 xi2 )
a2

These equations can be set equal to zero and rearranged to develop the following set of
normal equations:

na0  ( xi )a1  ( xi2 )a2   y i


( xi )a0  ( xi2 )a1  ( xi3 )a2   xi yi

E
( xi2 )a0  ( xi3 )a1  ( xi4 )a2   xi2 yi

E
Now a0, a1 and a2 can be calculated using matrix inversion.
/E
Linearization of Nonlinear Relationships:
ST

Linear regression is a powerful technique for fitting a best line to data. However it is
dependent on the fact that the relationship between the dependent and independent
AU

variables should be linear. This is always not the case. In those cases, we use polynomial
regression. In some cases, transformation can be used to express the data in a form that is
compatible with linear regression.

One example is the exponential model

y  a1eb1x …………………………..3

Where a1 and b1 are constants.

Another example of a nonlinear model is the simple power equation

y  a2 xb2 …………………………..4

Where a2 and b2 are constants.

18
Nonlinear regression techniques are available to fit these equations to experimental data directly.
However, a simpler alternative is to use mathematical manipulations to transform the equations
into linear forms. Then simple linear regression can be used to fit the equations to data.

For example equation (3) can be linearized by taking its normal logarithm to yield

ln y  ln a1  b1 x

Thus a plot of lny vs x will yield a straight line with a slope of b1 and an intercept of lna1

Equation (4) can be linearized by taking its base10 logarithm to give

E
log y  b2 log x  log a2

E
Thus a plot of logy vs logx will yield a straight line with a slope of b2 and an intercept of
loga2
/E
ST

Report:

# Exercise 1:
AU

Fit a second order polynomial to the data given in table 2

Table 2
x y
0 2.1
1 7.7
2 13.6
3 27.2
4 40.9
5 61.1

Ans: a0=2.47857, a1=2.35929, a2=1.86071

19
# Exercise 2:
Fit the equation y  a2 xb2 to the data given in Table 3

Table 3
x y
1 0.5
2 1.7
3 3.4
4 5.7
5 8.4

Ans: a2=0.5, b2=1.75

E E
Hints: find logx and logy for all points. Using these converted points, using linear
regression find slope b2 and intercept loga2. Then find a2 and b2.
/E
ST
AU

20
Experiment 04: Solution of Simultaneous
Linear Algebraic Equations

Objective
Systems of linear algebraic equations occur often in diverse fields of science and
engineering and is an important area of study. In this experiment we will be concerned
with the different techniques of finding solution of a set of n linear algebraic equations in
n unknowns.

Concept of linear equations and their solution

E
A set of linear algebraic equations looks like this:

a11 x1  a12 x2  ...a1N xN  b1

… … … …
E
a21 x1  a22 x2  ...a2 N xN  b2 (1)
/E
aM 1 x1  aM 2 x2  ...aMN xN  bM

Here the N unknowns xj , j = 1, 2, . . .,N are related by M equations. The coefficients aij
ST

with i = 1, 2, . . .,M and j = 1, 2, . . .,N are known numbers, as are the right-hand side
quantities bi, i = 1, 2, . . .,M.

Existence of solution
AU

If N = M then there are as many equations as unknowns, and there is a good chance of
solving for a unique solution set of xj‟s. Analytically, there can fail to be a unique
solution if one or more of the M equations is a linear combination of the others (This
condition is called row degeneracy), or if all equations contain certain variables only in
exactly the same linear combination(This is called column degeneracy). (For square
matrices, a row degeneracy implies a column degeneracy, and vice versa.) A set of
equations that is degenerate is called singular.
Numerically, at least two additional things can go wrong:
• While not exact linear combinations of each other, some of the equations may be so
close to linearly dependent that round off errors in the machine renders them linearly
dependent at some stage in the solution process. In this case your numerical procedure
will fail, and it can tell you that it has failed.
• Accumulated round off errors in the solution process can swamp the true solution. This
problem particularly emerges if N is too large. The numerical procedure does not fail
algorithmically. However, it returns a set of x‟s that are wrong, as can be discovered by
direct substitution back into the original equations. The closer a set of equations is to
being singular, the more likely this is to happen.

21
Matrices
Equation (1) can be written in matrix form as
A·x=b (2)
Here the raised dot denotes matrix multiplication, A is the matrix of coefficients, x is the
column vector of unknowns and b is the right-hand side written as a column vector,

 a11 a12 ... a1N   x1   b1 


a a22 ... a2 N  x  b 
A   21 x 2 b 2
 ... ... ... ...   ..   .. 
     
 aM 1 aM 2 ... aMN   xN  bM 

Finding Solution
There are so many ways to solve this set of equations. Below are some important
methods.

E
(1) Using the backslash and pseudo-inverse operator

E
In MATLAB, the easiest way to determine whether Ax = b has a solution, and to find
such a solution when it does, is to use the backslash operator. Exactly what A \ b returns
/E
is a bit complicated to describe, but if there is a solution to A · x = b, then A \ b returns
one. Warnings: (1) A \ b returns a result in many cases when there is no solution to A · x
= b. (2) A \ b sometimes causes a warning to be issued, even when it returns a solution.
This means that you can't just use the backslash operator: you have to check that what it
ST

returns is a solution. (In any case, it's just good common sense to check numerical
computations as you do them.) In MATLAB this can be done as follows:

Using backslash operator:


AU

x = A\b;

You can also use the pseudo-inverse operator:


x=pinv(A)*b; % it is also guaranteed to solve Ax = b, if Ax = b has a solution.

As with the backslash operator, you have to check the result.

(2) Using Gauss-Jordan Elimination and Pivoting

To illustrate the method let us consider three equations with three unknowns:

a11 x1  a12 x2  a13 x3  a14 (A)


a21 x1  a22 x2  a23 x3  a24 (B)
a31 x1  a32 x2  a33 x3  a34 (C)
Here the quantities bi, i = 1, 2, . . .,M‟s are replaced by aiN+1, where i=1,2, ….M for
simplicity of understanding the algorithm.

22
The First Step is to eliminate the first term from Equations (B) and (C). (Dividing (A) by
a11 and multiplying by a21 and subtracting from (B) eliminates x1 from (B) as shown
below)
a a a a
(a21  11 a21 ) x1  (a22  12 a21 ) x2  (a23  13 a21 ) x3  (a24  14 a21 )
a11 a11 a11 a11
a
Let, 21  k2 , then
a11
(a21  k2 a11 ) x1  (a22  k2 a12 ) x2  (a23  k2a13 ) x3  (a24  k2a14 )
a
Similarly multiplying equation (A) by 31  k3 and subtracting from (C), we get
a11
(a31  k3a11 ) x1  (a32  k3a12 ) x2  (a33  k3a13 ) x3  (a34  k3a14 )
Observe that (a21  k2 a11 ) and (a31  k3a11 ) are both zero.
In the steps above it is assumed that a11 is not zero. This case will be considered later in
this experiment.

E
The above elimination procedure is called triangularization.

E
Algorithm for triangularizing n equations in n unknowns:
/E
1 for i  1 to n and j  1 to (n  1) in steps of 1 do read aij endfor
2 for k  1 to (n 1) in steps of 1 do
for i  (k  1) to n in steps of 1 do
ST

3
4 u  aik / akk
5 for j  k to (n  1) in steps of 1 do
6 aij  aij  uakj endfor
AU

endfor
endfor
The reduced equations are:
a11 x1  a12 x2  a13 x3  a14
a22 x2  a23 x3  a24
a32 x2  a33 x3  a34
The next step is to eliminate a32 from the third equation. This is done by multiplying
second equation by u  a32 / a22 and subtracting the resulting equation from the third. So,
same algorithm can be used.
Finally the equations will take the form:
a11 x1  a12 x2  a13 x3  a14
a22 x2  a23 x3  a24
a33 x3  a34
The above set of equations are said to be in triangular (Upper) form.

23
From the above upper triangular form of equations, the values of unknowns can be
obtained by back substitution as follows:
x3  a34 / a33
x2  (a24  a23 x3 ) / a22

x2  (a14  a12 x2  a13 x3 ) / a11

Algorithmically, the back substitution for n unknowns is shown below:

1 xn  an ( n1) / ann

2 for i  (n 1) to 1 in step of -1 do

3 sum  0

for j  (i  1) to n in steps of 1 do

E
4

5 sum  sum  aij x j endfor

E
xi  (ai ( n1)  sum) / aii
/E
endfor
ST

Pivoting
AU

In the triangularization algorithm we have used,

u  aik / akk

Here it is assumed that akk is not zero. If it happens to be zero or nearly zero, the
algorithm will lead to no results or meaningless results. If any of the akk is small it would
be necessary to reorder the equations. It is noted that the value of akk would be modified
during the elimination process and there is no way of predicting their values at the start of
the procedure.

The elements akk are called pivot elements. In the elimination procedure the pivot should
not be zero or a small number. In fact for maximum precision the pivot element should be
the largest in absolute value of all the elements below it in its column, i.e. akk should be
picked up as the maximum of all amk where, m  k

24
So, during the Gauss elimination, amk elements should be searched and the equation with
the maximum value of amk should be interchanged with the current position. For example
if during elimination we have the following situation:

x1  2 x2  3x3  4
0.3x2  4 x3  5
8 x2  3x3  6

As 8  0.3, 2nd and 3rd equations should be interchanged to yield:

x1  2 x2  3x3  4
8 x2  3x3  6
0.3x2  4 x3  5

E
It should be noted that interchange of equations does not affect the solution.
The algorithm for picking the largest element as the pivot and interchanging the

E
equations is called pivotal condensation.
/E
Algorithm for pivotal condensation
ST

1 max  akk

2 pk
AU

3 for m  (k  1) to n in steps of 1 do

4 if ( amk  max) then

5 max  amk

6 pm

7 endif

endfor

8 if ( p ~  k )

9 for q  k to (n  1) in steps of 1 do

10 temp  akq

25
11 akq  a pq

12 a pq  temp

endfor

endif

(3) Using Gauss-Seidel Iterative Method


There are several iterative methods for the solution of linear systems. One of the efficient
iterative methods is the Gauss-Seidel method.
Let us consider the system of equations:

E
4 x1  x2  x3  7
4 x1  8 x2  x3  21

E
2 x1  x2  5 x3  15
/E
The Gauss-Seidel iterative process is suggested by the following equations:

7  x2k  x3k
x1k 1 
4
ST

k 1 21  4 x1k 1  x3k
x2 
8
15  2 x1k 1  x2k 1
x3k 1 
5
AU

The very first iteration, that is x20 , x30 ,.....xn0 (for n equations) are set equal to zero and x11
is calculated. The main point of Gauss-Seidel iterative process to observe is that always
the latest approximations for the values of variables are used in an iteration step.

It is to be noted that in some cases the iteration diverges rather than it converges. Both the
divergence and convergence can occur even with the same set of equations but with the
change in the order. The sufficient condition for the Gauss-Seidel iteration to converge is
stated below.
The Gauss-Seidel iteration for the solution will converge (if there is any solution) if the
matrix A (as defined previously) is strictly diagonally dominant matrix.

A matrix A of dimension N  N is said to be strictly diagonally dominant provided that

26
N
akk   akj for k  1, 2,...N
j 1
j k

Report:

# Exercise 1.
Given the simultaneous equations shown below (i) triangularize them (ii) use back
substitution to solve for x1 , x2 , x3 .

2 x1  3x2  5 x3  23
3x1  4 x2  x3  14
6 x1  7 x2  2 x3  26

For generalization, you will have to write a program for triangularizing n equations in n

E
unknowns with back substitution.

#Exercise 2.

E
/E
Modify the MATLAB program written in exercise 1 to include pivotal condensation.
ST

#Exercise 3.
Try to solve the following systems of equations (i) Gauss-Jordan elimination (ii) Gauss-
Jordan elimination with pivoting
AU

2 x1  4 x2  6 x3  4 x1  x2  6 x3  7
(A) x1  5 x2  3x3  10 (B)  x1  2 x2  9 x3  2
x1  3x2  2 x3  5 x1  2 x2  3x3  10

4 x1  8 x2  4 x3  8
x1  5 x2  4 x3  3x4  4
(C)
x1  4 x2  7 x3  2 x4  10
x1  3x2  2 x4  4

#Exercise 4.
Solve the following equations using Gauss-Seidel iteration process:

27
8 x1  3x2  10 4 x  y  15
(A) (B)
 x1  4 x2  6 x  5y  9

5 x1  x2  x3  10 2 x  8 y  z  11
(C) 2 x1  8 x2  x3  11 (D) 5 x  y  z  10
 x1  x2  4 x3  3 x  y  4z  3

E
E
/E
ST
AU

28
Experiment 5: Solutions to Non-Linear Equations
(Bisection Method & False-Position Method)

Bisection method:

The Bisection method is one of the simplest procedures for finding root of a function in a
given interval.
The procedure is straightforward. The approximate location of the root is first determined
by finding two values that bracket the root (a root is bracketed or enclosed if the function
changes sign at the endpoints). Based on these a third value is calculated which is closer

E
to the root than the original two value. A check is made to see if the new value is a root.
Otherwise a new pair of bracket is generated from the three values, and the procedure is

E
repeated. /E
ST
AU

Consider a function d ( x) and let there be two values of x , xlow and xup ( xup > xlow ),
bracketing a root of d ( x) .

Steps:

1. The first step is to use the brackets xlow and xup to generate a third value that is
closer to the root. This new point is calculated as the mid-point between xlow and,
xlow  xup
namely xmid  . The method therefore gets its name from this bisecting
2
of two values. It is also known as interval halving method.

29
2. Test whether xmid is a root of d ( x) by evaluating the function at xmid .
3. If xmid is not a root,
a. If d ( xlow ) and d ( xmid ) have opposite signs i.e. d ( xlow ) . d ( xmid ) <0,
root is in left half of interval.
b. If d ( xlow ) and d ( xmid ) have same signs i.e. d ( xlow ) . d ( xmid ) >0,
root is in right half of interval.
4. Continue subdividing until interval width has been reduced to a size 
where = selected x tolerance.

Algorithm: Bisection Method

Input xLower, xUpper, xTol


yLower = f(xLower) (* invokes fcn definition *)
xMid = (xLower + xUpper)/2.0
yMid = f(xMid)

E
iters = 0 (* count number of iterations *)
While ( (xUpper - xLower)/2.0 > xTol )

E
iters = iters + 1
if( yLower * yMid > 0.0) Then xLower = xMid
Else xUpper = xMid
/E
Endofif
xMid = (xLower + xUpper)/2.0
yMid = f(xMid)
ST

Endofwhile
Return xMid, yMid, iters (* xMid = approx to root *)

Note: For a given x tolerance (epsilon), we can calculate the number of iterations
AU

directly. The number of divisions of the original interval is the smallest value of n
xup  xlow xup  xlow
that satisfies:   or 2 n

2n 
 xup  xlow 
Thus n log 2  .
  
In our previous example, xlow = -1, xup =0 and = selected x tolerance = 10 4 .
So we have n  14 .

False-Position Method (Regula Falsi)

A shortcoming of the bisection method is that, in dividing the interval from xlow to
xup into equal halves, no account is taken of the magnitude of f ( xlow ) and f ( xup ) . For
example, if f ( xlow ) is much closer to zero than f ( xup ) , it is likely that the root is closer
to xlow than to xup . An alternative method that exploits this graphical insight is to join
f ( xlow ) and f ( xup ) by a straight line. The intersection of this line with the x axis

30
represents an improved estimate of the root. The fact that the replacement of the curve by
a straight line gives the false position of the root is the origin of the name, method of
false position, or in Latin, Regula Falsi. It is also called the Linear Interpolation Method.

E E
Using similar triangles, the intersection of the straight line with the x axis can be
estimated as
/E
f ( xlow ) f ( xup )

x  xlow x  xup
ST

f ( xup )( xlow  xup )


That is x  xup 
f ( xlow )  f ( xup )
AU

This is the False Position formulae. The value of x then replaces whichever of the two
initial guesses, xlow or xup , yields a function value with the same sign as f (x) . In this
way, the values of xlow and xup always bracket the true root. The process is repeated until
the root is estimated adequately.

Report:

#Exercise 1.
Find the real root of the equation d ( x)  x5  x  1 using Bisection Method. xlow = -1,
xup =0 and = selected x tolerance = 10 4 .

#Exercise 2.
Find the root of the equation d ( x)  x5  x  1 using False Position Method. xlow = -1,
xup =0 and = selected x tolerance = 10 4 . (Develop the algorithm by yourself. It is very
similar to Bisection Method).

31
Experiment 6: Solutions to Non-Linear Equations
(Newton Raphson Method & Secant Method)

Newton Raphson Method:

If f (x) , f (x) and f (x) are continuous near a root x , then this extra information
regarding the nature of f (x) can be used to develop algorithms that will produce
sequences {x k } that converge faster to x than either the bisection or false position
method. The Newton-Raphson (or simply Newton's) method is one of the most useful
and best-known algorithms that relies on the continuity of f (x) and f (x) .
The attempt is to locate root by repeatedly approximating f (x) with a linear function at
each step. If the initial guess at the root is x k , a tangent can be extended from the point
xk , f ( xk ). The point where this tangent crosses the x axis usually represents an

E
improved estimate of the root.

f (x)

E
/E
Slope = f ( xk )
ST

f ( xk )
AU

0 x k 1 xk x

32
The Newton-Raphson method can be derived on the basis of this geometrical
interpretation. As in the figure, the first derivative at x is equivalent to the slope:
f ( xk )  0
f ( x k ) 
x k  x k 1

which can be rearranged to yield

f ( xk )
x k 1  x k 
f ( xk )

E
which is called the Newton Raphson formulae.

E
So the Newton-Raphson Algorithm actually consists of the following steps:
1. Start with an initial guess x 0 and an x-tolerance .
/E f ( xk )
2. Calculate x k 1  x k  k  0,1,2,
f ( xk )

Algorithm - Newton’s Method


ST

Input x0, xTol


iters = 1
dx = -f(x0)/fDeriv(x0) (* fcns f and fDeriv *)
root = x0 + dx
AU

While (Abs(dx) > xTol)


dx = -f(root)/fDeriv(root)
root = root + dx
iters = iters + 1
End of while
Return root, iters

The Secant Method:


The Newton-Raphson algorithm requires two functions evaluations per iteration,
f ( xk ) and f ( xk ) . Historically, the calculation of a derivative could involve
considerable effort. Moreover, many functions have non-elementary forms (integrals,
sums etc.), and it is desirable to have a method for finding a root that does not depend on
the computation of a derivative. The secant method does not need a formula for the
derivative and it can be coded so that only one new function evaluation is required per
iteration.

33
The formula for the secant method is the same one that was used in the Regula Falsi
method, except that the logical decisions regarding how to define each succeeding term
are different.
In the Secant method, the derivative can be approximated by a backward finite divided
difference, as in the figure,

f ( xk 1 )  f ( xk )
f ( xk ) 
xk 1  xk

f (x)

E
f ( xk )

E
/E
ST

f ( xk 1 )
AU

x k 1 xk x

34
Using Newton-Raphson method,
f ( xk )
x k 1  x k 
f ( xk )
Substituting f ( xk ) ,
f ( xk )( xk 1  xk )
xk 1  xk 
f ( xk 1 )  f ( xk )

Notice that the approach requires initial estimates of x .

Algorithm - Secant Method


Input xk, xkMinus1, xTol, maxiters
iters = 1
yk =f(xk) (* invokes function f *)
ykMinus1 = f(xkMinus1)
root = (xkMinus1*yk - xk*ykMinus1)/(yk - ykMinus1)

E
ykPlus1 = f(root)
While( (Abs(root - xk) > xTol) and (iters < maxiters) )

E
xkMinus1 = xk
ykMinus1 = yk
xk = root
/E
yk = ykPlus1
root = (xkMinus1*yk - xk*ykMinus1)/(yk - yk Minus1)
ykPlus1 = f(root)
ST

iters = iters + 1
Endofwhile
Return root, ykPlus1, iters
AU

Report:

# Exercise 3.
Use the Newton Raphson method to estimate the root of f ( x)  e  x  1 , employing an
initial guess of x0  0. The tolerance is = 10 8 .

# Exercise 4.
Find the root of the equation f ( x)  3x  sin( x)  e x , starting values are 0 and 1. The
tolerance limit is 0.0000001.

35
Experiment 7: Interpolation

Introduction:

Forming a polynomial:

A polynomial, p(x) of degree n in MATLAB is stored as a row vector, p, of length n+1.


The components represent the coefficients of the polynomial and are given in the
descending order of the power of x, that is

E
p = [an an-1 ……….. a1 a0]
is interpreted as
p(x) = anxn+ an-1xn-1+ ………..+ a1x+a0

E
In MATLAB the following commands are used to evaluate a polynomial:
/E
polyval, poly, roots, conv etc.
ST

Interpolation:

In the mathematical subfield of numerical analysis, interpolation is a method of


constructing new data points from a discrete set of known data points.
In engineering and science one often has a number of data points, as obtained by
AU

sampling or some experiment, and tries to construct a function which closely fits those
data points. This is called curve fitting. Interpolation is a specific case of curve fitting, in
which the function must go exactly through the data points.
Definition:
Given a sequence of n distinct numbers xk called nodes and for each xk a second number
yk, we are looking for a function f so that

A pair xk,yk is called a data point and f is called the interpolant for the data points.

For example, suppose we have a table like this, which gives some values of an unknown
function f. The data are given in the table:

36
Table 1

x f(x)

0 0

1 0.8415

2 0.9093

3 0.1411

4 -0.7568

5 -0.9589

6 -0.2794

E
The plot can be shown as:
.

E
/E
ST
AU

What value does the function have at, say, x = 2.5? Interpolation answers questions like
this.

Types of interpolation:
A. Linear interpolation
One of the simplest methods is linear interpolation. Consider the above example of
determining f(2.5). We join the data points by linear interpolation and get the following
plot:

37
E
Noe we can get f(2.5). Since 2.5 is midway between 2 and 3, it is reasonable to take f(2.5)
midway between f(2) = 0.9093 and f(3) = 0.1411, which yields 0.5252.

E
Generally, linear interpolation takes two data points, say (xa,ya) and (xb,yb), and the
interpolant is given by
/E
This formula can be interpreted as a weighted mean.
ST

Linear interpolation is quick and easy, but it is not very precise.


B. Polynomial interpolation
AU

Polynomial interpolation is a generalization of linear interpolation. Note that the linear


interpolant is a linear function. We now replace this interpolant by a polynomial of higher
degree.
Consider again the problem given above. The following sixth degree polynomial goes
through all the seven points:

f(x) = − 0.0001521x6 − 0.003130x5 + 0.07321x4 − 0.3577x3 + 0.2255x2 +


0.9038x
Substituting x = 2.5, we find that f(2.5) = 0.5965.
Generally, if we have n data points, there is exactly one polynomial of degree n−1 going
through all the data points. The interpolation error is proportional to the distance between
the data points to the power n.
However, polynomial interpolation also has some disadvantages. Calculating the
interpolating polynomial is relatively very computationally expensive Furthermore,
polynomial interpolation may not be so exact after all, especially at the end points.

38
a. Lagrange Polynomial:
The Lagrange interpolating polynomial is the polynomial P( x) of degree (n  1) that
passes through the n points , , ..., , and is
given by

where

Written explicitly,

E E
/E
When constructing interpolating polynomials, there is a tradeoff between having a better
fit and having a smooth well-behaved fitting function. The more data points that are used
ST

in the interpolation, the higher the degree of the resulting polynomial, and therefore the
greater oscillation it will exhibit between the data points. Therefore, a high-degree
interpolation may be a poor predictor of the function between points, although the
accuracy at the data points will be "perfect."
AU

For points,

Note that the function P( x) passes through the points , as can be seen for the case
,

39
Algorithm for the Lagrange Polynomial: To construct the Lagrange polynomial

of degree n, based on the n+1 points for . The Lagrange


coefficient polynomials for degree n are:

for .
So, for a given x and a set of (N+1) data pairs, (xi, fi), i= 0, 1, . ….. N:
Set SUM=0
DO FOR i=0 to N
Set P=1

E
DO FOR j=0 to N

E
IF j~=i
/E
Set P=P*(x-x(j))/(x(i)-x(j))
End DO(j)
ST

Report:
#Exercise 1:
Construct a polynomial such that C(x)= A(x)*B(x)
AU

Where A(x)= 3x2+2x-4 and B(x)= 2x3-2


Also find the roots of A(x), B(x) and C(x).
#Exercise 2.
Plot the curve corresponding to table1 using linear interpolation.
#Exercise 3.
y  sin( x); x  0 :10; x(i)  0 : 0.25:10; Construct the interpolant y and plot.

#Exercise 4.
Write a MATLAB program implementing Lagrange Polynomial.
#Exercise 5.
Construct a Lagrange interpolating polynomials for the data points given in table 1.

40
Experiment 8: Numerical Differentiation

Introduction:

We are familiar with the analytical method of finding the derivative of a function when
the functional relation between the dependent variable y and the independent variable x is
known. However, in practice, most often functions are defined only by tabulated data, or
the values of y for specified values of x can be found experimentally. Also in some cases,
it is not possible to find the derivative of a function by analytical method. In such cases,

E
the analytical process of differentiation breaks down and some numerical process have to
be invented. The process of calculating the derivatives of a function by means of a set of

E
given values of that function is called numerical differentiation. This process consists in
replacing a complicated or an unknown function by an interpolation polynomial and then
differentiating this polynomial as many times as desired.
/E
ST

Forward Difference Formula:

All numerical differentiation are done by expansion of Taylor series

f ( x)h2 f ( x)h3


AU

f ( x  h)  f ( x)  f ( x)h    ……………..(1)
2 6

From (1)

f ( x  h)  f ( x )
f ( x)   O(h) ……………..(2)
h

Where, O(h) is the truncation error, which consists of terms containing h and higher
order terms of h.

Central Difference Formula (of order O (h2)):

f ( x)h 2 f (c1 )h 3


f ( x  h)  f ( x)  f ( x)h   ....... ….(3)
2 6

41
f ( x)h 2 f (c2 )h 3
f ( x  h)  f ( x)  f ( x)h   ....... …(4)
2 6

Using (3) and (4)

f ( x  h )  f ( x  h)
f ( x)   O(h 2 ) ……….(5)
2h

Where, O(h 2 ) is the truncation error, which consists of terms containing h2 and higher
order terms of h.

Central Difference Formula (of order O (h4)):

Using Taylor series expansion it can be shown that

E
 f ( x  2h)  8 f ( x  h)  8 f ( x  h)  f ( x  2h)
f ( x)   O(h 4 ) ……….(6)

E
12h

Here the truncation error reduces to h4


/E
Richardson’s Extrapolation:
ST

We have seen that

f ( x  h )  f ( x  h)
f ( x)   O( h 2 )
2h
AU

Which can be written as

f ( x  h)  f ( x  h )
f ( x)   Ch2
2h
Or, f ( x)  D0 (h)  Ch2 ……………………………….(7)

If step size is converted to 2h

f ( x)  D0 (2h)  4Ch2 ………………………….……(8)


Using (7) and (8)

4 D0 (h)  D0 (2h)  f 2  8 f1  8 f 1  f 2
f ( x)   ……………….(9)
3 12h

Equation (9) is same as equation (6)

42
The method of obtaining a formula for f ( x) of higher order from a formula of lower
order is called extrapolation. The general formula for Richardson‟s extrapolation is

4k Dk 1 (h)  Dk 1 (2h)
f ( x)  Dk (h)  O(h2 k  2 )   O(h 2 k  2 ) ……(10)
4 1
k

Algorithm for Richardson Approximation:

%Input-f(x) is the input function


% -delta is the tolerance for error
% -toler is the tolerance for relative error
%Output-D is the matrix of approximate derivatives
% -err is the error bound

E
% -relerr is the relative error bound
% -n is the coordinate for best approximation

E
Define
err=1
relerr=1
/E
h=1
j=1
ST

Compute D(1,1)=(f(x+h)-f(x-h))/(2h)

While relerr > toler & err > delta &j <12
AU

Compute
h=h/2;
D(j+1,1)=(f(x+h)-f(x-h))/(2h)

DO For k=1:j
Compute D(j+1,k+1)=D(j+1,k)+ (D(j+1,k)-D(j,k))/((4^k)-1)
END DO(k)

Compute
err=|D(j+1,j+1)-D(j,j)|
relerr==2err/(|D(j+1,j+1)|+|D(j,j)|+eps)
j=j+1
END While

43
Report:

#Exercise 1:
Given f(x) =ex, find f (1) using h=10-1, 10-2,,,, upto 10-10. Find out the error in each case
by comparing the calculated value with exact value (Use forward difference formula).

#Exercise 2:
Given f(x) =ex, find f (1) using h=10-1, 10-2,,,, up to 10-10. Use equation (5). Find out the
error in each case by comparing the calculated value with exact value.

E
#Exercise 3:

E
Given f(x) =sin (cos (1/x)) evaluate f (1 / 2 ) . Start with h =1 and reduce h to 1/10 of
previous step in each step. If Dn+1 is the result in (n+1) th step and Dn is the result in nth
/E
step then continue iteration until |Dn+1-Dn|>=|Dn-Dn-1| or |Dn-Dn-1| <tolerance. Use
equation (6) for finding D.

#Exercise 4:
ST

1 5
Given f(x) =sin (x3-7x2+6x+8) evaluate f ( ) . Use Richardson‟s extrapolation.
2
Approximation should be accurate up to 13 decimal places.
AU

44
Experiment 09: Numerical Integration

Introduction
There are two cases in which engineers and scientists may require the help of numerical
integration technique. (1) Where experimental data is obtained whose integral may be
required and (2) where a closed form formula for integrating a function using calculus is

E
difficult or so complicated as to be almost useless. For example the integral
t3

E
x
(t )   dt.
0 et  1

Since there is no analytic expression for ( x) , numerical integration technique must be


/E
used to obtain approximate values of ( x) .
Formulae for numerical integration called quadrature are based on fitting a polynomial
ST

through a specified set of points (experimental data or function values of the complicated
function) and integrating (finding the area under the fitted polynomial) this
approximating function. Any one of the interpolation polynomials studied earlier may be
used.
AU

Some of the Techniques for Numerical Integration

Trapezoidal Rule
Assume that the values of a function f ( x) are given at x1, x1+h, x1+2h ……x1+nh and it
is required to find the integral of f ( x) between x1 and x1+nh. The simplest technique to
use would be to fit straight lines through f(x1), f(x1+h) ……and to determine the area
under this approximating function as shown in Fig 7.1.

45
f(x)

f3

f4
f2
f1

x
x1 x1+h x1+2h x1+3h x1+4h x1+5h

Fig. 7.1 Illustrating trapezoidal rule

For the first two points we can write:


x1  h
h

x1
f ( x)dx 
2
( f1  f 2 )

E
This is called first-degree Newton-Cotes formula.
From the above figure it is evident that the result of integration between x1 and x1+nh is

E
nothing but the sum of areas of some trapezoids. In equation form this can be written as:
x1  nh
/E n
( fi  fi 1 )
 f ( x)dx   h
x1 i 1 2
ST

The above integration formula is known as Composite Trapezoidal rule.


The composite trapezoidal rule can explicitly be written as:
x1  nh
h
 f ( x)dx  ( f1  2 f 2  2 f3  ......2 f n  f n 1 )
AU

x1
2

Simpson’s 1/3 Rule


This is based on approximating the function f(x) by fitting quadratics through sets of
three points. For only three points it can be written as:
x1  2 h
h

x1
f ( x)dx  ( f1  4 f 2  f3 )
3

This is called second-degree Newton-Cotes formula.


It is evident that the result of integration between x1 and x1+nh can be written as

46
x1  nh
h
 f ( x)dx  
i 1,3,5,..., n 1 3
( fi  4 fi 1  fi  2 )
x1

h
 ( f1  4 f 2  2 f3  4 f 4  2 f5  4 f 6  ... 4 f n  f n 1 )
3
In using the above formula it is implied that f is known at an odd number of points (n+1
is odd, where n is the no. of subintervals).

Simpson’s 3/8 Rule


This is based on approximating the function f(x) by fitting cubic interpolating polynomial
through sets of four points. For only four points it can be written as:
x1 3 h
3h
 f ( x)dx  ( f1  3 f 2  3 f3  f 4 )

E
x1
8

E
This is called third-degree Newton-Cotes formula.
It is evident that the result of integration between x1 and x1+nh can be written as
/E
x1  nh
h
 f ( x)dx  
i 1,4,7,..., n  2 3
( f i  3 f i 1  3 f i  2  f i 3 )
ST
x1

3h
 ( f1  3 f 2  3 f3  2 f 4  3 f5  3 f 6  2 f 7 + ...  2 f n 2  3 f n 1  3 f n  f n 1 )
8
In using the above formula it is implied that f is known at (n+1) points where n is
AU

divisible by 3.
An algorithm for integrating a tabulated function using composite trapezoidal rule:
Remarks: f1, f2,………, fn+1 are the tabulated values at x1, x1+h,………x1+nh (n+1 points)

1 Read h

2 for i  1 to n  1 Read fi endfor

3 sum  ( f1  f n1 ) / 2

4 for j  2 to n do

5 sum  sum  f j

endfor

47
6 int egral  h . sum

7 write int egral

stop

An algorithm for integrating a known function using composite trapezoidal rule:

If f(x) is given as a closed form function such as f ( x)  e x cos x and we are asked to
integrate it from x1 to x2, we should decide first what h should be. Depending on the
value of h we will have to evaluate the value of f(x) inside the program for x=x1+nh
where n=0,1, 2,….n and n  ( x2  x1 ) / h .

1 h  ( x2  x1 ) / n

E
2 x  x1

sum  f ( x)

E
3

4 for i  2 to n do
/E
5 x  xh

6 sum  sum  2 f ( x)
ST

endfor

7 x  x2
AU

8 sum  sum  f ( x)

h
9 int egral  . sum
2
10 write int egral

stop

Adaptive Integration
When f(x) is a known function we can choose the value for h arbitrarily. The problem is
that we do not know a priori what value to choose for h to attain a desired accuracy (for
example, for an arbitrary h sharp picks of the function might be missed). To overcome
this problem, we can start with two subintervals, h  h1  ( x2  x1 ) / 2 and apply either
trapezoidal or Simpson‟s 1/3 rule. Then we let h2  h1 / 2 and apply the formula again,

48
now with four subintervals and the results are compared. If the new value is sufficiently
close, the process is terminated. If the 2nd result is not close enough to the first, h is
halved again and the procedure is repeated. This is continued until the last result is close
enough to its predecessor. This form of numerical integration is termed as adaptive
integration.
The no. of computations can be reduced because when h is halved, all of the old points at
which the function was evaluated appear in the new computation and thus repeating
evaluation can be avoided. This is illustrated below.

k=1

k=2

E
k=3

k=4
E
/E
o = New points
ST

 = Old points
AU

An algorithm for adaptive integration of a known function using trapezoidal rule:

1 Read x1 , x2 , e Remark: The allowed error in integral is e

2 h  x2  x1

3 S1  ( f ( x1 )  f ( x2 )) / 2

4 I1  h . S1

5 i 1

Repeat

6 x  x1  h / 2

7 for j  1 to i do

49
8 S1  S1  f ( x)

9 x  xh

endfor
10 i  2i

11 h h/2

12 I 0  I1

13 I1  h . S1

14 until I1  I 0  e . I1

15 write I1 , h, i

E
Stop

E
/E
Report:
ST

# Exercise1.
Integrate the function tabulated in Table 7.1 over the interval from x=1.6 to x=3.8 using
composite trapezoidal rule with (a) h=0.2, (b) h=0.4 and (c) h=0.6
AU

Table 7.1

X f(x) X f(x)

1.6 4.953 2.8 16.445


1.8 6.050 3.0 20.086
2.0 7.389 3.2 24.533
2.2 9.025 3.4 29.964
2.4 11.023 3.6 36.598
2.6 13.468 3.8 44.701

50
The data in Table 7.1 are for f ( x)  e x . Find the true value of the integral and compare
this with those found in (a), (b) and (c).

# Exercise 2.
(a) Integrate the function tabulated in Table 7.1 over the interval from x=1.6 to
x=3.6 using Simpson‟s composite 1/3 rule.
(b) Integrate the function tabulated in Table 7.1 over the interval from x=1.6 to x=3.4

E
using Simpson‟s composite 3/8 rule.

# Exercise 3.

E
/E
(a) Find (approximately) each integral given below using the composite trapezoidal
rule with n  12 .
ST
1 4

 (1  x ) x e
2 1 2 x
(i) dx (ii) dx
1 0

(b) Find (approximately) each integral given above using the Simpson‟s composite 1/3
and 3/8 rules with n  12 .
AU

# Exercise 4.

Evaluate the integral of xe2x between x=0 and x=2 using a tolerance value
2

sufficiently small as to get an answer within 0.1% of the true answer, 0.249916
(Use adaptive integration for both Ex 4 & 5).

# Exercise 5.

Evaluate the integral of sin 2 (16 x) between x  0 and x   / 2 . Why the result is
erroneous? How can this be solved? (The correct result is  / 4 )

51
Experiment 10: Solutions to Linear Differential Equations

In mathematics, a differential equation is an equation in which the derivatives of a


function appear as variables. Differential equations have many applications in physics
and chemistry, and are widespread in mathematical models explaining biological, social,
and economic phenomena.

Differential equations are divided into two types:

E
a. An Ordinary Differential Equation (ODE) only contains function of one

E
variable, and derivatives in that variable.
b. A Partial differential Equation (PDE) contains multivariate functions and their
partial derivatives.
/E
The order of a Differential equation is that of the highest derivative that it contains. For
ST

instance, a first-order Differential equation contains only first derivatives.

A linear differential equation of order n is a differential equation written in the


following form:
AU

dn y d n1 y dy
an ( x) n
 an 1 ( x ) n 1
   a1 ( x)  a0 ( x) y  f ( x)
dx dx dx
where an ( x)  0 .

Initial value problem:

A problem in which we are looking for the unknown function of a differential equation
where the values of the unknown function and its derivatives at some point are known is
called an initial value problem (in short IVP).

If no initial conditions are given, we call the description of all solutions to the differential
equation the general solution.

52
Methods of solving the Ordinary Differential Equations:

1. Euler’s Method:

Let y ( x)  f ( x, y( x))

y( x0 )  y0
Here f ( x, y) is a given function, y0 is a given initial value for y at x  x0 .
The unknown in the problem is the function y ( x) .

Our goal is to determine (approximately) the unknown function y ( x) for x  x0 . We are


told explicitly the value of y ( x0 ) , namely y0 . Using the given differential equation, we

E
can also determine exactly the instantaneous rate of change of y at x0 . The basic idea is
simple. This differential equation tells us how rapidly the variable y is changing and the

E
initial condition tells us where y starts
y ( x0 )  f ( x0, y( x0 ))  f ( x0 , y0 ).
/E
If the rate of change of y ( x) were to remain f ( x0 , y0 ) for all time, then y ( x) would be
exactly y0  f ( x0 , y0 )( x  x0 ) . The rate of change of y ( x) does not remain f ( x0 , y0 ) for
ST

all time, but it is reasonable to expect that it remains close to f ( x0 , y0 ) for x close to x0 .
If this is the case, then the value of y ( x) will remain close to y0  f ( x0 , y0 )( x  x0 ) for x
close to x0 . So pick a small number h and define
x1  x0  h
AU

y1  y0  f ( x0 , y0 )( x1  x0 )  y0  f ( x0 , y0 )h
By the above argument
y( x1 )  y1

Now we start over. We now know the approximate value of y at x1 . If y ( x1 ) were


exactly y1 , then the instantaneous rate of change of y at x1 would be exactly f ( x1 , y1 ) . If
this rate of change were to persist for all future value of x , y ( x) would be exactly
y1  f ( x1 , y1 )( x  x1 ) .

As y ( x1 ) is only approximately y1 and as the rate of change of y ( x) varies with x , the


rate of change of y ( x) is only approximately f ( x1 , y1 ) and only for x near x1 . So we
approximate y ( x) by y1  f ( x1 , y1 )( x  x1 ) for x bigger than, but close to, x1 . Defining
x2  x1  h  t0  2h
y2  y1  f ( x1 , y1 )( x2  x1 )  y1  f ( x1 , y1 )h

53
We have y( x2 )  y2
We just repeat this argument. Define, for n  0,1, 2,3
xn  x0  nh
Suppose that, for some value of n , we have already computed an approximate value
yn for y ( xn ) . Then the rate of change of y ( x) for x close to xn is
f ( x, y( x))  f ( xn , y( xn ))  y( xn , yn )
and, again for x near xn ,
y( x)  yn  f ( xn , yn )( x  xn ) .
Hence
y( xn1 )  yn1  yn  f ( xn , yn )h
This algorithm is called Euler's Method. The parameter h is called the step size.

2. The Improved Euler's Method

E
Euler's method is one algorithm that generates approximate solutions to the initial value

E
problem
y ( x)  f ( x, y( x))
/E
y( x0 )  y0
ST

In applications, f ( x, y) is a given function and x0 and y0 are given numbers. The


function y ( x) is unknown. Denote by  ( x) the exact solution for this initial value
problem. In other words  ( x) is the function that obeys the following relation exactly.
 ( x)  f ( x,  ( x))
AU

 ( x0 )  y0

Fix a step size h and define xn  x0  nh . We now derive another algorithm that
generates approximate values for  at the sequence of equally spaced values
x0 , x1 , x2 , We shall denote the approximate values yn with yn   ( xn )
By the fundamental theorem of calculus and the differential equation, the exact solution
obeys
xn1 xn 1

 ( xn1 )   ( x ) 
n   ( x)dx   ( x
xn
n
) 
xn
f ( x,  ( x))dx

Fix any n and suppose that we have already found y0 , y1 , y2 ,, yn . Our algorithm
for computing yn 1 will be of the form
xn1

yn 1  yn  approximate value for 


xn
f ( x,  ( x))dx

54
In fact Euler's method is of precisely this form. In Euler's method, we approximate
f ( x,  ( x)) for xn  x  xn1 by the constant f ( xn , yn ) . Thus Euler's approximate value
for
xn 1 xn 1


xn
f ( x,  ( x))dx  
xn
f ( xn , yn )dx  f ( xn , yn )h

The area of the complicated region 0  y  f ( x,  ( x)) ; xn  x  xn1 (represented by the


shaded region under the parabola in the left half of the figure below) is approximated by
the area of the rectangle 0  y  f ( xn , yn ) ; xn  x  xn1 (the shaded rectangle in the
right half of the figure below).

E E
/E
Our second algorithm, the improved Euler's method, gets a better approximation by
attempting to approximate by the trapezoid on the right below rather than the rectangle on
ST

the right above. The exact area of this trapezoid is the length h of the base multiplied by
AU

1
the average,  f ( xn ,  ( xn ))  f ( xn1,  ( xn1 )) , of the heights of the two sides. Of course
2
we do not know  ( xn ) or  ( xn1 ) exactly. Recall that we have already found
y0 , y1 , y2 ,, yn and are in the process of finding yn 1 . So we already have an
approximation for  ( xn ) , namely yn , but not for  ( xn1 ) . Improved Euler uses
 ( xn1 )   ( xn )   ( xn )h  yn  f ( xn , yn )h

55
1
in approximating  f ( xn ,  ( xn ))  f ( xn1,  ( xn1 )) . Altogether Improved Euler's
2
xn 1
1
approximate value for  f ( x,  ( x))dx   f ( xn , yn )  f ( xn 1 , yn  f ( xn , yn )h)  h
xn
2
so that the improved Euler's method algorithm is
1
y( xn1 )  yn1  yn   f ( xn , yn )  f ( xn1 , yn  f ( xn , yn )h) h
2

The general step is


pn1  yn  hf ( xn , yn ) , xn1  xn  h ,
h
yn1  yn  ( f ( xn , yn )  f ( xn 1 , pn 1 ))
2

Report:

E
#Exercise 1.
x y

E
Use Euler‟s method to solve the IVP y  on [0,3] with y(0)  1 . Compare
2
1 1 1
solutions for h  1, , and .
/E
2 4 8
The exact solution is y  3e x / 2  2  x .
ST

#Exercise 2.
Consider the following circuit:
AU

In this circuit, R  20K , C  10 F , E  117V and Q(0)=0 . Find


Q for t  0 to t  3sec .

#Exercise 3.
Solve exercise 1 and 2 using Improved Euler‟s method.

56

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy