MATLAB: Ode45 giving “index exceeds matrix dimension” error

differentialfunctionMATLABode45transfer functionunit step

I am trying to get an ode45 code to plot the differential equation.
My code is:
tspan = [0 100];
y0 = [0,0];
[tout,yout] = ode45(@prob1,ts,y0);
plot(t,y(:,3))
grid on
xlabel('Time')
ylabel('x(t)')
and
function ydot = prob1(t,y)
ydot(1) = [-6*y(1) - 2*y(2) - 5*y(3) + .5*step(t)];
ydot(2) = y(1);
ydot(3) = y(2);
ydot = [ydot(1);ydot(2);ydot(3)];
end
but it is giving me an error of when I run the function of "undefined function or variable t" and when I run the ode45 script is gives:
Index exceeds matrix dimensions.
Error in prob1 (line 3)
ydot(1) = [-6*y(1) - 2*y(2) - 5*y(3) + .5*step(t)];
Error in odearguments (line 87)
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);
Error in prob1_file (line 4)
[tout,yout] = ode45(@prob1,ts,y0);
Any advice would be greatly appreciated.

Best Answer

The second parameter of an ode function will have a number of elements equal to the number of elements of the boundary conditions that you pass in. Your ode function tries to access 3 elements, so the boundary conditions you pass in needs to have at least 3 elements, but the boundary conditions you told ode45 to use is y0 which is length 2.
Related Question