0% found this document useful (0 votes)
30 views114 pages

Numerical Methods - Lab Manual-Spring 2017

Uploaded by

thaer syam
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)
30 views114 pages

Numerical Methods - Lab Manual-Spring 2017

Uploaded by

thaer syam
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/ 114

Qatar University

College of Engineering
Numerical Methods Lab

Numerical Methods
GENG 300

LAB MANUAL

Prepared by

Mohammad Hasan Arshad

i
Qatar University
College of Engineering
Numerical Methods Lab
INTRODUCTION TO MATLAB

MATLAB is the Language of Technical Computing

Definition:

MatLab is a software tool that enables the user to perform many types of calculations and be able
to implement numerical methods. It is operated by entering commands in the command window
and display the results.

As defined by MathWorks

MATLAB® is the high-level language and interactive environment used by millions of engineers and
scientists worldwide. It lets you explore and visualize ideas and collaborate across disciplines
including signal and image processing, communications, control systems, and computational finance

MATLAB Environment:

MatLab uses three primary windows:

1. Command window, which is used to enter commands and data.

2. Graphics window that is used to display plots and graphs.

3. Edit window which is used to create and edit what is called “M-files”

1
Qatar University
College of Engineering
Numerical Methods Lab

Step-by-Step Introduction to MATLAB.


1. When MatLab is started, the command window is opened with command prompt displayed
as “>>” as shown below.

2. You can use command window as calculator to start with it, Here are few examples.

Try some more mathematical operations.

3. Note that automatically MatLab assigns the answer to a variable “ans” which you can
use in further calculations.
2
Qatar University
College of Engineering
Numerical Methods Lab

4. Quick example: type >> ans*3. What did you obtain?

5. If you wish to assign a value to a “variable” in MatLab, all what you have to do is to type:

>> variable name = value

And this is called a “scalar” quantity.

6. Echo printing: it is a characteristic of MatLab which enables the “reprint” of the value
you have assigned to your variable. It is effective in 2 cases:

a. If you end the command line with a comma “,”.


b. If you leave the command line without closing it with any symbol.

7. Echo printing can be suppressed by ending the command line with a semicolon “;” as
shown in figure

8. Note that MatLab treats the variable name in a case sensitive manner, so “a” is not the
same as “A”

3
Qatar University
College of Engineering
Numerical Methods Lab
9. Format commands:

“format long” & “format short” to increase and decrease the decimal places of the value
respectively. See figure below for illustration

10. Arrays, vectors, and matrices:

a) Array: a collection of values that are represented by a single variable. Brackets “[ ]”


are used to enter an array in MatLab. For example:

>> a = [1 2 3 4 5]

b) Vector: one dimensional arrays. It can be a “row vector” or “column vector” (mostly
used). To enter a row vector no commas or semicolons are introduced between the
numbers as:

>> b = [6 7 8];

4
Qatar University
College of Engineering
Numerical Methods – GENG 300

To enter a column vector, one of 2 ways can be followed:

1) Introducing semicolons between numbers as:

>> b = [2; 4; 6; 8; 10]


2) By transporting a row vector with the ‘operator as

>>b = [2 4 6 8 10]’

c) Matrix: two-dimensional arrays and it can be assigned in MatLab as


>> A = [1 2 3; 4 5 6; 7 8 9]

Or, using the “enter” key to separate the rows as follows:

>> A = [1 2 3
456
7 8 9]

5
Qatar University
College of Engineering
Numerical Methods – GENG 300

d) To select elements from an already entered array, use the subscript notation “()” as:

A (mth row, nth column), as shown below in figure 1.7.

11. who and whos command:

Type “who” to see the variables you have already defined in the opened command window.

6
Qatar University
College of Engineering
Numerical Methods – GENG 300

12. Size of a matrix: in order to get the size of a matrix which helps you know if
the operations performed on the matrices are correct, the size command is used
as shown in figure 1.9.

13. The colon operator: is a tool that is used to create and manipulate arrays.

a. If it is used between two numbers, MatLab automatically generates the


numbers between them using an increment of one.

b. If it is used to separate three numbers, MatLab generates the numbers


between the first and the third numbers using an increment equal to the
second number.

c. Negative increments can also be used by adding ”-“ sign to the number.
d. The colon operator can also be used to select the individual rows and columns
of a matrix. For example, if a colon is used instead of a certain subscript, the
colon represents the entire row or column.
e. The colon operator can be used as well to “selectively” extract a series of
elements from within an array. Flow up on figure on next page

7
Qatar University
College of Engineering
Numerical Methods – GENG 300

14. The “linspace” function:

 It is a built- in function, which is used to generate a row vector of equally


spaced points.
 It has the form: linspace (x1, x2, n) which generates “n” points between
x1 and
x2.

If the “n” is omitted or not specified, MatLab automatically generated 100 points.

All of the above is illustrated in figures

8
Qatar University
College of Engineering
Numerical Methods – GENG 300

15. The “logspace” function:


a. It is a built- in function which is used to generate a row vector that is
“logarithmically” equally spaced.

b. It has the form: logspace (x1, x2, n) which generates “n” logarithmically equally
spaced points between the decades 10x1 and 10x2.

If we removed n, MatLab automatically generated 50 points


here is the example of using “logspace” command

16.

9
Qatar University
College of Engineering
Numerical Methods – GENG 300

16. Mathematical operations:

a. Straight forward with scalar quantities.

b. In their “order of priority” they are listed as:

^ Exponentiation
- Negation
* / Multiplication and division
\ Left division which applies to matrix
algebra
+ - Addition and subtraction

c. Results of operations can be assigned to a variable or simply displayed

Exercise 1: define the variables u = 3, a=5;t=6 in MATLAB and perform the following
mathematical operations:
find v=u+at
find S=ut+at2/2
x = u2
y=1/u+sin(x)+ex
Answer:

Note: realize by observing error sign that MATLAB variable is case sensitive.

10
Qatar University
College of Engineering
Numerical Methods – GENG 300

Exercise 2 ( Problem 2.10 in text book)

Solution:

11
Qatar University
College of Engineering
Numerical Methods – GENG 300

Exercise 2: find the inner product and outer product of the two vectors: a = [1 2 3 4 5] and b
= [2;4;6;8;10]. (hint: use the * operator).

12
Qatar University
College of Engineering
Numerical Methods – GENG 300

Exercise 3: define the following in MA: A = [1 2 3; 4 5 6;7 8 9], a = [1 2 3], b = [4


5 6]’, then perform the following operations:

1) a * A
2) A * b
3) A * a (matrices can’t be multiplied if the inner dimensions are
unequal).
4) A * A
5) A/pi
6) A2 (MatLab will apply the simple arithmetic operators in vector-
matrix fashion if possible).
7) A. ^2 (note that the . preceding the ^ operator signifies that
the operation is to be carried out element by element).

17. In order to get back to the last typed line and editing it, press the up-arrow key. You
can use it to get back to any command line you entered by just keep pressing on it. It
is a very useful shortcut for editing commands, for example you can type “b” and
press the
up arrow key and you will get back to the last command beginning with “b”.

13
Qatar University
College of Engineering
Numerical Methods – GENG 300

18. Built – in functions:

log = natural log


Log2 = log base 2
log10 = log base 10)
exp = exponential
sqrt = square root
abs = absolute values
sin = sinusoidal function
cos = cosine function
tanh = inverse of tangent
sqrtm = square root of a matrix
round = rounds the elements to the nearest integers.
ceil = Round towards plus infinity
mean = Average or mean value
prod = ?
sort = ?
length = ?
floor = ?

Exercise : Try to get information of other built-in command using help in MATLAB, as for
example (type: help command name)

14
Qatar University
College of Engineering
Numerical Methods – GENG 300

19. Graphics in MATLAB (Generating the Plot)

 Enter x and y as row data, then use plot(x,y) command to generate plots. As for example

 Plotting as scattered data


Qatar University
College of Engineering
Numerical Methods – GENG 300

 Creating 3-D plots

You need to generate 3-D plot when you have dependent variable and two independent
variables. For example if you want to plot Z as function of x and y
Qatar University
College of Engineering
Numerical Methods – GENG 300

Programming with MatLab

Getting help in MatLab:

There are several ways to get help in MatLab, for example:

1. Click on the help menu in the command window and explore the different options.

2. Use the “help” command (as discussed in Lab 1) if you know the command or
function name.

3. Use the “lookfor” command to search the MatLab help files for occurrences of text if
you don’t know the name of the command or function. For illustration, type:

>> lookfor logarithm

4. Visit the MathWorks, Inc., website: www.mathworks.com.

Introduction to Lab 2:

As it had been discussed in the Lab1, there are three main windows in MatLab; the
command window at which commands are entered in sequence to operate MatLab, the
graphics window at which plots are presented, and the edit window at which M-files are
created and edited.

After introducing command window and graphics window in previous lab, in Lab
2, the use of edit window is extended to explain how to create and edit M-files and what
are the main uses of M-files.
Qatar University
College of Engineering
Numerical Methods – GENG 300

M - files:

An M-file contains a “series of statements” that can be run all at once in order to
provide different way of performing operations using MatLab. The nomenclature “M-
file” comes from the fact that those files are stored with a (.m) extension.

There are two main types of an M – file:

1. Script file.
2. Function file.

Script files:

A script file mainly consists of a series of MatLab commands that are saved on a
file. It is very beneficial for the sake of retaining a series of commands that can be
executed more than one time and for different objectives.

It is important to keep in mind that a script file can be executed by:

a. Typing the file name in the command window.

b. Invoking the menu selections in the edit window: Debug, run (or right click >>
run).

 Exercise 1: develop a script file to compute the distance travelled when initial value,
time and acceleration are given

 Solution steps:

1. Open the editor with menu selections: file, new, M-file.

2. Type the proper statements to calculate the distance with known values of u,a and t.

3. Save the file as “scriptexercise1. m”.

4. Go to the command window and type: >> scriptexercise1


Qatar University
College of Engineering
Numerical Methods – GENG 300

5. Check the result as shown in figure below.

Below is the script M file

And if you run the script M file you will get as shown below in command window
Qatar University
College of Engineering
Numerical Methods – GENG 300

Function files:

A function file is an M-file that starts with the word “function”. It differs from script file that it
can accept input arguments and return outputs. The syntax for the function file can be
represented as:

function outvar = funcname (arglist)

% helpcomments

statements

outvar = value;

Table 1: function file terminology

outvar = The name of the output variable


funcname The function’s name
arglist The function’s argument list (comma-
delimited values that are passed into the
function).
helpcomments Text that provides the user with information
regarding the function (can be invoked by
typing Help funcname in the command
window).
statements MatLab statements that compute the value
that is assigned to outvar.

Main characteristics of function file:

 The first line of the “helpcomments” is called the “H1” line and it is the line that is
searched by the lookfor command. Because of that; key descriptive words related to
the file must be included on this line.

 The M-file should be saved as funcname.m.

 The function can be run by typing funcname in command window.


Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 2: develop a function file to compute the distance travelled when initial value,
time and acceleration are given

 Solution steps:
1. Creation of the function file as illustrated in figure below

2. Save the file as distance.m

3. To invoke the function at different values, type distance (value of t, value of m, value
of Cd) as you want.

4. To invoke the help comments, simply type: >> help distance in command window.

5. Later on, if you forgot the name of this function, but you remember that it is about
bungee jumping, type in the command window: >> lookfor bungee.

6. See figure 3 for more illustration.


Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 3: compute the mean and standard deviation of the vector y = [8 5 10 12 6 7.5
4] using function M-file.

Solution: function M file can be written as below


Qatar University
College of Engineering
Numerical Methods – GENG 300

Above function M file can be run to find mean and standard deviation of the above data as below

 Important note: because script files have limited utility, function M – files will be the
primary programming tool for our interactive labs. So whenever referred to M – files they
are function M – files.

Input – Output:

Information is passed into function via the argument list and is output via the function’s name.
There are other functions which can be used to enter and display information directly using the
command window. These functions are the “input” function and the “display” function.

The ‘input’ function:

The input function prompts the user for values directly from the command window. Its syntax is:

n = input (‘promptstring’)

The function will show the promptstring and will wait for the user to input a value or a string so
it will be assigned for the variable “n”. If the user wants to enter a string instead of value, the
following syntax is used:

n = input (‘promptstring’,’s’)
Qatar University
College of Engineering
Numerical Methods – GENG 300

The ‘disp’ function:

This function is used to display a value or a string and its syntax is: disp (value/variable/string).

 Exercise 4: compute the distance travelled when initial value, time and acceleration are
given (exercise 2) using the input and display functions for input/output.

Solution: Function M file can be created as below

The above function file can be run as shown below


Qatar University
College of Engineering
Numerical Methods – GENG 300

Structured Programming:

The simplest type of all M – files perform the operations sequentially or line by line. Because
this sequence is highly limiting, MatLab includes statements that allow the program to take non-
sequential path. These statements are classified as:

1. Decisions (or selections) which represents the branching of flow based on a decision.
2. Loops (or repetition) which represents the looping of flow to allow statements to be
repeated (to be discussed in lab 3).

Decisions

1. The “if” structure: allows the user to execute a set of statements if a logical condition is
true. The general syntax is:

if condition
statements
end

Where “condition” is a logical expression that is either true or false. It is important to


mention as well that in cases where only one statement is executed, it is convenient to
implement the ‘if’ structure as a single line:

if condition, statements, end

By the same way, the ‘if else’ and ‘if elseif’ structures are represented in MatLab:
Qatar University
College of Engineering
Numerical Methods – GENG 300

if condition
statements
else
statements
end

if condition
statements
elseif condition
statements
end

 Exercise 7: create a function M –file that asks the student to input his/her name &
grade in numbers to see the overall grade (A, B+, B,…etc).

2. The ‘error’ function which has the syntax: error (message) and can be used for error
trapping by displaying the “message” and terminating the M - file and return to the
Qatar University
College of Engineering
Numerical Methods – GENG 300

command window. For example, if you have identified your variable f = 1/x and you
want to detect the error of dividing by zero, the error function can be implemented in the
M – file itself as shown below.

3. The logical conditions:


 The simplest form of the condition is a single relational expression that compares two
values: value 1 relation value 2.
 ‘value’ can be constant, variable, or expression.
 ‘relation’ is one of the relational operators listed in table 2 below.

Table 2:summary of relational operators in MATLAB


Qatar University
College of Engineering
Numerical Methods – GENG 300

 MatLab allows the user to test more than one logical condition by employing logical
operators. The mostly used expressions are:

1. ~ (not): used to perform logical negation on an expression, so if the


expression is true, the result is false and vice versa.
2. & (and): used to perform a logical conjunction on two expressions. If both
are true, the result is true. If either or both are false, the result is false.
3. | (or): used to perform a logical disjunction on two expressions. If either or
both are true, the results is true.
Qatar University
College of Engineering
Numerical Methods – GENG 300

Lab session 3

Programming with MatLab (Continued):


Structured programming (loops):

1. Loops

As the name indicates, what loops do is that they perform operations repetitively. Depending on
how the repetitions are terminated; loops come into two forms, the for loop and the while loop:

“for” loop: it ends after a specified number of repetitions:

 The “for…end” structure: a “for” loop repeats statements a specific number of times and
its general syntax is:

for index = start: step: finish


statements
end

How does the operation of the “for loop” go?

a. The index is a variable that is set at an initial value “start”.


b. The program then compares the index with a desired value “finish”.
c. If the index < = finish, the program executes the “statements”.
d. When it reaches the “end” line that marks the end of the loop, the “index” variable is
increased by the “step” and the program loops back up to the “for” statement.
e. The process continues until index > finish value. At this point the loop terminates as the
program skips down to the line immediately following the “end” statement.
f. Remember that if an increment of 1 is desired the “step” can be skipped (like using the
colon operator – lab 1).
g. The step size can be an integer or any other value and can be positive or negative as well.
h. Remember that if the negative step is used the loop will countdown in reverse and so the
loop’s logic is reversed (reverse of above steps).

For a quick example, try to execute the following using an M-file:

for j = 10:-1:1

disp (j)

end
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 1: develop an M-file to compute the factorial of a number.

While Loop:

The “while” structure: a while loop repeats as long as a logical condition is true and its
general syntax is:

while condition
statements
end

Put in mind that the “statements” between the “while” and the “end” are repeated as long as
the “condition” is true. For a quick example, execute the following using an M-file:

x=8
while x > 0
x = x – 3;
disp (x)
end

 The “while…break” structure: the while structure is very useful, but it exits at the
beginning of the structure on a false result which is limiting. In order to overcome this
problem; a special version of the while loop is used with the following syntax:

while (1)
statements
if condition, break, end
statements
end
Qatar University
College of Engineering
Numerical Methods – GENG 300

 The “break” is used to terminate execution of the loop if the condition tests true. It can be
placed at the middle of a loop and this is called the “midtest” loop. The “break” can be
added at the beginning (before statements) if required and this is called the “pretest” loop:

while (1)

if x < 0, break, end

x = x – 5;

end

Nesting and Indentation:

“Nesting” is basically referring to placing structures within other structures.

 Exercise 2: develop a function to compute the quadratic roots of an equation using the
following formula:

√ 4
2
Note that the user must provide the values of a, b, and c.
Qatar University
College of Engineering
Numerical Methods – GENG 300

Passing functions to M – files:

What we have done so far using MatLab is developing a function for each new equation we want
to analyze. Although this procedure is fine, a better alternative is to design a generic function and
pass the particular equation that the user wants to analyze as an “argument”. In MatLab, such
functions are names as “function functions”.

Anonymous functions:

Ananymous functions allow the user to create a simple function without creating an M – file.
These functions can be defined within the command window with the below syntax:

fhandle = @ (arglist) experessions

Where:
Qatar University
College of Engineering
Numerical Methods – GENG 300

 fhandle: the function which the user can use to call the function.
 arglist: a comma separated list of input arguments to be passed to the function.
 expressions: any single valid MatLab expression.

 Exercise 3: create the anonymous function f(x) = 4x2 using MatLab and evaluate it for x
= 3.
Qatar University
College of Engineering
Numerical Methods – GENG 300

function functions: fplot

Function functions are functions that operate on other functions which are passed to it as input
arguments. “passed function” is the function that is passed to the function function. A quick
example is the built-in function “fplot” which plots the graphs of functions, and its syntax is:

fplot (fun, lims)

Where “fun” is the function being plotted between the x – axis limits specified by “lims” = [xmin,
xmax]. In this case “fun” is the passed functions. What is important to mention is that this function
is “smart” because it automatically analyzes the function and decides how many values to use so
that the plot will exhibit all the function’s features.

 Exercise 4: use “fplot” to plot the velocity of the free-falling bungee jumper. Remember
that: m = 68.1 kg, g = 9.81 m/s, Cd = 0.25 and let the plot be for values of ‘t’ between 0
and 12.

 Exercise 5: Develop an M-file functions function to determine the average value of a


function over a certain range.
Qatar University
College of Engineering
Numerical Methods – GENG 300

Using the above function M file to find average value of a function y=x3+3x2+5

Root Finding Using MatLab:

In this section we will be covering root finding by using:

 Newton Raphson function M file.


 Bisection function M file.
 “fzero” MatLab built-in function.
 “roots” MatLab built-in function.

Newton – Raphson function:

It is important to mention that the program must have access to the function (func) and its first
derivative (dfunc) (can simply be accomplished by the inclusion of user-defined functions to
compute these quantities). For a simple example on using ‘newtraph’ function in root finding,
execute the following in the command window:

>> newtraph (@ (x) x^2-9, @ (x) 2*x, 5)


Qatar University
College of Engineering
Numerical Methods – GENG 300

Function M file for newton rapshson method

function yint=Newtint(x,y,xx)
%Newtint: Newton interpolating polynomial
%yint=Newtint(x,y,xx): Uses an (n-1)-order Newton interpolating polynomial
%based on n data points (x,y) to determine a value of the dependant
%variable (yint) at a give value of the independant variable, xx.
%input:
% x= independant variable
% y= dependant variable
% xx= value of the independant variable at which interpolation is
% calculated
%output:
% yint=interpolated value of dependant variable
%compute the finite divided differences in the form of a difference table
n=length(x);
if length(y)~=n, error('x and y must have same length'); end
b=zeros(n,n);
%assign dependant variables to the first column of b.
b(:,1)=y(:); % the (:) ensures that y is a column vector
for j=2:n
for i=1:n-j+1
b(i,j)=(b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i));
end
end
%use the finite divided differences to interpolate
xt=1;
yint=b(1,1);
for j=1:n-1
xt=xt*(xx-x(j));
yint=yint+b(1,j+1)*xt;
end

Example: Use above M file to solve the following equations

1. 2x3-6x-4=0
2. x2-ex+x3=0

Solution:
Qatar University
College of Engineering
Numerical Methods – GENG 300

Note: Please observed that in order to get more than one root for same equation you
have to have to run the function file with different guess value.
Qatar University
College of Engineering
Numerical Methods – GENG 300

Bisection method function M file

function root = bisection(func,xl,xu,es,maxit)


% root = bisection(func,xl,xu,es,maxit):
% uses bisection method to find the root of a function
% input:
% func = name of function
% xl, xu = lower and upper guesses
% es = (optional) stopping criterion (%)
% maxit = (optional) maximum allowable iterations
% output:
% root = real root
if func(xl)*func(xu)>0 %if guesses do not bracket a sign change
disp('no bracket') %display an error message
return %and terminate
end
% if necessary, assign default values
if nargin<5, maxit=50; end %if maxit blank set to 50
if nargin<4, es=0.001; end %if es blank set to 0.001
% bisection
iter = 0;
xr = xl;
while (1)
xrold = xr;
xr = (xl + xu)/2;
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
test = func(xl)*func(xr);
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea <= es | iter >= maxit, break, end
end
root = xr;
Qatar University
College of Engineering
Numerical Methods – GENG 300

Example: Use above M file to solve the following equations

3. 2x3-6x-4=0
4. x2-ex+x3=0

solution:

Please note that in order to get the other poosible roots you have to run the M
file again with different bracket (xlower & xupper)
Qatar University
College of Engineering
Numerical Methods – GENG 300

‘fzero’ MatLab function:

The ‘fzero’ function in MatLab is designed to find the real root of a single equation. The syntax
of this function is:

fzero (function, x0)

Where ‘function’ is the name of the function being evaluated and x0 is the initial guess. In case
there are two guesses that bracket a sign change, these guesses can be passes as a vector:

fzero (function, x0, x1)

 Exercise 7: find the roots of x2 – 9 = 0 using ‘fzero’ function in MatLab.


Qatar University
College of Engineering
Numerical Methods – GENG 300

Polynomials: ‘roots’ MatLab function:

If the user is dealing with a case where a single real root must be determined for a polynomial,
then some techniques such as bisection and the Newton – Raphson method can be used easily.
However, if the user desires to get all roots real and complex, then these simple techniques will
not be of much help.

Fortunately, MatLab has an excellent built-in capability which is the ‘roots’ function. The
general syntax of this function is:

x = roots (c)

Where ‘x’ is a column vector containing the roots, and ‘c’ is a row vector containing the
polynomial’s coefficients. Note that if for example the polynomial is:

x5 – π then ‘c’ = [1 0 0 0 0 –pi]

 How does the ‘roots’ function work?

 Assume that we have a polynomial:

a1x5 + a2x4 + a3x3 + a4x2 + a5x + a6 = 0

 Dividing by a1 and rearranging, gives:


x5 = -a2/a1x4 – a3/a1 x3 –a4/a1 x2 – a5/a1 x – a6/a1

 Then a special matrix can be constructed by using the coefficients from the right hand
side as the first row and with 1’s and 0’s written for the other rows as shown in equation
6.13 in your text book.

 This formed matrix is called the polynomial’s companion matrix. It has the useful
property that its ‘’eigenvalues’’ are the roots of the polynomial.

 Important note about the ‘roots’ function: the roots function in MatLab has an inverse
function called ‘poly’, which when passed the values of the roots, will return the
polynomial’s coefficients. Its syntax is:

c = poly (r)
Qatar University
College of Engineering
Numerical Methods – GENG 300

Where ‘r’ is a column vector containing the roots and ‘c’ is a row vector containing the
polynomial coefficients.

In order to evaluate the polynomial at a certain value of the independent variable, the
following syntax can be used:
polyval (a, xi)

Where ‘a’ is the row vector that contains the polynomial coefficients and ‘xi’ is the value of
independent variable that the user wants to compute the polynomial at.

Example: Solve x3-2x2+x-3

Solution:

f=[1 -2 1 -3];

>> roots(f)

ans =

2.1746 + 0.0000i

-0.0873 + 1.1713i

-0.0873 - 1.1713i
Qatar University
College of Engineering
Numerical Methods – GENG 300

Linear Algebra:
Matrices:
So far we have been discussing what are the mostly used tools in Excel and MatLab to
solve single equations. What if there is a need to solve a system of linear equations
simultaneously at which they have “n” number of unknowns? The first thing to make sure of
when trying to solve a system of multiple equations, multiple unknowns is that the number of
equations is equal to the number of unknowns or otherwise this system can’t be solved.

Solving two simultaneous equations is kind of simple to be done by hand, but what if you are
faced with a system of 3 or more, or let’s say 100 unknowns!

In this case the use of “matrices” is practically implemented to manage the process of finding the
unknowns. A “matrix” is a rectangular array of elements represented by a single symbol ‘[A]’.
The matrix consists of ‘m’ rows and ‘n’ columns. When m = n the matrix is called a square
matrix.

There are different types of matrices, such as:

a. Symmetric matrix
A=

5 1 2

1 3 7

2 7 8

b. Diagonal matrix

B= 5 0 0

0 3 0

0 0 8

c. Identity matrix
Qatar University
College of Engineering
Numerical Methods – GENG 300

C=

1 0 0

0 1 0

0 0 1

d. Upper triangular matrix

D= 1 2 3

0 5 6

0 0 8

e. Lower triangular matrix

E=

1 0 0

1 5 0

1 2 8

Matrix operations:
There are different operations that can be performed on matrices, so let us just go over them
quickly:

a. Addition and subtraction

b. Multiplication

c. Inverse of a matrix

d. Identity matrix

e. Size of a matrix
Qatar University
College of Engineering
Numerical Methods – GENG 300

f. Augmentation of 2 matrices

g. Determinant of a matrix

Imagine that these types of operations that take so much time to be completed by hand
calculation (special for large size matrices) can be easily performed using our powerful tool
“MatLab”.

 Exercise 3: For the following matrix A = [1 5 6; 7 4 2; -3 6 7] perform the following


operations using MatLab:

1. Get the transpose of the matrix


2. Combine the following rows into one matrix and call it “B”:

X = [8 6 9]
Y = [-5 8 1]
Z = [4 8 2]

3. Evaluate [C] = [A] + [B]


4. [A] * [B]
5. [A].*[B]
6. Insert [D] = [1 4 3; 5 8 1]
7. A*D
8. D *A
9. Get the inverse of the matrix A = [AI]
10. A*AI
11. Get the identity matrix = I
12. Get the augment Aug = [A I]
13. Get the size of the matrix [Aug].
Qatar University
College of Engineering
Numerical Methods – GENG 300
Qatar University
College of Engineering
Numerical Methods – GENG 300
Qatar University
College of Engineering
Numerical Methods – GENG 300
Qatar University
College of Engineering
Numerical Methods – GENG 300

Using matrices and MatLab to solve a system of linear equations:


When facing a large system of linear equations, it is always easy to represent it using a “matrix”
and then using MatLab to solve all the equations and get the unknowns. For example, let us say
that we have the below 3 equations with 3 unknowns that need to be solved:

This system can be expressed as:

[A]{x} = {b}

Where:

{b}= [b1; b2; b3] (a column vector of constants)

x : is a column vector for unknowns

To obtain the solution of this system using MatLab can be found using:

1. x = A\b
2. x = inv (A) *b
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 4: use MatLab to solve the following equations:

150x1 – 100x2+50x3+120x4 = 588.6


-100x1 + 150x2 – 50 x3= 686.7
-50x2 + 50 x3= 784.8
-110x1+45x2+85x4=112
Qatar University
College of Engineering
Numerical Methods – GENG 300

Naïve Gauss elimination:


The Naïve Gauss elimination can be used to solve system of linear equations. The hand
calculations of this method is kind of heavy and can be manageable for up to a 3x3 matrix,
however, for more than 3 unknowns then the M-file of this function can be called in MatLab to
find the solution easily and within a short period of time.
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 5: use Naïve Gauss elimination method in MatLab to solve the following
system of linear equations:

3x1 – 0.1x2 – 0.2x3 = 7.85


0.1x1 + 7x2 – 0.3x3 = -19.3
0.3x1 – 0.2x2 + 10x3 = 71.4
Qatar University
College of Engineering
Numerical Methods – GENG 300

The Pivoting method:


The pivoting method can also be used to solve system of linear equations. The hand calculations
of this method is kind of heavy and can be manageable for up to a 3x3 matrix, however, for more
than 3 unknowns then the M-file of this function can be called in MatLab to find the solution
easily and within a short period of time.
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 6: solve exercise 5 using the pivoting method in MatLab.

LU Factorization:
The idea of the LU factorization is to be able to decompose matrix [A] into two triangular
matrices: the upper triangle matrix [U] and the lower triangle matrix [L].

[A] = [L][U]

Recalling that:

[A]{x} = {b}

The following can be concluded:

[U]{x} = {d}

[L]{d} = {b}

Factorization can be used to solve a system of linear equations according to the following steps:

1. Enter the overall matrix A in MatLab.


2. Enter the column vector b.
3. Compute LU factorization using MatLab as: >> [L,U]=lu(A)

4. Compute the vector “d” as: >> d=L\b


5. Compute the value of the vector ‘x’ as: >> x=U\d
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 7: use MatLab to compute the LU factorization and find the solution for the
following system of linear equations:
Qatar University
College of Engineering
Numerical Methods – GENG 300

Cramer’s rule:
The Cramer’s rule and the concept of Matrix “determinant” can be used to solve system of
linear equations (for a small number of equations). The hand calculations of this method is kind
of heavy and can be manageable for up to a 3x3 matrix, however, for more than 3 unknowns then
the M-file of this function can be called in MatLab to find the solution easily and within a short
period of time.

In order to solve a system of linear equations using Cramer’s rule in MatLab, the following steps
can be followed:

 Write the system of equations in the form of a Matrix: A{x} = {b}.


 Input the matrix coefficients.
 Input column vector “b”.
 Calculate determinant of A as: D = det(A)
 To compute x1, get modified matrix A
 >> A(:,1)=b

 To calculate x1:
 x1=det(A)/D
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 1: use Cramer’s rule with MatLab to solve the following system of linear
equations:
Qatar University
College of Engineering
Numerical Methods – GENG 300

M file for Cramer’s Rule


Qatar University
College of Engineering
Numerical Methods – GENG 300

Gauss Seidel:
Guess Seidel is one of the available methods which can be used to solve a system of linear
equations. In order to implement this method in MatLab, you must have the GaussSeidel M-file
which is shown in the below figure.
Qatar University
College of Engineering
Numerical Methods – GENG 300

In order to use this method along with the M-file, the following steps are followed:

 Write the system of equations in the form of a Matrix: A{x} = {b}.


 Input the matrix coefficients.
 Input column vector “b”.
 Compute all the unknowns as: GaussSeidal (A,b)

 Exercise : use GaussSeidel with MatLab to solve the following system of linear
equations:
Qatar University
College of Engineering
Numerical Methods – GENG 300

Solving Systems of Nonlinear Equations:


In this section, nonlinear equations will be introduced with two ways to solve them: a. using
solver in Excel, b. using MatLab tool.

Newton Raphson for a non linear system of equations:


The idea of using Newton Raphson method for a non linear system is to compute the function
value and the Jacobian at a given value of “x”, then the new estimate of x is calculated as:

xi+1 = xi – [J]-1{f}
Using MatLab for this purpose can be into forms:

1. Identifying the range of values of “x” and calculating the Jacobian and the function in the
command window.

2. A better alternative to work out the trials is by using the M-file that computes the
function values and the Jacobian in an iterative fashion. The iterations will continue until
an upper limit of iterations (maxit) or a specified percent relative error (es) is reached.
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 3: use Newton Raphson along with MatLab to find the roots of the following
system of non-linear equations:

x12 + x1x2 =10

x2 + 3x1x22 =57
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Solution:

1. Create a function M-file which contains the equation of the function and the Jacobian
as functions of “x”.

Figure 1: answer for exercise 3/ part 1

2. Call the function using Newton Raphson M-file as shown in the below figure:
Qatar University
College of Engineering
Numerical Methods – GENG 300

Regression

What is Regression?
Mathematical method for determining the best equation that reproduces a data set.

- In a data set (x,y) what is x and y. x is cause and called as independent variable. Whereas
y is effect and called as dependent variable.
- A data set is obtained through experiments and experiment can be conducted only for a
limited data points.
- That’s why regression is performed to come up with a mathematical model which can
predict the value of dependent variable (y) at the all points of x for which experimental
data is not available.

Least Square Regression: A mathematical procedure for finding the best‐fitting curve to a
given set of points by minimizing the sum of the squares of the offsets ("the residuals") of the points
from the curve.

- Linear Least Square Regression:


- Linear least-squares regression is a method to determine the “best” coefficients in a linear
model for given data set.

In Linear model we two coefficients a0 and a1 as unknown

y  a 0  a1 x
- “Best” for least-squares regression means minimizing the sum of the squares of the
estimate residuals. For a straight line model, this gives:
n n
S r   e i
2
  y i  a 0  a1xi
2

i1 i1

- Applying least square method, Sr in equation 1 become objective function which is to be


minimized with a0 and a1 as variable.
- For minimum Sr, partial derivative of Sr with respect to a0 and a1 has to be zero.
Qatar University
College of Engineering
Numerical Methods – GENG 300

S r
 2 ( y i  a 0  a1 xi )
a 0

S r
  2   yi  a0  a1 xi  xi 
a1
Rearranging and simplifying above equation lead to following

n a0   xi a 1   yi

 xi a 0   xi
2
a 1   xi yi
Above is the system of linear equations with 2 equations and 2 knowns can be presented
in MATRIX form as follows

n  xi  a0   yi 
     
  x i    x i y i 
2
x i   1 
a

Above system of linear equations can be solved for a0 and a1 as follows

a1 
n x y x  y
i i i i

n  x   x 
2 2
i i

a0  y  a1 x

Where y and x are the mean of y and x respectively.


Qatar University
College of Engineering
Numerical Methods – GENG 300

Quantification of Error

Recall for a straight line, the sum of the squares of the estimate residuals
n n
Sr   e 2
i   y i  a 0  a1 x i 
2

i1 i1

Standard error of the estimate:

Sr
sy/ x 
n2
- Regression data showing (a) the spread of data around the mean of the dependent data
and (b) the spread of the data around the best fit line:
Qatar University
College of Engineering
Numerical Methods – GENG 300

The reduction in spread represents the improvement due to linear regression.

St  ( y  y) 2
Coefficient of Determination

• The coefficient of determination r2 is the difference between the sum of the squares of the
data residuals and the sum of the squares of the estimate residuals, normalized by the sum
of the squares of the data residuals:

St  Sr
r2 
St
• r2 represents the percentage of the original uncertainty explained by the model.

• For a perfect fit, Sr=0 and r2=1.

• If r2=0, there is no improvement over simply picking the mean.

• If r2<0, the model is worse than simply picking the mean!


Qatar University
College of Engineering
Numerical Methods – GENG 300

Nonlinear Relationships

Linear regression is predicated on the fact that the relationship between the dependent and
independent variables is linear - this is not always the case.

Three common examples are:

exponential : y   1e 1 x

power : y   2 x 2
x
saturation - growth - rate : y  3
3  x

Linearization of Nonlinear Relationships


One option for finding the coefficients for a nonlinear fit is to
linearize it. For the three common models, this may involve taking
logarithms or inversion:

Model Nonlinear Linearized

exponential : y  1e1 x ln y  ln 1  1 x

power : y  2 x 2 log y  log  2  2 log x


x 1 1 3 1
saturation - growth - rate : y   3  
3  x y 3 3 x
Qatar University
College of Engineering
Numerical Methods – GENG 300

Transformation Examples
Qatar University
College of Engineering
Numerical Methods – GENG 300

Best Fit of Data Using Microsoft Excel (linear regression):


Microsoft Excel can be used as a very useful tool for “regression” which might be defined as a
form of “data fitting” which is used when data exhibit significant degree of error or “noise”
where the curve does not have to pass through all points.

 Exercise 1: fit the following data using Microsoft Excel using linear regression:

x y
1 10
2 16.3
3 23
4 27.5
5 31
6 35.6
7 39
8 41.5
9 42.9
10 45
11 46
12 45.5
13 46
14 49
15 50

1. Start by plotting the data as shown in figure 1.

60
50
40
30
Series1
20
10
0
0 5 10 15 20
Qatar University
College of Engineering
Numerical Methods – GENG 300

2. Right click >> add trend line >> linear as shown in figure 2. Also make sure to check the
boxes corresponding to “display equation on chart” and “display R-squared value on
chart”.

3. As can be shown from the obtained plot, the slope and intercept or (ao and a1) can be
easily obtained by this way. (Remember that for linear regression: y = ao + a1x).

4. Another way to obtain the coefficients (ao and a1) is simple by calculating them using the
following equations:

∑ ∑ ∑
∑ ∑

5. This is illustrated in figure 3 below.


Qatar University
College of Engineering
Numerical Methods – GENG 300

6. Compare the obtained values of coefficients with the ones obtained from the equation on
the plot.

 Note: the same procedure using Excel can be used to fit data in polynomial, exponential, and
power form. For exponential and power forms also linear regression can be used if the
equation was properly transformed into linear form, as an example:

a. Power form: y = axb, can be transformed to linear form as: log(y) = log (a) +b(log(x))

Where: “b” is the slope or “a1” and “log a” is the intercept or “ao”.

b. Exponential form: y = aebx, can be transformed to linear form as: ln(y) = ln(a)+ bx
Qatar University
College of Engineering
Numerical Methods – GENG 300

Best Fit of Data Using MatLab (single regression):

MATLAB can be used as well to fit the data by different methods:

Calculation of coefficients:

 Exercise 2: resolve exercise 1 using MatLab by calculating the coefficients.


Qatar University
College of Engineering
Numerical Methods – GENG 300

MatLab m-file: linregr:


“linregr” is an m-file which is developed in order to fit the data “linearly” by calculating the
slope and the intercept of the linear regression equation (ao and a1). The structure of this m-file is
shown below
Qatar University
College of Engineering
Numerical Methods – GENG 300

The use of this m-file to calculate the coefficients of the linear regression can be easily done by
just specifying the data points and then call the m-file. For better illustration, follow exercise 4.

 Exercise 4: Resolve exercise 1 using “linregr” m-file.

As it can be shown from the above solution that the obtained values of the coefficients are in
descending order (a1 then ao).

 Note: the same m-file can be used to fit data of exponential order or power order “if they are
transformed into the linear form as was discussed before”.
Qatar University
College of Engineering
Numerical Methods – GENG 300

“Polyfit” and “Polyval” built in functions:


There are two useful built in functions in MatLab which can be used to help in calculating the
needed coefficients for data fitting. “Polyfit” can be used to fit a least-square nth – order
polynomial to data (remember that a first order polynomial is linear). Its syntax is:

P = polyfit (x, y, n)

Where “x” and “y” are the vectors of the independent and dependent variables, respectively and
“n” is the order of the polynomial. This function when used returns a vector “P” which contains
the polynomial coefficients (ao, a1, a2,….an) in descending order.

Another built-in function which is available is “polyval” which can be used to compute the value
of the function at certain value of “x” given the polynomial coefficients. Its syntax is:

y = polyval (P, x)

 Exercise 3: Resolve exercise 1 using “polyfit” built-in function and calculate the value of
the function @ x = 9.
Qatar University
College of Engineering
Numerical Methods – GENG 300

Polynomial Regression

• The least-squares procedure from linear regression can be readily extended


to fit data to a higher-order polynomial. Again, the idea is to minimize the
sum of the squares of the estimate residuals.

• The figure shows the same data fit with:

a) A first order polynomial

b) A second order polynomial

Poor fit: straight line

Good fit (parabola)


Qatar University
College of Engineering
Numerical Methods – GENG 300

Fitting Second Order polynomial

Therefore, for second order polynomial we got the above system of linear
equations with a0, a1 and a2 unknown. Moreover, above system of equations
can be written in MATRIX form as follows

n  x  x  a  yi
2

  0 
i i

 xi  a2    
 x  x  i i 
2 3
x y
 
i i
  
  x i  x  x   a 3 
  x i y i 
2 3 4 2
i i
Qatar University
College of Engineering
Numerical Methods – GENG 300

Practice example in LAB. Create an M file to fit a second order polynomial to


data as below. You just need to write code to solve above system of linear
equations.
x = [0 1 2 3 4 5]’, y = [2.1 7.7 13.6 27.2 40.9 61.1]’. Compute “r2” and the standard error for
the obtained results.
Qatar University
College of Engineering
Numerical Methods – GENG 300

Best Fit of Data Using Microsoft Excel (multiple regression):


Linear regression is implemented in the case where there is on dependent variable and multiple
independent variables. Microsoft Excel can be easily used for this purpose as illustrated in
exercise 7.

 Exercise 7: use multiple linear regression in Excel to fit the following set of data:

x1 x2 y
0 0 5
2 1 10
2.5 2 9
1 3 0
4 6 3
7 2 27

Solution: there are different ways to solve this problem, the solution depends on the following
equation:

∑ 1 ∑ 2 ∑
∑ 1 ∑ 1 ∑ 1 2 = ∑ 1
∑ 2 ∑ 1 2 ∑ 2 ∑ 2

Now how to use Microsoft Excel to best fit the data?

1. Insert the data in Excel.


2. Load the “data analysis” if it doesn’t appear under “data” as shown in figure below.
3. Click on Data >> Data Analysis and select “regression” as shown in figure below
4. Click ok >> input Y – range and input X – range and select “new worksheet Ply” >>
click ok as shown in figure
5. Summary output will appear as shown in figure.
Qatar University
College of Engineering
Numerical Methods – GENG 300
Qatar University
College of Engineering
Numerical Methods – GENG 300
Qatar University
College of Engineering
Numerical Methods – GENG 300

Nonlinear Regression in MATLAB


 To perform nonlinear regression in MATLAB, write a function M file that returns the sum
of the squares of the estimate residuals for a fit and then use MATLAB’s fminsearch
function to find the values of the coefficients where a minimum occurs

 The arguments to the function to compute Sr should be the coefficients, the independent
variables, and the dependent variables

 Example: fit a non‐linear model to the x‐y data given below


y  A 1  e  x / 
x 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 7
y 1.48 1.9 2.07 2.2 2.34 2.38 2.43 2.46 2.47 2.48 2.488 2.49

Solution:
The function M file….
function f = fSSR(a, x, y)
yp = a(1)*(1-exp(-x/a(2)));
f = sum((y-yp).^2);

Running the above function M file


a = fminsearch(@fSSR, [1, 1], [], x, y)
a = 2.4939 1.1009
Qatar University
College of Engineering
Numerical Methods – GENG 300

Interpolation:
Interpolation is used to estimate intermediate values between precise data points. For “n” data
points, there is one and only one polynomial of order (n – 1) that passes through all the points.
The polynomial equation can be represented as follows:

f(x) = a1 + a2x + a3x2 + ….. + anxn‐1


An important note about MatLab is that it represents the polynomial coefficients in a different
manner, so instead of increasing powers of ‘x’ as shown in the above equation; it uses
decreasing powers as shown below:

f(x) = p1xn‐1 + p2xn‐2 + …+ pn‐1x + pn


Different types of interpolation and different ways to compute it using MatLab will be
presented in the following sections.

MatLab functions: ‘Polyfit’ & ‘Polyval’ (direct interpolation method):

Remember from last session that ‘polyfit’ can be used to perform polynomial regression. In
such application, the number of data points is greater than the number of coefficients being
estimated. Because of that, the least – squares fit line doesn’t necessarily pass through any of
the points, but rather follows the general trend of data.

In case the number of data points = number of coefficients, then “polyfit” performs
interpolation. That means that it returns the coefficients of the polynomial that pass directly
through the data points.

The general syntax is: p = polyfit (x, fx, n) where “n” = order of polynomial.
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 1: determine the coefficients of the parabola f(x) = p1x2 + p2x + p3 that passes
through the following points:

X F(x)
300 0.616
400 0.525
500 0.457

Solution: there are two ways to solve this exercise and find the coefficients using MatLab:

o Solving for the simultaneous equations:

0.616 = p1 (300)2 + p2 (300) + p3


0.525 = p1 (400)2 + p2 (400) + p3
0.457 = p1 (500)2 + p2 (500) + p3

So the polynomial is: f(x) = 0.00000115x2 – 0.001715x + 1.027


Qatar University
College of Engineering
Numerical Methods – GENG 300

o Using polyfit built – in function:

Therefore, the polynomial is f(x) = 0.00000115x2 – 0.001715x + 1.027. In addition, the function
was evaluated at x = 350 and the result is reasonable.
Qatar University
College of Engineering
Numerical Methods – GENG 300

Newton Interpolation:
“Newtint” is an M – file that can be developed to implement Newton interpolation in MatLab.
This M – file is shown in figure 3. The first step is to compute the finite divided differences and
store them in an array. The differences are then used to perform the interpolation.
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 2: employ a second order Newton polynomial to estimate “ln 2” with the
following data points using MatLab:

X F(x)
1 0
4 1.386294
6 1.791759
Qatar University
College of Engineering
Numerical Methods – GENG 300

Lagrange interpolating polynomial:


“Lagrange” is an M – file which can be developed to implement lagrange interpolation in
MatLab. This M – file is shown in figure 5. The function is passed two vectors containing the
independent variable “x” and the dependent variable “fx”. It is also passed the value of the
independent variable where you want to interpolate “xx”.
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 3: employ a third order polynomial to estimate “f(x) at x = 15” using Lagrange
interpolation with the following data points:

X F(x)
‐40 1.52
0 1.29
20 1.2
50 1.09

MatLab function: ‘interp1’:


The built – in function ‘interp1’ provides a useful tool to implement a number of different types
of piecewise one‐dimensional interpolation. It has the following general syntax:

yi = interp1 (x, y, xi, ‘method’)

Where ‘x’ and ‘y’ are vectors containing the values to be interpolated, ‘yi’ is a vector containing
the results of the interpolation as evaluated at the points in the vector ‘xi’, and ‘method’ is the
desired method.

The various methods available are listed in table 1:


Qatar University
College of Engineering
Numerical Methods – GENG 300

Table 3: different types of “methods” to be used with ‘interp1’

Method Description
nearest Nearest neighbor interpolation which sets a
value of an interpolated point to the value of
the nearest existing data point.
linear Linear interpolation which uses straight lines
to connect the points.
spline Piecewise cubic spline interpolation.
pchip and cubic Piecewise cubic Hermite interpolation.

 Note: if the “method” argument is omitted, the default one is linear interpolation where
interp1(x,y,xi) performs one dimensional interpolation where x and y are related as y = f(x)
and xi is some value for which we want to find y(xi) by linear interpolation. This command
will search the x vector to find two consecutive entries between which the desired value
falls. It then performs linear interpolation to find the corresponding value of y.

 Exercise 4: use MatLab ‘interp1’ function o fit the following data with (a) linear
interpolation, (b) nearest neighbor interpolation (extra), (c) spline:

X F(x)
0 0
20 20
40 20
56 38
68 80
80 80
84 100
96 100
104 125
110 125
Qatar University
College of Engineering
Numerical Methods – GENG 300

a. Linear Interpolation
Qatar University
College of Engineering
Numerical Methods – GENG 300

(b) Nearest neighbor interpolation


Qatar University
College of Engineering
Numerical Methods – GENG 300

(c) Spline Interpolation


Qatar University
College of Engineering
Numerical Methods – GENG 300

Integration:
Integration as can be represented in the below equation is defined as the total value or
summation of f(x)dx over the range x = a to b.

Different presentations and methods used to evaluate the integration using MatLab will be
presented in the upcoming sections.

MatLab functions: ‘trapz’ and ‘cumtrapz’:


MatLab has a built – in function, that evaluates integrals for data in the form of trapezoidal rule
for unequally spaced data. It has the following general syntax:

Z = trapz(x,y)

 Exercise 5: integrate the following set of data using “trapz” in MatLab:

X F(x)
0 0.2
0.12 1.309729
0.22 1.305241
0.32 1.743393
0.36 2.074903
0.40 2.456
0.44 2.842985
0.54 3.507297
0.64 3.181929
0.70 2.363
0.80 0.232
Qatar University
College of Engineering
Numerical Methods – GENG 300

F(x) = 0.2 + 25x – 200x2 + 675x3 – 900x4 + 400x5

 Note: ‘cumtrapz’ is another function which computes the cumulative integral and it has the
following syntax: z = cumtrapz(x,y).

 Exercise 6: let us follow example 17.7 in your text book.

MatLab functions: ‘quad’ and ‘quad1’:


MatLab has two very useful built‐in functions which can be used for implementing adaptive
quadrature.

a. ‘quad’: this particular function uses adaptive Simpson quadrature. It may be more
efficient for low accuracies or no smooth functions.
b. ‘quad1’: this function uses “Lobatto quadrature”. It may be more efficient for high
accuracies and smooth functions.

The general syntax for the ‘quad’ function is:

q = quad (fun, a, b, tol, trace, p1, p2, …)

Where:

 ‘fun’ is the function to be integrated.


 ‘a’ and ‘b’ are the integration bounds.
 ‘tol’ is the desired absolute error tolerance (default = 10‐6).
 ‘trace’ is a variable that when set to a nonzero value causes additional computational
detail to be displayed.
 ‘p1’ and ‘p2’ are parameters that you want to pass to ‘fun’.
Qatar University
College of Engineering
Numerical Methods – GENG 300

 Note: if values of ‘tol’ and ‘trace’ are omitted then default values will be used in the
calculations.

 Exercise 7: use quad to integrate the following function:

1 1
f ( x)   s
( x  q )  0.01 ( x  r ) 2  0.04
2

between the limits x=0 to 1 for q=0.3, r=0.9, and s=6.

Differentiation:
The derivative, which serves as the fundamental vehicle for differentiation, represents the rate
of change of dependent variable with respect to an independent variable. Mathematically, the
derivative of a function can be presented as shown in the below equation:


lim
∆ → ∆

In this section, two MatLab functions will be introduced which can be used easily to find the
derivative in MatLab.

MatLab function: ‘diff’:


‘diff’ is a built – in MatLab function which when it is passed a one dimensional vector of length
“n”, this function returns a vector of length “n‐1” containing the difference between adjacent
elements, and these can be used to find the finite difference approximations of first derivatives.

 Exercise 8: use ‘diff’ function to differentiate the following function:


Qatar University
College of Engineering
Numerical Methods – GENG 300

f ( x)  0.2  25 x  200 x 2  675 x 3  900 x 4  400 x 5

From x = 0 to 0.8. Compare obtained results with the exact solution:

f ' ( x)  25  400 x  2025 x 2  3600 x 3  2000 x 4

 Solution steps:

1. Express f(x) as an anonymous function.


2. Generate a series of equally spaced values for the independent and dependent
variables.
3. Use the ‘diff’ function to determine the differences between adjacent elements
of each vector.
4. Compute the divided difference approximations of the derivative by performing
a vector division of “y” differences by the “x” differences.

Steps 1 to 4 are shown in figure 12.

5. The vector‘d’ now contains derivative estimates corresponding to the midpoint


between adjacent elements. So in order to develop a plot of the results, first you
must generate a vector holding the “x” values for the midpoint of each interval.
Qatar University
College of Engineering
Numerical Methods – GENG 300

6. Compute the values for the analytical derivative at a finer level of resolution to
include the plot for comparison as shown in figure 13.

As it can be shown from figure 13, the obtained results from ‘diff’ are close to the analytical
solution of the derivative.
Qatar University
College of Engineering
Numerical Methods – GENG 300

MatLab function: ‘gradient’:


The “gradient” function returns the differences as well, however, it does so in a manner that is
more compatible with evaluating derivatives at the values themselves rather than in the
intervals between the values. The general syntax of this function is:

fx = gradient (f)

where ‘f’ is a one dimensional vector of length ‘n’ and ‘fx’ is a vector of length ‘n’ containing
differences based on ‘f’. Same as with the ‘diff’ function, the first value returned is the
difference between the first and the second value. However, for the intermediate values, a
centered difference based on adjacent values is returned:

2
The last value is then computed as the difference between the final two values.

Note that the spacing between the points is assumed to be one. If the vector represents equally
spaced data, the following version divides all the results by the interval and hence returns the
actual values of the derivatives:

fx = gradient (f, h)

Where ‘h’ is the spacing between the points.


Qatar University
College of Engineering
Numerical Methods – GENG 300

 Exercise 9: use the ‘gradient’ function to differentiate the same equation of exercise 8.
Qatar University
College of Engineering
Numerical Methods – GENG 300

Solving ODEs using MATLAB solvers


Typical initial value problem (IVP)

t=0, y=y0
Example 1

dy
 4 e 0 .8 t  0 . 5 y
dt
Solve above ode from t=0 to 4 using ODE solvers in MATLAB. Analytical solution to this ODE

4 0.8t 0.5t
y (e  e )  2e0.5t
1.3
MATLAB code
Define function

function dy = program1(t,y)

dy = 4*exp(0.8*t)-0.5*y

Running the function M file

[t,y] = solver(@function,tspan,y0)

[t,y] = solver(@program1,tspan,y0)

Solvers: ode45, ode23, ode15s etc.


Qatar University
College of Engineering
Numerical Methods – GENG 300

Example 2. Solve above ODE using M file of Euler Method


function [T,Y] = eulers(f,a,b,ya,m)
%---------------------------------------------------------------------------
%EULER Euler's solution for y' = f(t,y) with y(a) = ya.
% Sample call
% [T,Y] = eulers('f',a,b,ya,m)
% Inputs
% f name of the function
% a left endpoint of [a,b]
% b right endpoint of [a,b]
% ya initial value
% m number of steps
% Return
% T solution: vector of abscissas
% Y solution: vector of ordinates
%
% NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1995
% To accompany the text:
% NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
% Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
% Prentice Hall, Inc.; USA, Canada, Mexico ISBN 0-13-624990-6
% Prentice Hall, International Editions: ISBN 0-13-625047-5
% This free software is compliments of the author.
% E-mail address: in%"[email protected]"
%
% Algorithm 9.1 (Euler's Method).
% Section 9.2, Euler's Method, Page 435
%---------------------------------------------------------------------------

h = (b - a)/m;
T = zeros(1,m+1);
Y = zeros(1,m+1);
T(1) = a;
Y(1) = ya;
for j=1:m,
Y(j+1) = Y(j) + h*feval(f,T(j),Y(j));
T(j+1) = a + h*j;
end
Qatar University
College of Engineering
Numerical Methods – GENG 300

Solution:

Here is the solution with number of steps =4 and 10.

Comment: As we increase number of step size (smaller h), numerical solutions are closer
to analytical one.
Qatar University
College of Engineering
Numerical Methods – GENG 300

Example 3. (Solving system of ODEs)

An example of a nonstiff system is the system of equations describing the motion of a rigid body
without external forces.

To simulate this system, create a function rigid containing the equations

function dy = rigid(t,y)
dy = [y(2)*y(3); -y(1)*y(3);-0.51*y(1)*y(2)];

In this example we change the error tolerances using the odeset command and solve on a time
interval [0 12] with an initial condition vector [0 1 1] at time 0.

options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);


[t,y] = ode45(@rigid,[0 12],[0 1 1],options);

Plotting the columns of the returned array Y versus T shows the solution

plot(t,y(:,1),'-',t,y(:,2),'-.',t,y:,3),'.')
Qatar University
College of Engineering
Numerical Methods – GENG 300

Example 4

Example of series reaction


Qatar University
College of Engineering
Numerical Methods – GENG 300

function dcdt = seriesreaction(t,c)


k1=1;k2=2;
dcdt = [-k1*c(1);k1*c(1)-k2*c(2); k2*c(2)];
running the fuction M file

[t,c] = ode45(@seriesreaction,[0 6],[8 0 0]);


plot(t,c(:,1),'-',t,c(:,2),'-.',t,c(:,3),'.')
legend('Ca','Cb','Cc')

Example 5. Solving initial value 2nd order or higher order equation problems

Solve the following equation for y1(0)=dy1/dt=1, for μ = 1


d 2 y1 2 dy
2
  (1  y1 ) 1  y1  0
dt dt

1st step: Convert the 2nd order ODE into a pair of first-order ODEs by defining
dy1
 y2
dt

2nd step: Write the original equation in terms of above definition:


dy 2
  (1  y1 ) y 2  y1  0
2

dt

________________________________________________________________

>> func=@(t,y) [y(2); 1*(1-y(1)^2)*y(2)-y(1)];


>> ode45(func, [0 20], [1 1])
Qatar University
College of Engineering
Numerical Methods – GENG 300

-1

-2

-3
0 2 4 6 8 10 12 14 16 18 20
Qatar University
College of Engineering
Numerical Methods – GENG 300

Stiffness

A stiff system is one involving rapidly changing component along with the slowly
changing ones.

 Rapid changing component can be ephemeral transient.


 Time step becomes critical.
 ODE solver suitable for stiff system to be used.

Example 1.6 (Solving system of ODEs with stiff systems)

An example of a stiff system is provided by the van der Pol equations in relaxation oscillation.
The limit cycle has portions where the solution components change slowly and the problem is
quite stiff, alternating with regions of very sharp change where it is not stiff.

To simulate this system, create a function vdp1000 containing the equations

function dy = vdp1000(t,y)
dy = zeros(2,1); % a column vector
dy(1) = y(2);
dy(2) = 1000*(1 - y(1)^2)*y(2) - y(1);

For this problem, we will use the default relative and absolute tolerances (1e-3 and 1e-6,
respectively) and solve on a time interval of [0 3000] with initial condition vector [2 0] at time
0.

[t,y] = ode15s(@vdp1000,[0 3000],[2 0]);

Plotting the first column of the returned matrix Y versus T shows the solution

plot(T,Y(:,1),'-o')
Qatar University
College of Engineering
Numerical Methods – GENG 300

Algorithms
ode45 is based on an explicit Runge-Kutta (4,5) formula, the Dormand-Prince pair. It is a one-
step solver – in computing y(tn), it needs only the solution at the immediately preceding time
point, y(tn-1). In general, ode45 is the best function to apply as a first try for most problems.

ode23 is an implementation of an explicit Runge-Kutta (2,3) pair of Bogacki and Shampine. It


may be more efficient than ode45 at crude tolerances and in the presence of moderate stiffness.
Like ode45, ode23 is a one-step solver.

ode113 is a variable order Adams-Bashforth-Moulton PECE solver. It may be more efficient


than ode45 at stringent tolerances and when the ODE file function is particularly expensive to
evaluate. ode113 is a multistep solver — it normally needs the solutions at several preceding
time points to compute the current solution.

The above algorithms are intended to solve nonstiff systems. If they appear to be unduly slow,
try using one of the stiff solvers below.

ode15s is a variable order solver based on the numerical differentiation formulas (NDFs).
Optionally, it uses the backward differentiation formulas (BDFs, also known as Gear's method)
that are usually less efficient. Like ode113, ode15s is a multistep solver. Try ode15s when
ode45 fails, or is very inefficient, and you suspect that the problem is stiff, or when solving a
differential-algebraic problem.
Qatar University
College of Engineering
Numerical Methods – GENG 300

ode23s is based on a modified Rosenbrock formula of order 2. Because it is a one-step solver, it


may be more efficient than ode15s at crude tolerances. It can solve some kinds of stiff problems
for which ode15s is not effective.

ode23t is an implementation of the trapezoidal rule using a "free" interpolant. Use this solver if
the problem is only moderately stiff and you need a solution without numerical damping. ode23t
can solve DAEs.

ode23tb is an implementation of TR-BDF2, an implicit Runge-Kutta formula with a first stage


that is a trapezoidal rule step and a second stage that is a backward differentiation formula of
order two. By construction, the same iteration matrix is used in evaluating both stages. Like
ode23s, this solver may be more efficient than ode15s at crude tolerances.
Qatar University
College of Engineering
Numerical Methods – GENG 300

M file of Runge Kutta methods


function [T,Y] = rk4(f,a,b,ya,m)
%---------------------------------------------------------------
------------
%RK4 Runge-Kutta solution for y' = f(t,y) with y(a) = ya.
% Sample call
% [T,Y] = rk4('f',a,b,ya,m)
% Inputs
% f name of the function
% a left endpoint of [a,b]
% b right endpoint of [a,b]
% ya initial value
% m number of steps
% Return
% T solution: vector of abscissas
% Y solution: vector of ordinates
%
% NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1995
% To accompany the text:
% NUMERICAL METHODS for Mathematics, Science and Engineering,
2nd Ed, 1992
% Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
% Prentice Hall, Inc.; USA, Canada, Mexico ISBN 0-13-624990-6
% Prentice Hall, International Editions: ISBN 0-13-625047-5
% This free software is compliments of the author.
% E-mail address: in%"[email protected]"
%
% Algorithm 9.4 (Runge-Kutta Method of Order 4).
% Section 9.5, Runge-Kutta Methods, Page 460
%---------------------------------------------------------------
------------
h = (b - a)/m;
T = zeros(1,m+1);
Y = zeros(1,m+1);
T(1) = a;
Y(1) = ya;
for j=1:m,
tj = T(j);
yj = Y(j);
k1 = h*feval(f,tj,yj);
k2 = h*feval(f,tj+h/2,yj+k1/2);
k3 = h*feval(f,tj+h/2,yj+k2/2);
k4 = h*feval(f,tj+h,yj+k3);
Qatar University
College of Engineering
Numerical Methods – GENG 300

Y(j+1) = yj + (k1 + 2*k2 + 2*k3 + k4)/6;


T(j+1) = a + h*j;
end

You might also like