Search code examples
matlabode

How to solve problems with ode23?


I have several problems:

Array indices must be positive integers or logical values.

Error in task2>diffsys (line 11)
dp = [ -4*y(0)+4*y(1)+1*y(2);

Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.

Error in ode23 (line 114)
    odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

Error in task2 (line 4)
[t, y] = ode23(@diffsys, tr, p);

This is my code:

tr = [0 1]; %tame range
p = [0;0;1]; %vector of initial conditions

[t, y] = ode23(@diffsys, tr, p);
plot(t,y);
grid on;
title('lab');
legend('y0','y1','y2');

function dp = diffsys(t, y)
dp = [ -4*y(0)+4*y(1)+1*y(2);
       2*y(0)-7*y(1)+3*y(2);
       2*y(0)+3*y(1)-4*y(2);
      ];
end

Solution

  • You need to pay attention to the first error message and the error location in the two lines after that. The other messages are the call stack, telling you how the error line was called from the main program.

    The error is that in Matlab, array indices start with 1, not 0, so you need to shift all indices one up.

    In this specific case you could formulate the ODE function without indices using the matrix-vector product as

    function dp = diffsys(t, y)
      dp = [ -4 4 1 ;  2 -7 3 ;  2 3 -4 ] * y;
    end