How do I fix this plot failure?
8 views (last 30 days)
Show older comments
The following code is trying to demonstrate a canon ball in motion using ode45. The code works fine right until the end where an error message occurs. I don't know how to fix this. Can anyone help me? (The comments are in danish, and are not of importance in regards to the problem)
function kanon_kugle
% Vinkel i forhold til horisontal, som kuglen skydes fra.
theta = 40;
% Start-betingelser
x0 = 0; %
u0 = 50*cosd(theta);
y0 = 0;
v0 = 50*sind(theta);
SB = [x0,u0,y0,v0];
% SB = Start-Betingelser
% u0 er start-hastigheden i x-retningen
% v0 er start-hastigheden i y-retningen
% Time-Span
t0 = 0; tf = 30;
tspan = (t0:0.08:tf);
% Numerisk integration
options = odeset("Events", @events);
[~, state_values] = ode45(@bane, tspan, SB, options);
% Udtrække data
x = state_values(:,1);
y = state_values(:,3);
% Plot
axis([0 500 0 300])
hold on
n=1;
while n < 1000
plot(x(n),y(n),"b.",'MarkerSize',30)
xlabel("x forskydning (m)"), ylabel("y forskydning (m)")
title("Scorched Earth")
pause(0.0001)
n = n + 1;
clf
xlim([0,500])
ylim([0,500])
hold on
end
function sdot = bane(~,s)
g = 9.8; % Tyngdeacceleration (m/s^2)
sdot = [s(2); 0; s(4); -g];
end
function [check, isterminal, direction] = events(~,s)
direction = []; % Default indstilling for "direction"
isterminal = 1; % Afslut integrationen når event opstår
check = double( s(3) <= 0 ); % event sker når y < 0 (kanonkugle rammer jorden)
end
end
0 Comments
Accepted Answer
VBBV
on 8 Nov 2020
Edited: Walter Roberson
on 8 Nov 2020
plot(x(1:n,1),y(1:n,3),'b')
%clf
Comment the clf line and use vector to plot
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!