0% found this document useful (0 votes)
14 views6 pages

Matlab 8 Slide

Uploaded by

Yazan Z Awad
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)
14 views6 pages

Matlab 8 Slide

Uploaded by

Yazan Z Awad
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/ 6

Lecture 9 Computer Applications 0933201 Chapter 8

UNIVERSITY OF JORDAN The area A under the curve of f (x) from x = a to x = b.


Figure 8.1–1
Faculty of Eng. and Tech.
Electrical Engineering Dept.
Instructor: Ziad R. Al-Khatib
b
Introduction to MATLAB 7 A= ³ f(x) dx
a
for Engineers
William J. Palm III
Chapter 8
Numerical Calculus and Differential Equations
Find the following
integral:

A = ³ sin(x2) dx Fresnel’s cosine integral


Z.R.K Z.R.K
Z.R.K 8-2

Illustration of (a) Rectangular and (b) Trapezoidal Numerical integration functions. Table 8.2–1
numerical integration. Figure 8.2–1 Command Description
quad(’fun’,a,b,tol) Uses an adaptive Simpson’s rule to
compute the integral of the function
’fun’ with a as the lower
integration limit and b as the upper
limit. The parameter tol is optional.
tol indicates the specified error
tolerance.
quadl(’fun’,a,b,tol)Uses Lobatto quadrature to
compute the integral of the
function ’fun’. The rest of the
syntax is identical to quad.

trapz(x,y) Uses trapezoidal integration to


compute the integral of y with
respect to x, where the array y
contains the function values at the
points contained in the array x.
Z.R.K Z.R.K
8-3 8-4

MATLAB function quad implements an adaptive A potential problem with integration.


version of Simpson’s rule, while the quadl function A function having a singularity at x = 1. Figure 8.1–2
is based on an adaptive Lobatto integration algorithm.
To compute the sine integral, type:
The plot of
>>A = quad(’sin’,0,pi) y = 1/(x-1)
The answer given by MATLAB is 2, which is correct.
We use quadl the same way; namely,
>> A = quadl(’sin’,0,pi). ³ 1/(x-1) dx
Integration example: Create the function: = ln|x–1|
function c2 = cosxsq(x)
% cosine x squared function Fresnel’s int. Some integrals
c2 = cos(x.^2); have infinite
Note that you must use array exponentiation ( .^ ). values.These are
The quad function is called as follows: called improper
>>quad(’cosxsq’,0,sqrt(2*pi)) integrals.
The result is 0.6119.
Z.R.K Z.R.K
8-5 8-6

Z.R.K 2008 Page 1 of 6


Lecture 9 Computer Applications 0933201 Chapter 8

Potential problem with integration: a function having a Although the quad and quadl functions are more
singularity in its slope function. The top graph shows the accurate than trapz, they are restricted to computing
function y —x. The bottom graph shows the derivative of y. the integrals of functions and cannot be used when the
The slope has a singularity at x 0. Figure 8.2–2 integrand is specified by a set of points. For such
cases, use the trapz function.
The plots of
Using the trapz function. Compute the integral
y=—x , S
and
³ sin x dx
dy/dx = 0.5/—x 0
First use 10 panels with equal widths of ʌ/10.
Some Slope of The script file is
the integrands x = linspace(0,pi,10);
becomes infinite y = sin(x);
values.The slope trapz(x,y)
function has a
singularity. The answer is 1.9797, which gives a relative error of
100(2 - 1.9797)/2) = 1%.
Z.R.K Z.R.K More? See pages 471-474.
8-7 More? See pages 468, 475, 476. 8-8

Numerical differentiation: Illustration of three


methods for estimating the derivative dy/dx. Figure 8.3–1 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].

Z.R.K Z.R.K
8-9 8-10

Numerical differentiation functions. Table 8.3–1


MATLAB provides the polyder function
Command Description to use for computing derivative polynomials.
d = diff(x) Returns a vector d containing the differences
between adjacent elements in the vector x. Let p1 = 5x + 2 and p2 = 10x2 + 4x -3.
Returns a vector b containing the coefficients
The results can be obtained as follows:
b = polyder(p)
of the derivative of the polynomial >> p1 = [5, 2]; p2 = [10, 4, -3];
represented by the vector p.
>> dp1 = polyder(p1)
b = polyder(p1,p2)Returns a vector b containing the coefficients >> dp2 = polyder(p2)
of the polynomial that is the derivative of the
>> prod = polyder(p1, p2)
product of the polynomials represented by
p1 and p2. >> [num, den] = polyder(p2, p1)
[num, den] = Returns the vectors num and den containing The results are:
polyder(p2,p1) the coefficients of the numerator and dp1 = [5], dp2 = [20, 4],
denominator polynomials of the derivative of
prod = [150, 80, -7], num = [50, 40, 23],
the quotient p2p1, where p1 and p2 are
polynomials.
Z.R.K
and den = [25, 20,Z.R.K4].
8-11 8-12

Z.R.K 2008 Page 2 of 6


Lecture 9 Computer Applications 0933201 Chapter 8

Differential equations: Free and total step response of the Numerical Methods for Differential Equations
equation Wdy/dt+y=f(t), where W=0.1, f(t)=10 & y(0)=2. Figure 8.4–1 Let dy/dt = –10y, y(0) = 2. and 0 d t d 0.5.
The true solution is y(t) = 2 e-10t .
1- The free
response The following script file solves and plots the
solution is: solution by using Euler method.
y(t)=y(0)e-t/W r = -10; delta = 0.02; y(1) = 2; % ?!
k=0;
2- The forced
response for time = [delta:delta:0.5] % ?!
solution is: k = k + 1;
y(t) = y(k+1) = y(k) + r*y(k)*delta;
end
M (1 - e-t/W )
t = [0:delta:0.5]; % for true solution.
The total y_t = 2*exp(-10*t); % true solution.
solution are
plot(t,y,’o’,t,y_t),xlabel(‘t’), ...
1 + 2.
ylabel(‘y’)
Z.R.K Z.R.K
8-13 More? See pages 483-485. 8-15

Euler method solution for the free response of Let dy/dt = –10y, y(0) = 2. and 0 d t d 0.5.
dy/dt = –10y, y(0) = 2. Figure 8.5–1 The true solution is y(t) = 2 e-10t .
The following script file solves and plots the
solution by using Modified Euler method.
r = -10; delta = 0.02; y(1) = 2; k=0;
for time = [delta:delta:0.5] % ?!
k = k + 1;
x(k+1) = y(k) + r*y(k)*delta;
y(k+1) = y(k) + r*y(k)*delta/2 ...
+ r*x(k+1);
end
t = [0:delta:0.5]; % for true solution.
y_t = 2*exp(-10*t); % true solution.
plot(t,y,’o’,t,y_t),xlabel(‘t’), ...
ylabel(y’)
Z.R.K Z.R.K
8-15 8-16 More? See pages 490-492.

Modified Euler solution of dy/dt = –10y, y(0) = 2. Figure 8.5–3


The ode solvers.
When used to solve the first order Ordinary
differential equation dy/dt = f (t, y), the basic
syntax is (using ode23 or ode45 as the
example):
[t,y] = ode23(’ydot’, [t_span], y0)
where ydot is the name of the function file
whose inputs must be t and y and whose
output must be a column vector representing
dy/dt; that is, f (t, y). The number of rows in this
column vector must equal the order of the
equation.
8-17 Z.R.K
8-18 Z.R.K More? See pages 496-499.

Z.R.K 2008 Page 3 of 6


Lecture 9 Computer Applications 0933201 Chapter 8

Application of an ode solver to find the response of an


RC circuit . Figure 8.5–5 The function is called as follows, and the
solution plotted along with the analytical
The circuit model for
solution y_true.
zero input voltage v is
RC dy/dt + y = v(t) >> [t, y] = ode45(’rccirc’, [0, 0.5], 2);
RC=0.1s, v(t)=0 y(0)=2. >> y_true = 2*exp(-10*t); % t gen. in ode45
First solve this for
>> plot(t,y,’ro’,t,y_true), xlabel...
dy/dt:
(’Time(s)’),ylabel(’Capacitor Voltage’)
dy/dt = -10y
Next define the Note: that we need not generate the array t to
following function file.
Note: that the order of the input
evaluate y_true, because t is generated by
arguments must be t and y. The func. name rccirc.m. the ode45 function.
function ydot = rccirc(t,y) The plot is shown on the next slide.
ydot = -10*y;
Z.R.K Z.R.K
8-19 8-20

Free response of an RC circuit. Figure 8.5–6 An oscillating solution: Numerical solutions of the equation
dy/dt = sin t, y (0) = 0. Using ode45 and ode23. Figure 8.5–7

8-21 Z.R.K More? See pages 498-500. 8-22 Z.R.K


More? See page 501.

Plots of the applied voltage and the capacitor voltage when Plots of the applied voltage and the capacitor voltage when the
the applied voltage is v (t) = 10e–t / 0.3 sin(pt). Figure 8.5–8 applied voltage is u (t) = 10e –t / 0.05 sin(2St /0.03). Figure 8.5–9

Z.R.K Z.R.K
8-23 8-24

Z.R.K 2008 Page 4 of 6


Lecture 9 Computer Applications 0933201 Chapter 8

Plots of the applied voltage and the capacitor voltage when the
applied voltage is v (t) = 10e –t / 0.3 sin(2St /0.03). Figure 8.5–10 Extension to Higher-Order Equations
To use the ODE solvers to solve an equation higher
than order 1, you must first write the equation as a
set of first-order equations.
For example,
5d2y/dt2 + 7dy/dt + 4y = f (t)
Set: x1 = y, and x2 = dy/dt
dx1/dt = x2
1 4 7
dx2/dt = f (t) - x1 - x2
5 5 5
This form is sometimes called the Cauchy form or
the state-variable form.

Solve: 5d2y/dt2 + 7dy/dt + 4y = sin t


Z.R.K Z.R.K
8-25 More? See pages 502-505. 8-26

Each row in the vector x corresponds to a time returned


Suppose that f (t) = sin t. Then the required file is in the column vector t. If you type plot(t,x), you will
function xdot = example1(t,x) obtain a plot of both x1 and x2 versus t.
% Computes derivatives of two equations Note that x is a matrix with two columns; the first
xdot(1) = x(2); column contains the values of x1 at the various times
xdot(2) = (1/5)*(sin(t)-4*x(1)-7*x(2)); generated by the solver. The second column contains
xdot = [xdot(1); xdot(2)]; the values of x2.
Note that: Thus to plot only x1, type plot(t,x(:,1)).
xdot(1) represents dx1/dt,
xdot(2) represents dx2/dt, An armature-controlled DC motor. Figure 8.6–5
x(1) represents x1, and x(2) represents x2.
Suppose we want to solve (8.6–1) for 0 d t d 6 with the
initial conditions x1(0) 3, x2(0) 9. Then the initial
condition for the vector x is [3, 9]. To use ode45,
you type
[t, x] = ode45(’example1’, [0, 6], [3, 9]);
8-27 Z.R.K More? See pages 506-507. 8-28 Z.R.K

From KVL and Newton’s law: Voltage input and resulting velocity response of a DC
v(t) = Ri + Ldi/dt + Kew, Â di/dt = - (R/L)i - (Ke/L)w - v(t)/L motor. Figure 8.6–6
T = KT i = Idw/dt + cw, Â dw/dt = (KT/I)i – (c/I)w .
Where: L, R & I are the motor’s inductance, resistance and inertia;
KT & Ke are the torque constant & back emf constant. The above
equations in matrix form is:
di/dt dx1/dt -R/L -Ke/L x1 1/L
= = + v(t)
dw/dt dx2/dt KT/I -c/I x2 0
For example, R=0.6:, L=0002H, KT=0.04N.m/A, Ke=0.04V.s/rad,
c=0 and I=6×10-5kg.m2 and the applied voltage is:
100t 0 d t d 0.1
10 0.1 d t d 0.4
v(t) =
-100(t-0.4)+10 0.4 < t d 0.5
0 t > 0.5
Z.R.K Z.R.K
8-29 8-30 More? See pages 515-518.

Z.R.K 2008 Page 5 of 6


Lecture 9 Computer Applications 0933201 Chapter 8

LTI object functions. Table 8.7–1


Basic syntax of the LTI ODE solvers. Table 8.7–2

Command Description Command Description


sys = ss(A, B, C, D)Creates an LTI object in state-space form, where impulse(sys) Computes and plots the unit-impulse
the matrices A, B, C, and D correspond to those response of the LTI object sys.
in the model dx/dt Ax Bu, y Cx Du.
initial(sys,x0) Computes and plots the free
[A, B, C, D] = Extracts the matrices A, B, C, and D
response of the LTI object sys given
ssdata(sys) corresponding to those in the model
dx/dt Ax Bu, y Cx Du. in state-model form, for the initial
conditions specified in the vector x0.
sys = Creates an LTI object in transfer-function form,
where the vector right is the vector of lsim(sys,u,t) Computes and plots the response of
tf(right,left)
coefficients of the right-hand side of the the LTI object sys to the input
equation, arranged in descending derivative specified by the vector u, at the times
order, and left is the vector of coefficients of
the left-hand side of the equation, also arranged
specified by the vector t.
in descending derivative order. step(sys) Computes and plots the unit-step
[right, left] = Extracts the coefficients on the right- and left- response of the LTI object sys.
tfdata(sys) hand sides of the reduced-form model.
8-31 Z.R.K
More? See pages 518-521. 8-32 Z.R.K More? See pages 521-526.

Free response of the model given by (8.7–2) through Step response of the model given by (8.7–2) through (8.7–5)
(8.7–5) for x1(0) = 5 and x2(0) = -2. Figure 8.7–1 and the model (8.7–10), for zero initial conditions. Figure 8.7–2
>>A=[0,1;-2.5,-1.5];
>>B=[0;0.5];
>>C=[1,0]; 5x’’ + 7x’ + 5x = 5f’ + f
>>D=0;
>> >>s=tf([5,1],...
>>sy=ss(A,B,C,D); [5,7,5]);
>> >>
>>initial(sy,[5,-2]) >>
>>step(sy,s,’--’)

Z.R.K Z.R.K
8-33 8-34

Square-wave response of the model


x’’ + 2x’ + 4x = 4f. Figure 8.7–3
End of Chapter 8
x’’ +2x’ +4x = 4f
>>s1=tf(4,[1,2,4]);
>>
>>[u,t]=gensig(...
‘square’,5,10,0.01);
>>
>>[y,t]=lsim(s1,u,t); Problems Page 532 - 539 !
>>
>>plot(t,y,t,u) Solve: 4, 14, 19, 23, 31, 38.
www.ju.edu.jo\zkhatib
8-35 Z.R.K Z.R.J.K
Z.R.K

Z.R.K 2008 Page 6 of 6

You might also like