How do I fix this plot failure?

조회 수: 10 (최근 30일)
Jonas Allaoua
Jonas Allaoua 2020년 11월 7일
편집: Walter Roberson 2020년 11월 8일
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