Making my simulation stop when z=0

2 次查看(过去 30 天)
I need a way to tell my plot to stop when z=0. Right now it keeps going into the negative z direction until it hits my time that I have it set to. Here is my code
tspan = [0, 30];
IC = [0, 91.8559, 0, 91.8559, 0, 86.6025]
[time, state_values] = ode45(@project12,tspan,IC);
x = state_values(:,1);
xdot = state_values(:,2);
y = state_values(:,3);
ydot=state_values(:,4);
z = state_values(:,5);
zdot=state_values(:,6);
%plot x(t)
subplot(3,1,1)
plot(time,x)
%plot y(t)
subplot(3,1,2)
plot(time,y)
%plot z(t)
subplot(3,1,3)
plot(time,z)
function sdot = project12(t,s)
% s eqauls x, xdot, y, ydot, z, zdot
sdot(1)=s(2)
sdot(2)=0
sdot(3)=s(4)
sdot(4)=0
sdot(5)=s(6)
sdot(6)=-9.81
sdot=sdot'
end

回答(1 个)

Walter Roberson
Walter Roberson 2019-10-28
You would use an event function.
I recommend looking at the ballode example, as it does this kind of processing.
  2 个评论
Stephen Mixon
Stephen Mixon 2019-10-28
I looked at the mathworks page for this. How would I implement this in my code
Walter Roberson
Walter Roberson 2019-10-29
Copy ballode to a new file . Change the function name on the first line. Change
for i = 1:10
to
for i = 1:1
Change function f to your actual function.
That should pretty much do it.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by