ode45 help!!

3 ビュー (過去 30 日間)
Anna Zito
Anna Zito 2020 年 9 月 22 日
回答済み: madhan ravi 2020 年 9 月 22 日
I am trying to use the ode45 command, but I keep receiving error codes. I have changed so many things but I can not get it to work.
I want to plot the following:
  • the position of the table and its velocity versus time for two or three periods.
  • the velocity versus position.
Here is my current code along with the error messages:
function dx=project1(t,x)
m=20;
M=500;
k=50000;
l=2;
g=9.81;
wn=sqrt(4*k/(m+M));
T=(2*pi)/wn;
x0=-(m*g)/(4*k);
x0dot=-((m)/(m+M))*sqrt(2*g*l);
t=0:0.001:2*T;
xi=x0;
xidot=x0dot;
[t,x]=ode45(@project1, t, [xi xidot]);
figure;
plot(t,x(:,1));
xlabel('time');
ylabel('position');
figure;
plot(t,x(:,2));
xlabel('time');
ylabel('velocity');
function xdot=project1(~,x)
xdot1=x(2);
xdot2=-wn*wn*x(1);
xdot=[xdot1, xdot2];
end
end
----------------------------------------------------------------------------------------------------------------------
Error Message:
Error using odearguments (line 93)
PROJECT1/PROJECT1 must return a column vector.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in project1 (line 23)
[t,x]=ode45(@project1, t, [xi xidot]);

回答 (1 件)

madhan ravi
madhan ravi 2020 年 9 月 22 日
Just make the following changes in your code:
function dx = project
xdot = [xdot1; xdot2];

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by