ode45 help!!
1 view (last 30 days)
Show older comments
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]);
0 Comments
Answers (1)
madhan ravi
on 22 Sep 2020
Just make the following changes in your code:
function dx = project
xdot = [xdot1; xdot2];
0 Comments
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!