MATLAB: Trying to plot numerical solutions for systems of DEs

ode45systems of equations

I am having trouble using ode45 to do this is it says that my function is not a column vector but I don't know why it isn't.
f = @(t,x) [-2*x + 4*y; x + 2*y]
tspan = [0 5]
x0 = [0;0]
[t,x] = ode45(f,tspan,x0)
plot(t,x,'-o')
Error using odearguments (line 93)
@(T,X)[-2*X+4*Y;X+2*Y] must return a column vector.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in prac (line 5)
[t,x] = ode45(f,tspan,x0)

Best Answer

The reason is that ‘y’ is not defined in your function.
One option is to code ‘x’ as ‘x(1)’ and ‘y’ as ‘x(2)’:
f = @(t,x) [-2*x(1) + 4*x(2); x(1) + 2*x(2)];
tspan = [0 5];
x0 = [0;0]+eps;
[t,x] = ode45(f,tspan,x0);
figure
plot(t,x,'-o')
That works, and may do what you want.