Making my simulation stop when z=0

1 view (last 30 days)
Stephen Mixon
Stephen Mixon on 28 Oct 2019
Commented: Walter Roberson on 29 Oct 2019
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

Answers (1)

Walter Roberson
Walter Roberson on 28 Oct 2019
You would use an event function.
I recommend looking at the ballode example, as it does this kind of processing.
  2 Comments
Stephen Mixon
Stephen Mixon on 28 Oct 2019
I looked at the mathworks page for this. How would I implement this in my code
Walter Roberson
Walter Roberson on 29 Oct 2019
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!