Bai2-4-6

Embed Size (px)

Citation preview

  • 8/8/2019 Bai2-4-6

    1/7

    Copyright 2006 Hi u

    2.4.6 Solving ODEs with MATLAB/Simulink Software

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

    3.5.1 Programming in MATLAB

    Example 3.16 Sovling 2nd order ODE by MATLAB using the modified Eulers method

    A dynamic system is represented by the following differential equation:

    u7y7y2y =

    where y is output and u is input. It is assumed that all initial conditions are zero. Find thesystem response when a unit step input is applied.

    SOLUTION

    Using the modified Eulers 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 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;

  • 8/8/2019 Bai2-4-6

    2/7

    Copyright 2006 Hi 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.

  • 8/8/2019 Bai2-4-6

    3/7

    Copyright 2006 Hi u

    0 1 2 3 4 5 6 7 8 9 100

    0.5

    1

    1.5

    Time (seconds)

    R e s p o n s e y

    0 1 2 3 4 5 6 7 8 9 10-1

    0

    1

    2

    Time (seconds)

    R e s p o n s e r a

    t e y - d o

    t

    Figure 17 Simulated results for Example 3.16 using the modified Eulers method

    3.5.2 Using Solvers in MATLAB

    MATLAB provides a number of built-in functions (called sovlvers) that can be used to solveODEs. They are ode23, ode45, ode113, ode15s, ode23s, ode23t, ode23tb. Readers can finddetailed descriptions of these built-in functions by typing the command help function_namein the MATLAB Command windows.

    Example 3.17 Solving 2nd order ODE by using a solver (ode45) in MATLAB

    Lets reconsider the equation in Example 3.16. A dynamic system is represented by thefollowing differential equation:

    u7y7y2y =

    where y is output and u is input. It is assumed that all initial conditions are zero. Find thesystem 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

  • 8/8/2019 Bai2-4-6

    4/7

    Copyright 2006 Hi 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 comparethe results with the above results solved by uing the modified Eulers method (see Figure3.17).

  • 8/8/2019 Bai2-4-6

    5/7

    Copyright 2006 Hi u

    0 1 2 3 4 5 6 7 8 9 100

    0.5

    1

    1.5

    Time (seconds)

    R e s p o n s e y

    0 1 2 3 4 5 6 7 8 9 10-1

    0

    1

    2

    Time (seconds)

    R e s p o n s e r a

    t e y - d o

    t

    Figure 3.18 Simulated results using solver ode45

    3.5.3 Using Simulink

    Example 3.18 Solving ODEs using Simulink

    Lets reconsider the differential equation in Example 16. We will use Simulink to solve thisequation.

    SOLUTION

    The Simulink model can be created as shown in Figure 3.19. In order to display computedresults, 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 thesave command in the MATLAB Command Window. The saved data can be retrieved byusing 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.

  • 8/8/2019 Bai2-4-6

    6/7

    Copyright 2006 Hi 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.

  • 8/8/2019 Bai2-4-6

    7/7

    Copyright 2006 Hi u

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

    0 1 2 3 4 5 6 7 8 9 100

    0.5

    1

    1.5

    Time (seconds)

    R e s p o n s e y

    0 1 2 3 4 5 6 7 8 9 10-1

    0

    1

    2

    Time (seconds)

    R e s p o n s e r a

    t e y - d o

    t

    Figure 3.22 Simulated results using Simulink and ode45 solver