You are on page 1of 110

MATLAB LECTURE

Aleksandar Tudzarov - Lecture


2
Matlab
Stands for MATrix LABoratory
Interpreted language
Scientific programming environment
Very good tool for the manipulation of
matrices
Great visualisation capabilities
Loads of built-in functions
Easy to learn and simple to use
Matlab Desktop
3

Workspace /
Current Directory
Command
Window

Command
History

Explore the Matlab Desktop


Variables
4
Dont have to declare type
Dont even have to initialise
Just assign in command window
>>
>> a=12; % variable a is assigned
12
Matlab comment
prompt suppress operator
assign
command
operator
output Try the same line without the
semicolon and comments
Variables (continued )

View variable contents by simply typing


the variable name at the command
prompt
>> a
a=
12
>>
>> a*2
a=
24
>>
Workspace
6

The workspace is Matlabs memory


Can manipulate variables stored in the

workspace
>> b=10;
>> c=a+b
c=
22
>>
Workspace (continued )
7

Display contents of workspace


>> whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
c 1x1 8 double array
Grand total is 3 elements using 24 bytes
>>

Delete variable(s) from workspace


>> clear a b; % delete a and b from workspace
>> whos
>> clear all; % delete all variables from workspace
>> whos
Matlab help commands
8

help
>> help whos % displays documentation for the
function whos
>> lookfor convert % displays functions with convert in the
first help line

Start Matlab help documentation


>> helpdesk
Matrices
9

Dont need to initialise type, or


dimensions
>>A = [3 2 1; 5 1 0; 2 1 7]
A=
square brackets to define matrices
3 2 1
5 1 0 semicolon for next row in matrix
2 1 7
>>
Manipulating Matrices
10

Access elements of a matrix


A=
>>A(1,2) 3 2 1

ans= indices of matrix element(s)


5 1 0
2 1 7
2
Remember Matrix(row,column)

Naming convention Matrix variables start

with a capital letter while vectors or


scalar variables start with a simple letter
The : operator
11

VERY important operator in Matlab


Means to

>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
>> 1:2:10
ans = Try the following
1 3 5 7 9 >> x=0:pi/12:2*pi;
>> y=sin(x)
The : operator and matrices
12

A=
>>A(3,2:3) 3 2 1
ans = 5 1 0
2 1 7
1 7
>>A(:,2)
ans =
2
1
Whatll happen if you type A(:,:) ?
1
Manipulating Matrices A=
3 2 1
5 1 0
13
2 1 7

>> A' % transpose B=


1 3 1
>> B*A % matrix multiplication 4 9 5
>> B.*A % element by element multiplication
2 7 2

>> B/A % matrix division


>> B./A % element by element division Enter matrix B
into the Matlab
>> [B A] % Join matrices (horizontally) workspace

>> [B; A] % Join matrices (vertically)

Create matrices A and B and try out the the matrix operators in this slide
Scripts
14

Matlab editor
Use scripts to execute a series of
Matlab commands

Matlab
Desktop
Press to create
new m-file in the
matlab editor
Scripts (continued)
15
Scripts will manipulate
and store variables and
matrices in the Matlab
Workspace (memory).
They can be called from
the Matlab command line
by typing the (case Will be slightly
sensitive!) filename of the different in Linux
script file.
>> myscript
Scripts can be opened in
the editor by the following
>> open myscript
Highlight a few lines of your
script by left- clicking and
dragging the mouse over the
lines. Right-click the
highlighted lines and select
Evaluate Selection.
Basic Task: Plot the function
sin(x) between 0x4
Create an x-array of 100 samples between 0
and>>x=linspace(0,4*pi,100);
4.

Calculate sin(.) of the x-array


>>y=sin(x); 1

0.8

0.6

0.4

0.2

Plot the y-array


0

-0.2

>>plot(y) -0.4

-0.6

-0.8

-1
0 10 20 30 40 50 60 70 80 90 100
Plot the function e-x/3sin(x)
between 0x4
Create an x-array of 100 samples between 0
and 4.
>>x=linspace(0,4*pi,100);

Calculate sin(.) of the x-array


>>y=sin(x);

Calculate e -x/3 of the x-array


>>y1=exp(-x/3);

Multiply the arrays y and y1


>>y2=y*y1;
Plot the function e-x/3sin(x)
between 0x4
Multiply the arrays y and y1 correctly
>>y2=y.*y1;
Plot the y2-array
0.7

>>plot(y2) 0.6

0.5

0.4

0.3

0.2

0.1

-0.1

-0.2

-0.3
0 10 20 30 40 50 60 70 80 90 100
Display Facilities
plot(.) 0.7

0.6

0.5

Example: 0.4

>>x=linspace(0,4*pi,100);
0.3

0.2

>>y=sin(x); 0.1

>>plot(y) -0.1

>>plot(x,y) -0.2

-0.3
0 10 20 30 40 50 60 70 80 90 100

stem(.) 0.7

0.6

Example:
0.5

0.4

>>stem(y) 0.3

0.2

>>stem(x,y) 0.1

-0.1

-0.2

-0.3
0 10 20 30 40 50 60 70 80 90 100
Display Facilities
title(.)
>>title(This is the sinus function)
This is the sinus function
1

0.8

xlabel(.) 0.6

0.4

0.2

>>xlabel(x (secs))
sin(x)
0

-0.2

-0.4

ylabel(.) -0.6

-0.8

-1
0 10 20 30 40 50 60 70 80 90 100

>>ylabel(sin(x))
x (secs)
Operators (relational,
logical)
== Equal to
~= Not equal to
< Strictly smaller
> Strictly greater
<= Smaller than or equal to
>= Greater than equal to
& And operator
| Or operator
Flow Control
if
for
while
break
.
Control Structures
If Statement Syntax
Some Dummy Examples

if ((a>3) & (b==5))


if (Condition_1) Some Matlab Commands;
Matlab Commandsend
elseif (Condition_2) if (a<3)
Matlab Commands Some Matlab Commands;
elseif (b~=5)
elseif (Condition_3) Some Matlab Commands;
Matlab Commandsend
else if (a<3)
Matlab Commands Some Matlab Commands;
end else
Some Matlab Commands;
end
Control Structures
Some Dummy Examples
For loop syntax
for i=1:100
Some Matlab Commands;
for i=Index_Array end

Matlab Commands for j=1:3:200


Some Matlab Commands;
end end

for m=13:-0.2:-21
Some Matlab Commands;
end

for k=[0.1 0.3 -13 12 7 -9.3]


Some Matlab Commands;
end
Control Structures
While Loop Syntax

Dummy Example
while (condition)
Matlab Commandswhile ((a>3) & (b==5))
Some Matlab Commands;
end end
Use of M-File
Click to
create a new
M-File

Extension .m
A text file containing script or function or program to run
Use of M-File Save file as Test.m

If you include ; at the


end of each statement,
result will not be shown
immediately
Writing User Defined Functions

Functions are m-files which can be executed by


specifying some inputs and supply some desired
outputs.
The code telling the Matlab that an m-file is actually
function
a function is out1=functionname(in1)
function out1=functionname(in1,in2,in3)
function [out1,out2]=functionname(in1,in2)

You should write this command at the beginning of


the m-file and you should save the m-file with a file
name same as the function name
Writing User Defined Functions
Examples
Write a function : out=squarer (A, ind)

Which takes the square of the input matrix if the input


indicator is equal to 1
And takes the element by element square of the input
matrix if the input indicator is equal to 2

Same Name
Writing User Defined Functions
Another function which takes an input array and returns the sum
and product of its elements as outputs

The function sumprod(.) can be called from command window or an


m-file as
Notes:
% is the neglect sign for Matlab
(equaivalent of // in C). Anything after it
on the same line is neglected by Matlab
compiler.
Sometimes slowing down the execution is
done deliberately for observation
purposes. You can use the command
pausepause
for %wait until any key
this purpose
pause(3) %wait 3 seconds
Functions
32

Programming in Matlab.
Users can write functions which can be called from the
command line.
Functions can accept input variable(s)/matrice(s) and will output
variable(s)/matrice(s).
Functions will not manipulate variable(s)/matrice(s) in the
Matlab Workspace.
In Matlab functions closely resemble scripts and can be written
in the Matlab editor. Matlab functions have the function
keyword.
Remember that the filename of a function will be its calling
function name.
Dont overload any built-in functions by using the same
filename for your functions or scripts!
Functions can be opened for editing using the open command.
Many built-in Matlab functions can also be viewed using this
command.
Functions (continued)

33 function name input


output
>> I=iterate(5)
I= 1 4 9 16
25
function keyword

help lines for function

for statement block


Access the comments of
your Matlab functions
>> help iterate Make sure you save changes to the
m-file before you call the function!
Functions (continued)

34

Functions can have many


>> [i j]=sort2(2,4) outputs contained in a matrix
i=
4
j=
2
if statement
>> block

Remember to use the


Matlab help command for
syntax
>> help if
More flow control
35

While statement block Switch statement block

Without ; to
print output

i=
4
i=
16
i= Method is linear
256 >>
Debugging
36

Debug menus
Set breakpoints to stop the execution of code
>> [i j]=sort2(2,4)
K>>
K>> whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
Grand total is 2 elements using 16 bytes
K>> a
a=
2
K>> return
i= local function
workspace
4 Click mouse on the left
j= of the line of code to
2
exit debug create a breakpoint
mode
Visualisation - plotting data
37

>> figure % create new figure


>> t=0:pi/12:8*pi;
>> y=cos(t); Plot style

>> plot(t,y,b.-')
Investigate the function
>> y=A*cos(w*t+phi);
for different values of phi (eg: 0, pi/4, pi/3,
pi/2), w (eg: 1, 2, 3, 4) and A (eg: 1, 0.5, 2). Use
the hold on Matlab command to display your
plots in the same figure. Remember to type A = amplitude
hold off to go back to normal plotting mode. phi = phase
Try using different plot styles (help plot) w = angular frequency = 2*pi*frequency
38

3
Generating Vectors from
functions
zeros(M,N) MxN matrix of zeros x = zeros(1,3)
x =
0 0 0

ones(M,N) MxN matrix of ones


x = ones(1,3)
x =
1 1 1
rand(M,N) MxN matrix of
uniformly x = rand(1,3)
distributed random
x =
numbers on (0,1)
0.9501 0.2311 0.6068
Matrix Index
The matrix indices begin from 1 (not 0 (as in C))
The matrix indices must be positive integer

Given:

A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers
or logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.

41
Some Built-in functions
mean(A):mean value of a vector
max(A), min (A): maximum and minimum.
sum(A): summation.
sort(A): sorted vector
median(A): median value
std(A): standard deviation.
det(A) : determinant of a square matrix
dot(a,b): dot product of two vectors
Cross(a,b): cross product of two vectors
Inv(A): Inverse of a matrix A
Indexing Matrices

Given the matrix:


A = n
0.9501 0.6068 0.4231
Then: m
0.2311 0.4860 0.2774
A(1,2) = 0.6068 Aij ,i 1...m, j 1...n
A(3) = 0.6068
index (i 1)m j
A(:,1) = [0.9501

1:m ]
0.2311


A(1,2:3)=[0.6068 0.4231]


Adding Elements to a Vector or a
Matrix
>> A=1:3 >> C=[1 2; 3 4]
A= C=
1 2 3 1 2
>> A(4:6)=5:2:9 3 4
A= >> C(3,:)=[5 6];
1 2 3 5 7 9 C=
1 2
>> B=1:2 3 4
B= 5 6
1 2
>> B(5)=7; >> D=linspace(4,12,3);
B= >> E=[C D]
1 2 0 0 7 E=
1 2 4
3 4 8
5 6 12
Graphics - 2D Plots
plot(xdata, ydata, marker_style);
For example: Gives:

>> x=-5:0.1:5;
>> sqr=x.^2;
>> pl1=plot(x, sqr, 'r:s');
Graphics - Overlay Plots
Use hold on for overlaying graphs
So the following: Gives:

>> hold on;


>> cub=x.^3;
>> pl2=plot(x, cub,b-o');
Graphics - Annotation
Use title, xlabel, ylabel and legend for
annotation

>> title('Demo plot');


>> xlabel('X Axis');
>> ylabel('Y Axis');
>> legend([pl1, pl2], 'x^2', 'x^3');
Graphics - Annotation
Graphics-Stem()
stem()is to plot discrete sequence data
The usage of stem() is very similar to plot()
cos(n/4)
1

>> n=-10:10;
>> f=stem(n,cos(n*pi/4)) 0.5

>> title('cos(n\pi/4)')
>> xlabel('n') 0

-0.5

-1
-10 -5 0 5 10
n
subplots
Use subplots to divide a plotting
window into several panes.
Cosine Sine
1 1

0.8 0.8
>> x=0:0.1:10;
>> f=figure; 0.6 0.6

>> f1=subplot(1,2,1); 0.4 0.4

>> plot(x,cos(x),'r'); 0.2 0.2

>> grid on; 0 0

>> title('Cosine')
-0.2 -0.2
>> f2=subplot(1,2,2);
-0.4 -0.4
>> plot(x,sin(x),'d');
>> grid on; -0.6 -0.6

>> title('Sine'); -0.8 -0.8

-1 -1
0 5 10 0 5 10
Save plots
Use saveas(h,'filename.ext') to save
a figure to a file.

Useful extension types:


bmp: Windows bitmap
>> f=figure;
emf: Enhanced metafile
>> x=-5:0.1:5;
eps: EPS Level 1
>> h=plot(x,cos(2*x+pi/3));
fig: MATLAB figure
>> title('Figure 1');
jpg: JPEG image
>> xlabel('x');
m: MATLAB M-file
>> saveas(h,'figure1.fig')
tif: TIFF image, compressed
>> saveas(h,'figure1.eps')
3D PLOTS
52

x = 2:0.5:4; y=1:0.5:3;
[X,Y] = meshgrid(x,y);

f (x, y) = (x 3)2 ( y 2)2


3D PLOTS
53
Primer
54

>> [X,Y] = meshgrid(-2:0.1:2, -2:0.1:2);


>> f = -X.*Y.*exp(-2*(X.^2+ Y.^2));
>> mesh (X,Y,f), xlabel('x'), ylabel('y'), grid
55

>> fmax = max(max(f))


>> kmax = find(f==fmax)
>> Pos = [X(kmax), Y(kmax)]
>> contour(X,Y,f)
>> xlabel('x'), ylabel('y'), grid, hold on
>> plot(X(kmax),Y(kmax),'*')
>> text(X(kmax),Y(kmax),'Maximum')

>> fplot(' 2*sin(x+3)', [-1 1])


File I/O
Matlab has a native file format to save
and load workspaces. Use keywords
load and save.
In addition MATLAB knows a large
number of popular formats. Type help
fileformats for a listing.
In addition MATLAB supports C style
low level file I/O. Type help fprintf
for more information.
Practice Problems
Plot the following signals in linear scale

x(t ) sin(3t ) 5 t 5
y (t ) e 2t 3 0t 5
Plot the following signals, use log scale for y-axis

x(t ) e t 2 (2t 1) 0 t 10
Plot the real part and imaginary part of the following signal

x(t ) e 0.5t j ( t / 3) 0 t 10
For the signal in previous question, plot its phase and magnitude
58

4
Simulink
Graphical block diagram capability
Can drag and drop components (called
blocks)

Extensive library of blocks available


DSP Blockset is one

Real-time Workshop
Simulink
An environment for building and
simulating models.
Continuous, discrete or hybrid

systems
Linear and nonlinear components

Can simulate asynchronous events

Closely integrated with MATLAB and


toolboxes
Simulink Model

AAtypical
typicalSimulink
Simulinkmodel
modelincludes
includesSources,
Sources,Systems
Systemsand
and
Sinks.
Sinks.

Sources Systems Sinks

1. Sinewaves 1. Interconnection 1. Displays scopes


2. Function of Linear and 2. FFT scopes
Generators Nonlinear blocks 3. To MATLAB
3. From MATLAB workspace
workspace 4. To disk files
4. From Disk Files
Simple Simulink Model

Scope
2

Sine Wave Gain


Sum Scope1

Gain1 1
0.9 Unit Delay
z
Simulink Block Libraries
Simulink contains block libraries, which contain
components that can be used to build models.

A block library can itself have other libraries as blocks.


Example: When you first start Simulink:

In1 Out1
Scope XY Graph
Sources Sinks Discrete Linear Nonlinear Connections
0
Blocksets & Simulink Block Library 2.2
Toolboxes Demos Display
Copyright (c) 1990-1998 by The MathWorks, Inc.

untitled.mat simout

To File To Workspace

Sinks is itself a STOP

block library of Stop Simulation

components
Building a Simulink Model
11

Gain
Gain Sum
Sum

11 11
Sine Wave Sum ss s+1
s+1
Integrator
Integrator Transfer Fcn
Transfer Fcn
Model Window
x'x'==Ax+Bu
Ax+Bu (s-1)
(s-1)
yy==Cx+Du
Cx+Du s(s+1)
s(s+1)
State-Space
State-Space Zero-Pole
Zero-Pole

du/dt
du/dt
1
Constant Derivative
Derivative DotProduct
Dot Product
Signal Step

Use left Generator


KK 11

mouse Ramp Sine Wave Repeating


Sequence
Matrix
Matrix
Gain
Gain
Slider
Slider
Gain
Gain

button to Discrete Pulse Pulse Chirp Signal

drag blocks
Generator Generator

12:34 Linear Blocks


to the Clock

untitled.mat
Digital Clock

[T,U]
Library
model From File From
Workspace

window
Random Uniform Random Band-Limited
Number Number White Noise

Sources Library
Connecting Blocks
1

Sine Wave Gain Sum 1

Sine Wave Gain Sum

Use the left mouse


button to click on a
port
and drag a
connection
1
1
Sine Wave Gain Sum Scope
Sine Wave Gain Sum Scope

Use the right mouse


button to click on a
line
to drag a branch line
More Simulink Basics

Double-click on a block to open its Dialog


Box. Parameters for the block can be set in
this box.
Example: setting the amplitude, frequency, phase
and sampling frequency of the sinewave source.

Click on the HELP button on the Dialog Box


for a block to launch the Web Browser
opened at the HELP file for that component.

After selecting a block, you can rotate, flip


or resize it. These operations are useful in
creating a more readable block diagram.
Setting Simulation
Parameters
The
The Simulation
Simulation menu
menu item
item on
on the
the models
models
window
window can
can be
be used
used to
to set
set simulation
simulation
parameters.
parameters.

You
You can
can specify
specify the
the appropriate
appropriate solver
solver that
that is
is
to
to be
be used.
used. For
For discrete-time
discrete-time systems
systems use
use
the
theFixed-step
Fixed-stepDiscrete
Discretesolver
solverififthere
thereis
isonly
onlyaasingle
single
sample
sampletime.
time.
The
TheVariable-step
Variable-stepDiscrete
Discretesolver
solverforformultirate
multirate
systems.
systems.

You
You can
can also
also specify
specify variables
variables that
that are
are to
to be
be
obtained
obtained from
from or
or returned
returned to
to the
the MATLAB
MATLAB
workspace.
workspace.
Subsystems
You can select portions of your model using the mouse and
make them into a subsystem.

Function Zero-Order This figure demonstrates Original


Generator Hold Signal
a simple first order IIR filter

In1
Out1

Filtered
My 1st order Signal
filter

1 B0
1
Function Zero-Order This figure demonstrates Original In1
Generator Hold Signal B0 Out1
a simple first order IIR filter Sum

1
In1
Out1 A1
z
Filtered A1 Unit Delay
My 1st order Signal
filter

You can created a masked subsystem, that hides the complexity


of the subsystem from the user.
69

Numerical methods
Solving Algebraic Equations
From linearity, it is easy to solve the
equation
3 x 7 12

or even the system of equations


2 x 5 y 12
x 6y 3

But what about an


x equation like
tan( x)e 1 0
Solving Algebraic Equations
In general, given a continuous function f,
how do you find the (any, all) solutions to

f ( x) 0
f(x) Graph of f(x) versus x

roots of f
Iterative Methods
Iterative methods start with a guess,
labeled x0 and through easy
calculations, generate a sequence
x1, x2, x3,

Goal is that
lim xn x
n

sequence satisfies
f x 0

convergence to a and x* is a solution


limit, x*
Rate of Convergence
Suppose {xn} is a sequence, converging to a limit x*. Let en denote the
difference x*-xn

If there is a constant C, and positive exponent such that for large n



en 1 C en

then the sequence {xn} converges with order to x*.


linear convergence is =1
The sequence 3, 2.5, 2.25, 2.125, 2.0625, converges linearly
to 2 (here C = )
The sequence 3, 2.1, 2.01, 2.001, 2.0001, converges linearly to
2 (here C=0.1)
quadratic convergence is =2
The sequence3,2 2 ,2 4 ,2 16 ,2 256 ,2 65536 ,...
1 1 1 1 1
converges
quadratically to 2 (here C=1)
Stopping Criteria
Suppose {xn} is a sequence, converging to a limit x*. The
limit x* has the property f(x*)=0. Let tol be a positive
number
But we dont know x*
Absolute
Absolute (in x) |xn xn-1| < tol
|xn x*| < tol
Relative
Relative (in x) |xn xn-1| < tol |xn|
|xn x*| < tol |x*|

Absolute in f
Sometimes (in bisection, for example) we
can bound this, even without knowing x*.
|f(xn)| < tol
Bisection Method, basic idea

f xL 0
Suppose f is continuous, f xR 0

f xR
Intermediate value theorem
xL xM
f xL xR

There must be a root x* between xL and xR.


Let xM be the midpoint. |xM-x*|0.5|xR-xL|
Based on sign of f xM replace either xL or xR with xM,
and repeat.
Simple pseudo-code:
Bisection
% Start with xL, xR,
fL = f(xL); fR = f(xR);
while StoppingCriteriaNotMet
xM = 0.5*(xL+xR);
yM = f(xM);
if fL*fM<0 % replace R with M
xR = xM;
else
xL = xM;
end
end
Newtons Method:
motivation
Graph of h(x), the straight-line
approximation to f at x0

graph of f(x)

x0 x
functions are equal at x0
h x0 f x0
slope of h (everywhere)
equals the slope of f at x0
h ' x f ' x0 for all x
graph of h is straight line
h x ax b for some a, b

h x f x0 f ' x0 x x0
Newtons Method:
motivation
Graph of h(x), the straight-line
approximation to f at x0

graph of f(x)

x0 x

h x f x0 f ' x0 x x0
Approximation to f, near x0

Approximate solving f(x)=0 by solving (easier) h(x)=0


f x0
h x 0 x x0 '
f x0
Newtons Method: iteration
Graph of h(x), the straight-line
approximation to f at x0

graph of f(x)

*
xapp x0 x

f x0 Algorithm: Repeat, using x*app


x *
x0 '
f x0
app
as the initial point.

f xk
xk 1 xk '
f xk General form of the iteration
Newtons Method: iteration
f xk
xk 1 xk ' General form of the iteration
f xk
Facts about Newtons method to solve f(x) = 0
At each step, need to evaluate the function and its
derivative at a single point
Not guaranteed to converge. It might cycle, and it
might diverge.
If it does converge, the convergence is quadratic.
More complicated than bisection, can work better.
Generalizes to N equations in N unknowns
Function handles and feval
Several Matlab functions solve your problem by repeatedly
calling functions that you supply
finding zeros of a function (fzero) New data type: add to list
with double, cell, char,
Integrating a function (trapz, quad) struct.
Integrating a differential equation (ode45)

minimizing a function (fminbnd, fminsearch)

For example, in calling fzero, you must pass a reference-to-


the-function (called a function handle) that you want fzero
to find the zeros of. Use the @ operator to get the reference.
Any function (built-in,
>> shan = @sin; mfile, etc)
>> class(shan)
>> feval(shan,linspace(0,pi,5))
Function handles and feval

General syntax for feval is


function handle

[out1,out2,] = feval(FuncH,arg1,arg2,)

Output argument list Input argument list


Differentiation

For a function f, the derivative of f at x is


defined as the limit
f x h f x
f x : lim
h 0 h

The derivative of f at x is the slope of


the graph of the function at x.
Numerical Differentiation

A common manner to numerically estimate of the


derivative is a finite-difference of theNoquotient
limit, just a
f x h f x fixed, nonzero h
f x lim
h 0 h

The forward difference at x with stepsize h (h>0)


f x h f x
is f f x
h

f x f x h
The backwardf bdifference
x at x with stepsize h
h
(h>0) is
f x h f x h
f c x
2h
The centered difference at x with stepsize h
(h>0) is
Numerical Differentiation

If the function values are only available as data


pairs
x1 , y1 , x2 , y2 , , xM , yM
With each yi = f(xi), then the finite differences are
computed with the data y(assuming
i 1 yi
the xi are
forward at xi
sorted) xi 1 xi

yi yi 1
backward at xi
xi xi 1

yi 1 yi 1
centered at xi
xi 1 xi 1
What are we actually
computing?
Average values: If g is integrable, then the average value of g
over the interval [a,b] is defined as

g x dx
b

g[avg :
a
a ,b ]
ba
In other words, avg is the constant which satisfies
g [ a ,b ]
ie., the only constant
function which has the
same integral over
[a,b] as does g is the

g x dx b a g
b
avg constant function
[ a ,b ] whose value is
a avg
g [ a ,b ]
What are we actually
computing?
If f is differentiable, then

f x dx
a
f b f a
If ab, then
b
1 f (b) f (a)
f x dx
ba a ba
Average value of f over the interval (a,b)
What are we computing?
(contd)
Mean-Value Theorem: If f is
differentiable on (a,b)
continuous on [a,b]
then there is an y(a,b) such that

f b f a b a f y
What are we computing?
(contd)
This gives two interpretations of the finite-
f b f a
difference b
1
f x dx
ba ba a
Average value of f over the interval (a,b)

f b f a
f y for some y a, b
ba
exact value of f at some point in interval (a,b)
What are we computing?
(contd)
So, two interpretations of the finite-
difference
f x h f x
xh
1
f d
h h x

Average value of f over the interval (x,x+h)

f x h f x
f y some y x, x h
h
Bounding the Errors

The second derivative of f gives how quickly the first


derivative is changing.
Bound the error between the finite difference estimate
and the actual derivative using
finite difference is the exact answer for some
intermediate value
intermediate first-derivative values differ because of

2nd derivative.
use Taylor series to bound the error as
f x h f x h
f x max f y
h 2 y( x , x h )
Error between finite difference and actual derivative
Higher Order derivatives

Use the finite difference estimates of the 1 st


derivative to compute the 2nd derivative,
based on f x h f x
f x lim
h 0 h
f(x h) f(x) f(x) f(x h)

f fd x h h
h

Example: Use backward formula, with forward


derivatives f ( x h) 2 f ( x ) f ( x h)
2
h
Matlab functions

Theres not much in the way of functions for numerical


differentiation. Essentially, you employ the expressions
we introduced today.

If X is a row vector, then


diff(X) returns
[X(2)-X(1) X(3)-X(2) X(end)-X(end-1)]

Given row vectors Xdata, Ydata (each 1-by-N)


Z = diff(Ydata)./diff(Xdata)
Z is 1-by-(N-1). Element Z(i) is the forward derivative at
X(i)
Integration
For a function f,
graph of f(x)

a b x
The integral of f from a to b is the area
under the graph of the function.
If f is continuous, then the area is well
defined, as the common limit of upper and
lower sums. Theb
integral is denoted

f ( x)dx
a
Integration: Fundamental theorem of
Calculus
If a function g is the antiderivative of f, namely
g x f x

for all x, then the fundamental theorem of calculus


gives that the integral of f can be computed by
evaluatingb g
f ( x)dx g (b) g (a)
a

But finding antiderivatives can be hard


In many cases of engineering and scientific (and
economics, etc) interest
the functions do not have known antiderivatives, so
the integral (area) must be approximated by a finite
number of function evaluations.
Numerical Integration:
Trapezoid
graph of f(x)

approximate this region

a b x
ba
AREA f b f a
2
graph of f(x)

with this trapezoid

a b x
Composite Trapezoid

graph of f(x)

Apply trapezoid approximation to


k subdivisions of [a b]

a b x

graph of f(x)

trapezoid approximation with 3


subdivisions of [a b]

a b x
Composite Trapezoid

graph of f(x)

trapezoid approximation with 3


subdivisions of [a b]

a b x
h
Let h=(b-a)/3. The sum of all the approximations is
h
I f (a) f (a h) h f (a h) f (a 2h) h f (a 2h) f (b)
2 2 2
h
f (a) 2 f (a h) 2 f (a 2h) f (b)
2
Derivation of Simpsons Rule:
Suppose f(x)=x3
b

x
3
dx
4

1 4
b a4
a


1
4

b a b 3 b 2 a ba 2 a 3


1
b a 2 b 3 1 b 3 b 2 a ba 2 1 a 3 2 a 3
4 3 3 3 3


1
b a 2 b 3 1 b a 3 2 a 3
4 3 3 3

1 2 3 8 b a 3 2 3
b a b a
4 3 3 2 3

1 3 b a
3

b a b 4 a3
6 2


1
b a f a 4 f b a f b
6 2
Simpsons Rule: Suppose
f(x)=x2

dx
x 2

3

1 3
b a3
a


1 ba
b a f a 4 f f b
6 2
Simpsons Rule: Suppose
f(x)=x

a x dx
2

1 2
b a 2


1 ba
b a f a 4 f f b
6 2
Simpsons Rule: Suppose
f(x)=1

1 dx b a
a


1
b a f a 4 f b a f b
6 2
Simpsons Rule: Derivation

Put all of those together, along with


b b

f ( x)dx f ( x)dx
a a

b b b

f ( x) g ( x) dx f ( x)dx g ( x)dx
a a a

Hence: If f is any cubic polynomial, then


b
1 ba
f ( x)dx b a f a 4 f f b
a
6 2
This is the basis for Simpsons rule.
Simpsons Rule

For any function f, the Simpsons approximation to

b
is f ( x)dx
a
and in the middle

1 ba
I Simpson b a f a 4 f f b
6 2

Evaluate the function at the endpoints


Composite Simpsons Rule

graph of f(x)

Simpson on 3 subdivisions of [a b]
h=(b-a)/3

a b x
h
h
I S1 f a 4 f a h
2 f a h
6
h
I S2 f a h 4 f a 3 h 2 f a 2h
6

h
I S3 f a 2h 4 f a 5 h 2 f b
6
Add them up. Total of 7 function evaluations.
Composite Simpsons Rule

graph of f(x)

Simpson on 2 subdivisions of [a b]
h=(b-a)/2

a b x
h
I S1 f a 4 f a h 2 f a h
6
h 5 function
I S2 f a h 4 f a 3h 2 f b evaluations
6
giving the approximation as IS=I1S+I2S
h
f (a) 4 f (a h 2 ) 2 f a h 4 f a 3h 2 f b
6
Numerical Integration:
Ad-Hoc stopping criteria
Pick a method (trapezoid, or Simpsons).
Set a stopping tolerance TOL.
Pick k, an initial number of subdivisions
Iterate as below
Apply composite method using k divisions
Apply composite method using 2k divisions
If answers are within TOL, stop, and return the 2k
division answer
If answers are not within TOL, increase k and repeat.

With only this much analysis, the computed answer


is not specifically known to be accurate to any
specific precision.
Adaptive Stepsize

Only use small h (the stepsize) where the


convergence demands it. Recursive implementation
is straightforward.

function I = adr(fh,a,b,tol)
Compute I1 using 1 subdivision
Compute I2 using 2 subdivisions
If the answers are within tol, I = I2;
Else
m = (a+b)/2;
ILeft = adr(fh,a,m,tol/2);
IRight = adr(fh,m,b,tol/2);
I = ILeft + IRight;
end
Error Analysis

More error analysis can be done making


assumptions about the magnitude of the
derivatives of f
A bound on the magnitude of 3rd derivative
of f yields accuracy bounds for the
Trapezoid method
A bound on the magnitude of 5th derivative
of f yields accuracy bounds for the
Simpsons method
Matlab functions

trapz
fixed trapezoidal approximation
data is vectors x and y (y represents f(x))
1 division between each data pair
quad
Adaptive Simpsons method
Data is function handle, interval endpoints,
stopping tolerance

You might also like