Keep Getting This Error : @(T,X)SECO​ND_ORDER(T​,X,A0,A1) must return a column vector.

Hello all! I'm trying to solve the second order equation y'' + a1*y' + a0*y = 0 using ode45 and I've gotten stuck and keep getting the same error. My current code:
%%Behavior of second order ODEs
% y'' + a1*y' + a0*y = 0
% x' = Ax
N=1000;
T=10;
t = linspace(0,T,N); % initialize the time vector
a0=0;
a1=0;
x0=2; %y
x1=0; %dy
x=[x0;x1];
dy = second_order(t,x,a0,a1);
%ODE
[t,x] = ode45(@(t,x)second_order(t,x,a0,a1),t,x0);
plot(t,x,'k-','LineWidth',2);
My Function:
function [ dx ] = second_order(t,x,a0,a1 )
A=[0,1;-a0,-a1];
dx= A*x;
end

 Accepted Answer

This line declares initial state that you pass to ode45 to be a scalar:
x0=2; %y
So when you call ode45 with x0 it assumes the state is a scalar. But you have this line also:
x=[x0;x1];
This 2-element vector is what you need to pass to ode45 as the initial state:
[t,x] = ode45(@(t,x)second_order(t,x,a0,a1),t,x);

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!