Академический Документы
Профессиональный Документы
Культура Документы
Example 3.16 Sovling 2nd order ODE by MATLAB using the modified Euler’s method
y + 2 y + 7 y = 7 u
where y is output and u is input. It is assumed that all initial conditions are zero. Find the
system response when a unit step input is applied.
SOLUTION
% Example0316Sim.m
% This program is to illustrate how to solve a second-order ODE
% by MATLAB (using the modified Euler's method).
%
clear
% initial conditions:
x = [0 0]';
h = 0.01;
N = 10;
u = 1; % unit step function
index = 0;
for ii = 0:h:N
index = index + 1;
[k1,y] = Example0316(x,u,ii);
[k2,y] = Example0316(x+0.5*h*k1,u,ii);
x = x + h*k2;
end
% Plots:
% Response:
subplot(211);
plot(data(:,1),data(:,2));
xlabel('Time (seconds)');
ylabel('Response y');
grid
% Response rate:
subplot(212);
plot(data(:,1),data(:,3));
xlabel('Time (seconds)');
ylabel('Response rate y-dot');
grid
% Example0316.m
%
% State vector x = [x1 x2]'
% u = input
% t = time
% Physical constants:
a = 1.0;
b = 2.0;
c = 7.0;
d = 7.0;
Running this simulation program, we have the following results as shown in Figure 3.17.
Response y
1
0.5
0
0 1 2 3 4 5 6 7 8 9 10
Time (seconds)
2
Response rate y-dot
-1
0 1 2 3 4 5 6 7 8 9 10
Time (seconds)
Figure 17 Simulated results for Example 3.16 using the modified Euler’s method
MATLAB provides a number of built-in functions (called sovlvers) that can be used to solve
ODEs. They are ode23, ode45, ode113, ode15s, ode23s, ode23t, ode23tb. Readers can find
detailed descriptions of these built-in functions by typing the command ‘help function_name”
in the MATLAB Command windows.
Example 3.17 Solving 2nd order ODE by using a solver (ode45) in MATLAB
Let’s reconsider the equation in Example 3.16. A dynamic system is represented by the
following differential equation:
y + 2 y + 7 y = 7 u
where y is output and u is input. It is assumed that all initial conditions are zero. Find the
system response when a unit step input is applied.
SOLUTION
% Example0317.m
% Second Order Differential Equation:
% y_dot_dot + 2*y_dot + 7*y = 7*u
u = 1; % input
y_dot = [y(2);
-2*y(2)-7*y(1) + 7*u];
% End of Example0317.m
% Example0317Sim.m:
clear
%plot(T,Y(:,1),T,Y(:,2));grid
%xlabel('Time (seconds)');
%ylabel('Response and rate')
% Plots:
% Response:
subplot(211);
plot(T,Y(:,1));
xlabel('Time (seconds)');
ylabel('Response y');
grid
% Response rate:
subplot(212);
plot(T,Y(:,2));
xlabel('Time (seconds)');
ylabel('Response rate y-dot');
grid
Running this program, we obtain the following results in Figure 3.18. Readers may compare
the results with the above results solved by uing the modified Euler’s method (see Figure
3.17).
Response y
1
0.5
0
0 1 2 3 4 5 6 7 8 9 10
Time (seconds)
2
Response rate y-dot
-1
0 1 2 3 4 5 6 7 8 9 10
Time (seconds)
Let’s reconsider the differential equation in Example 16. We will use Simulink to solve this
equation.
SOLUTION
The Simulink model can be created as shown in Figure 3.19. In order to display computed
results, Simulink library provides a number of blocks (in Sinks): Scope block, Display block,
To Workspace block, etc.
After running the Simulink model, user can save computed data in the Workspace using the
‘save’ command in the MATLAB Command Window. The saved data can be retrieved by
using the ‘load’ command.
Before running the Simulink model, a solver can be selected as shown in Figure 3.20
(Simulation menu – Configuration Parameters…).
The initial conditions for the differential equation can be set by double-clicking the Integrator
block.
1.5
Response y
0.5
0
0 1 2 3 4 5 6 7 8 9 10
Time (seconds)
2
Response rate y-dot
-1
0 1 2 3 4 5 6 7 8 9 10
Time (seconds)