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

1 次查看(过去 30 天)
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

采纳的回答

Geoff Hayes
Geoff Hayes 2015-1-11
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.)

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by