matlabstate-spaceode45

Error while solving State space equations using ode45


My code should output me a column vector of X at the end of the program.I'm getting a lot of errors. Please help!

    clc;
    clear;
    t0=0;
    tend=.001;
    T=[t0 .00005];
    T1=[.00005 tend];
    temp=1;
    X(:,1) = [0;0;0;0];

    for k=1:2   
    for i= temp:50*(k)
            [T,X]=ode45(@(T,X)sys(T,X,A1,B1),T,X(:,1));
        else
            [T1,X1]=ode45(@(T1,X1)sys1(T1,X1,A0,B0),T1,X(:,end));
        end
    end
    temp=50;
    end 

    function Xdot = sys(T,X,A1,B1,U)
    Xdot= A1*X + B1*U;
    end
    function Xdot = sys1(T1,X,A0,B0,U)
    Xdot= A0*X + B0*U;
    end

The errors are as follows:

Not enough input arguments. 
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.
 Error in ode45 (line 115)
    odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, 
    varargin);

Solution

  • It looks like you are not setting the argument U to the functions sys and sys1.

    I have highlighted the corresponding places below with <U missing>:

    if sw(i)==0 && X(2,i)> vdon || sw(i)==1 && X(1,i)>0
        sw(i+1)=1;
        [T,X] = ode45( @(T,X) sys(T,X,A1,B1, <U missing> ), T, X(:,1) );
    else
        sw(i+1)=0;
        [T1,X1] = ode45( @(T1,X1) sys1(T1,X1,A0,B0, <U missing> ), T1, X(:,end) );
    end