Вы находитесь на странице: 1из 7

2.4.

6 Solving ODEs with MATLAB/Simulink Software


MATLAB/Simulink software provides with very good tools to solve ordinary differential
equations. You can solve ODEs by your own codes (M-files) using the above-mentioned
numerical methods, MATLAB built-in functions (solvers) or Simulink. The simulation
techniques in Simulink was also designed based on the above mentioned numerical methods.
The following examples illustrate how to solve differential equations using
MATLAB/Simulink software in different ways.

3.5.1 Programming in MATLAB

Example 3.16 Sovling 2nd order ODE by MATLAB using the modified Euler’s method

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

Using the modified Euler’s method, sample M-files are as follows:

% Example0316Sim.m
% This program is to illustrate how to solve a second-order ODE
% by MATLAB (using the modified Euler's method).
%

% Made by Hung Nguyen <h.nguyen@amc.edu.au> in January 2006


% Last modified on 19th Jan 2006. This program is for the unit:
% Marine and Offshore Systems Simulation and Diagnostics, BE (MOS).
% Copyright(C) 2006 Hung Nguyen.
%

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;

% Modified Euler's method

[k1,y] = Example0316(x,u,ii);
[k2,y] = Example0316(x+0.5*h*k1,u,ii);
x = x + h*k2;

Copyright © 2006 Hải Âu


% Store computed data in the matrix data:

data(index,1) = ii; % Time (seconds)


data(index,2) = x(1); % Response
data(index,3) = x(2); % Response rate

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

function [x_dot,y] = Example0316(x,u,t)

% Physical constants:

a = 1.0;
b = 2.0;
c = 7.0;
d = 7.0;

% State space model:


A = [0 1;-c/a -b/a];
B = [0 d/a]';
C = [1 0];

x_dot = A*x + B*u;


y = C*x;

% End of function Example0216

Running this simulation program, we have the following results as shown in Figure 3.17.

Copyright © 2006 Hải Âu


1.5

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

3.5.2 Using Solvers in MATLAB

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

Using MATLAB solver (ode45) we have the following sample M-files:

% Example0317.m
% Second Order Differential Equation:
% y_dot_dot + 2*y_dot + 7*y = 7*u

Copyright © 2006 Hải Âu


function y_dot = Example0317(t,y)

u = 1; % input

y_dot = [y(2);
-2*y(2)-7*y(1) + 7*u];

% End of Example0317.m

% Example0317Sim.m:

clear

[T,Y] = ode45('Example0317',[0 10],[0 0]);

%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).

Copyright © 2006 Hải Âu


1.5

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 3.18 Simulated results using solver ode45

3.5.3 Using Simulink

Example 3.18 Solving ODEs using Simulink

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.

Copyright © 2006 Hải Âu


Figure 3.19 Simulink model for Example 17

Figure 3.20 Selection of solver (ode45)


Running this Simulink model, we obtain the following results.

Copyright © 2006 Hải Âu


Figure 3.21 The Scope block can be used to display computed results

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)

Figure 3.22 Simulated results using Simulink and ode45 solver

Copyright © 2006 Hải Âu

Вам также может понравиться