matlabsimulationdifferential-equationsode45

How can I simulate with a input u with ODE45 - MATLAB


I have two function handles:

A = @(x, y, z) [10*y - 10*x; 28*x - z*x - y; x*y - 8/3*z]
B = @(u) [u; 0; 0]

How can I simulate them that in MATLAB with a control variable u? Control variable u is time varying.


Solution

  • You can use:

    [t,out] = ode45(@(t,vars) myfunction(t,vars,U),tsim,xyz0);
    x = out(1);
    y = out(2);
    z = out(3);
    

    Where U is the value of u, tsim is the simulation time (final time o time vector) and xyz0 is the vector of initial condition of variables x, y and z in the form [x0 y0 z0]. myfunction is defined as:

    function dxyz= myfunction(t,vars,u)
    
        x = vars(1);
        y = vars(2);
        z = vars(3);
    
        % u varying with a condition
        if t>1
            u = newValue;
        end
    
        % u is a function of time u=f(t)
        u = 2*t;
    
        A = [10*y - 10*x; 28*x - z*x - y; x*y - 8/3*z];
        B = [u; 0; 0];
        dxyz = A+B;
    
    end