Got an error using ode45 - FUNC1 must return a column vector

1 view (last 30 days)
Script looks like this :
clear all
clc
x0=[7 10 13];
tspan=[0,10];
[t,x]=ode45(@func1,tspan,x0);
subplot(3,1,1);
hold on;
plot(t,x(:,1),'k');
subplot(3,1,2);
hold on;
plot(t,x(:,2),'k');
subplot(3,1,3);
hold on;
plot(t,x(:,3),'k');
and Function :
function xd = func1( t,x )
xd(1)=2*x(1)+3*x(2)-4*x(3);
xd(2)=-x(1)+2*x(2);
xd(3)=(-2)*x(1)+x(2)+2;
end

Accepted Answer

Geoff Hayes
Geoff Hayes on 11 Jan 2015
Serbianu - the error message is telling you that the function func1 must return a column vector. From your code, the vector xd is a row vector. So just change this to a column vector as
function xd = func1( t,x )
xd = zeros(3,1);
xd(1)=2*x(1)+3*x(2)-4*x(3);
xd(2)=-x(1)+2*x(2);
xd(3)=(-2)*x(1)+x(2)+2;
end
In the above, we've added a line that just initialized xd as a column vector with three elements. Try making the change and see what happens!
(An alternative solution would be just to transpose the vector xd.)

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!