How to use ODE 45 to simulate a model with two state equation?
4 views (last 30 days)
Show older comments
Hi, I have started to use MATLAB from 2 months ago, and I am having a trouble of simulating a box model with parameters through ODE 45.
I cannot get the right plots of graph, even though I tried so many times to debug my code.
My following is the code that I set up, however, I do not know what is wrong from my codes
------- Here is the variables--
function dx = wheelchaircorrectversion (t,x)
global m g d F h Ix w
m=10;
g=-9.81;
F=5;
d=10;
h=20;
Ix=30;
w=20;
if t==0.01
F=5;
else t>=0.02
F=0;
end
z=(sqrt((.5*w).^2+(.5*h).^2)).*(sin(atan((h/2)/w/2)));
d=(sqrt((.5*w).^2+(.5*h).^2)).*(cos(atan((h/2)/w/2)));
dx=zeros(2,1);
dx(1)=x(2);
dx(2)=(-(m*g*d)+(F+z))/Ix;
end
---- and this is the main function
clear all
clc
global m g d F h Ix w
m=10;
g=-9.81;
F=5;
d=10;
h=20;
Ix=30;
w=20;
i=1;
x1_ini = [0 0]';
dt = 0.01;
duration = 0;
for t = 0.0 : dt : duration
i = i+1;
t0 = t;
tf = (t+dt)*(1+eps);
tspan = [t0 tf];
[t1,x1] = ode45('wheelchaircorrectversion',tspan,x1_ini);
n1 = length(x1);
tmp1 = x1(n1,:);
x1_ini = tmp1; % update initial conditions
traj_p0(i+1,:) = x1_ini;
end
subplot(221)
plot(traj_p0(:,1),traj_p0(:,2))
xlabel( 'disp. in x direction(m)' )
ylabel( 'disp. in y direction(m)' )
----
As you can see above, the codes are representing a simple box model, pushed with a force of 5 Newtons to simulate the roll angle of the object: x1 being phi, and x2 being phi dot.
The parameters are randomly put, and the time intervals are set so that I could get the control over time to put force for only a 0.0001 second, not continuously.
I am a beginner in MATLAB so I have only little knowledge of coding.. Could anyone see the problem I am having?
Thank you for your time.
0 Comments
Answers (1)
Jan
on 13 Jan 2013
Edited: Jan
on 13 Jan 2013
ODE45, as the other ODE integrators of Matlab, requires a continuously differentiable function. Otherwise the stepsize control can explode. You find several exhaustive explanation in this forum for this topic, but sometimes I'm afraid I'm the only one who cares about this detail.
This looks strange:
if t==0.01
F=5;
else t>=0.02
F=0;
end
Do you mean "elseif"?
Using global variables to define parameters is suboptimal. See benefit of using anonymous functions .
5 Comments
Jan
on 14 Jan 2013
No, I cannot understand it better. I cannot recognize, where try to apply a force for 0.0001 seconds. Creating a loop, which runs for 1 iteration also works, but is meaningless. I cannot see, how such a loop should support, that the force is not applied continuously. I still do not see any reaction to my argument, that the function to be integrated must be smooth or that anonymous functions have benefits.
You set i=1, then i=i1 inside the loop and write to traj_p0(i+1,:) finally. Then the first 2 rows of traj_p0 are undefined. Is this wanted?
What is "z", "d" and "w" in your formula?
Why is the "wheelchair" a square? Do you consider the rotational energy of the wheel? If not, simulation the center of mass as a sliding point would be much easier and you can even get the results in clodes form without a numerical integration.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!