Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
Serbianu
Serbianu am 10 Jan. 2015
Kommentiert: Serbianu am 11 Jan. 2015
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

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 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.)
  1 Kommentar
Serbianu
Serbianu am 11 Jan. 2015
Thank you a lot, it's working now. I didn't expect to get answered so fast :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by