MATLAB: Error using ode45 – Column vector

column vectorerrorode45

Hi, I have this error:
Error using odearguments (line 90)
@(T,P)('FUN1') must return a column vector.
But the function returns a column vector. Can anyone help me find my mistake? I have read the other questions people have asked on this and they were all entering row vectors. Here is my code:
The function:
function f = fun1(t,P)
f = zeros(3,1);
f(1) = -1.92*P(1);
f(2) = 1.92*P(1)-1.2*P(2);
f(3) = 1.2*P(2);
end
The code which uses the function:
dPdt = @(t,P)('fun1');
P0 = [1,0,0];
[t,P] = ode45(dPdt, [0 5], P0);
plot(t, P)

Best Answer

function main
P0 = [1,0,0];
[t,P] = ode45(@fun1, [0 5], P0);
plot(t, P)
end
function f = fun1(t,P)
f = zeros(3,1);
f(1) = -1.92*P(1);
f(2) = 1.92*P(1)-1.2*P(2);
f(3) = 1.2*P(2);
end
The problem is the string 'fun1' in: dPdt = @(t,P)('fun1'). You do not want to get the string (row vector of type char) 'fun1', but the function handle @fun1.