Matlab 8 Slide
Matlab 8 Slide
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.
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
Z.R.K Z.R.K
8-9 8-10
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.
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
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
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.
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.
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