0% found this document useful (0 votes)
6 views

NumericalMethods - Lesson 5 - 2021

Uploaded by

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

NumericalMethods - Lesson 5 - 2021

Uploaded by

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

University of Malawi – The

Polytechnic

BBME,BECE,BEEE &BETE
Computer Applications
ELE-COA-411
Lesson 5: Numerical Methods
with MATLAB
RS Bakolo, PhD
2021
Textbook: Introduction to MATLAB for Engineers by W. Palm III
Numerical Integration
2

The integral of f(x) interpreted as the area


A under the curve of f (x) from x = a to x =
b.

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
Numerical Integration
3

Illustration of (a) rectangular and (b)


trapezoidal numerical integration. Figure 9.1–
1, page 370.

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
Numerical Integration
4

 MATLAB can be used to obtain the


values of definite integrals. We will
explore two methods; Trapezoidal
integration and the Quadrature
Integration
 The values obtained using the above
methods are most of the times
approximations. The realisation of a
value close to the exact depends on the
number of samples taken on the
function.
RS Bakolo :The closer
UNIMA – The the Wednesday,
Polytechnic : 2021
24
values the
August 14, 20
Trapezoidal Integration
5

 Trapezoidal integration is achieved in


MATLAB by the function trapz with the
syntax trapz(x,y), where the array y
contains the function values at the
points contained in the array x.
 The function y has to be computed first with each
value in the array x before calling the function
trapz(x,y)
 Example: Use
 trapz to compute the integral
y sin xdx
0
RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20
24
6

 The exact value of the integral can be shown to = 2


 Using the trapz function:
x = linspace(0,pi,step)
y = sin(x)
result = trapz(x,y)
 If the value of ‘step’ is changed we have
step = 10, result = 1.9797; step=20, result = 1.9954
and step = 100, result = 1.9998.
 The more the number of steps used the better the
accuracy
RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20
24
Quadrature Integration
7

 Quadrature integration uses two


functions to evaluate definite integrals
i.e. quad and quad1
 quad integrates a function over
specified limits, based on the adaptive
Simpson’s rule. The adaptive rule uses
variable steps until a suitable one is
found to approximate the integral. A
𝑏
tolerance ‘tol’ can be specified to
𝑦 =∫ 𝑓 (𝑥 )𝑑𝑥
compute the integral 𝑎
to a specified
error….this can– The
RS Bakolo : UNIMA speed up
Polytechnic the
: 2021 process
Wednesday,
24
August 14, 20
8

 q=quad(fun,a,b) approximates the integral y


6
over the limit a and b within a default error1eof

 The first step in using quad is to write a function fun that


evaluates the function f(x).
 The m-file function would be
function y = myfunction(x)
%function to use with quad function
y=sin(x);
 quad can be used in two ways depending on the version of
MATLAB
RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20
24
9

 q=quad('myfunction',0,pi) and
q=quad(@myfunction,0,pi) will both return
the value of 2.0000
 To use quad with an unknown function use the
following syntax: choose a function handler, say
Z
Z = @(x)sin(x)
Q = quad(Z,0,pi)
 quadl is a more accurate version of quad and it is based
on the adaptive Labatto quadrature rule

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
10

Although the quad and quadl functions are more


accurate than trapz, they are restricted to
computing the integrals of functions and cannot be
used when the integrand is specified by a set of
points. For such cases, use the trapz function.
See example 9.1-2 on page 374
Polynomial Integration
q = polyint(p,C)returns a polynomial q
representing the integral of polynomial p with a
user-specified scalar constant of integration C. See
page 375.

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
Double Integrals
11

A = dblquad(fun, a, b, c, d) computes the


integral of f(x,y) from x = a to b, and y = c to
d. Here is an example using an anonymous
function to represent f(x,y) = xy2.

>>fun = @(x,y)x.*y^2;
>>A = dblquad(fun,1,3,0,1)

The answer is A = 1.3333. For more, see pages


376 to 377.

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
Triple Integrals
12

A = triplequad(fun, a, b, c, d, e, f)
computes the triple integral of f(x,y, z) from x = a
to b, y = c to d, and z = e to f. Here is an example
using an anonymous function to represent f(x,y,z)
= (xy -y2)/z.

>>fun = @(x,y,z)(x*y –y^2)/z;


>>A = triplequad(fun,1,3,0,2,1,2)

The answer is A = 1.8484. Note that the function


must accept a vector x, but scalar y and z. See
page 377.RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20
24
Exercise: Use Trapz() and quad() or quadl() –
Compare results
13

1 .5
y  e  x dx
0 .5

4
y e  x sin( 3 x ) dx
0

6.6
1
y   dx
1
x

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
Numerical Differentiation
14

 Numerical methods were developed to


convert a differential equation into a
difference equation so as to be able to
program it on a computer
 Three methods exist : Euler method, The
predictor-corrector method and Runge-Kutta
Methods. The details about these methods
are covered elsewhere in the curriculum.
 The student is encouraged to look at these
at their own time
RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20
24
Numerical Differentiation
15

Numerical differentiation: Illustration of three


methods for estimating the derivative dy/dx.
Figure 9.2–1, page 378.

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
The diff Function
16

MATLAB provides the diff function to use for


computing derivative estimates.
Its syntax is d = diff(x), where x is a vector of
values, and the result is a vector d containing the
differences between adjacent elements in x.
That is, if x has n elements, d will have n - 1
elements, where

d = [x(2) - x(1), x(3) - x(2), . . . , x(n) - x(n -1)].

For example, if x = [5, 7, 12, -20], then diff(x)


returns the vector [2, 5, -32]. For more, see pages
377-379 and Table
RS Bakolo 9.2-1.
: UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20
24
Polynomial Differentiation
17

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
Computing Gradients
18

Typing

[df_dx, df_dy] = gradient(f,dx,dy)

computes the gradient of the function f(x,y), where


df_dx and df_dy represent the partial derivatives,
and dx, dy represent the spacing.

For an example, see Figure 9.2-2 and the program


on pages 381-382.

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
The Euler Method
19

 The simplest algorithm for obtaining numerical values


for differential equations. The accuracy depends
largely on the step size chosen. This method is not
suitable for variables that are rapidly changing.
 For a first order Differential Equation

dy
r (t ) y
where r (t )is a known function, the solution
using
dt the Euler method will be approximated as

y (t k 1 )  y (t k )  r (t k ) y (t k )t from page 383
dy y (t  t )  y (t )

dt t

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
The Euler Method Cont’d
20

 With parameter like r = -10 and y(0)=2, set a


specified time range 0 t 0.5 , a program using
the for-loop can be written to obtain values of the
DE at specified step t
 for time = [delta:delta:0.5]
k = k+1;
y(k+1) = r*y(k)*delta
end

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
MATLAB Solvers
21

 MATLAB has a number of solvers implemented to


solve differential equations with better accuracy by
using variations of the predictor-corrector and
Runge-Kutta algorithms. Some of these algorithms
use variable step size.
 Functions based on these algorithms are called
solvers. Solvers that use variable step size include
ode23, ode45 and ode113. we will look at 23 and 45
 https://www.mathworks.com/company/newsletters/
articles/stiff-differential-equations.html

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
Solver syntax
22
dy
 For a first order ODE,
dt
 f (t , y ) the basic
syntax using ode23 will be:
[t, y] = ode23(‘ydot’,tspan,y0)
ydot is the name of the function file with inputs t
and y and its output being dy/dt i.e. (f(t, y)) and y(0)
is the initial value of the ODE. tspan is entered as a
vector having initial and final time ( t0 and t f) or
specific points like tspan = [2 5] means solutions for
DE will be obtained at 2 and 5

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24
example
23

 Response of an RC circuit with an input


excitation

dy
RC
dt
 y v(t )  RC 0.1
 The DE being if , and
y (0) 2
 , for starters we will assume the supply
v(t ) 0
voltage
dy
 First, we need a function that evaluatesdt  10 y

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


Known as ydot 24
example cont’d
24

 function ydot = rcresp(t,y)


ydot = -10*y;
 So the ode23 code will be as shown below
[t,y] = ode23('rcresp', [0 0.4], 2);
plot(t,y)
 Use of ode45 follows the same pattern as in ode23, otherwise
everything remains unchanged.
 As an exercice, write a program where the input voltage has
a defined function
 t
 v(t ) 10e sin( 2t / T ) where T is the sinusoidal oscillation
period of the source
RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20
24
Ode45 with same example
25
MATLAB recommends ode45 - based on the six step
fifth order Runge-Kutta – more accurate
The circuit model for zero input voltage v is

dy/dt + 10y = 0

First solve this for dy/dt:

dy/dt = -10y

Next define the following function file. Note that the order of
the input arguments must be t and y.

function ydot = RC_circuit(t,y)


ydot = -10*y
RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20
24
26

The function is called as follows, and the solution plotted


along with the analytical solution y_true. The intial
condition is y(0)=2.

[t, y] = ode45(@RC_circuit, [0, 0.5], 2);


y_true = 2*exp(-10*t);
plot(t,y,’o’,t,y_true),xlabel(’Time(s)’),...
ylabel(’Capacitor Voltage’)

Note that we need not generate the array t to evaluate


y_true, because t is generated by the ode45 function.

The plot is shown on the next slide and in Figure 9.3-2


on page 387..
RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20
24
27

RS Bakolo : UNIMA – The Polytechnic : 2021 Wednesday, August 14, 20


24

You might also like