How do I fix this plot failure?

10 次查看(过去 30 天)
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

采纳的回答

VBBV
VBBV 2020-11-8
编辑:Walter Roberson 2020-11-8
plot(x(1:n,1),y(1:n,3),'b')
%clf
Comment the clf line and use vector to plot

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by