Matlab code running forever when trying to run this code?

조회 수: 6 (최근 30일)
Preets12
Preets12 2018년 4월 8일
편집: Walter Roberson 2018년 4월 8일
I created a code to plot the distance (s) in the horizontal direction achieved by a cannon ball vs. firing angle (from 0deg to 90deg) using an initial velocity v0 = 100 m/s. I want to produce four plots in one figure (with different colours/line markers) for each of the different values of k = 0, k = 0.0005, k = 0.001 and k = 0.0015. (k is drag coefficient) But when i run my code it tends to run forever so i have to pause it. Why is this an issue?
Here's my code:
v0 = 100;
%set dt
dt = 0.1;
%set g
g = 9.8;
%User sets an array of his/her chosen values for k
for k = [0,0.0005,0.001,0.0015]
for firing_angle = 0:90
%User 'calls' function to display the graph of the trajectory of the
%projectile motion of the cannon ball
[t,x,y] = cannon_trajectory(dt,v0,firing_angle,k);
s = (2*(v0^2)*sin(2*firing_angle))/2*g;
plot(firing_angle,s);
xlabel('x');
ylabel('y');
title('Horizontal distance VS Firing angle');
hold on;
legend('k=0','k=0.0005','k=0.001','k=0.0015');
end
end

답변 (1개)

Walter Roberson
Walter Roberson 2018년 4월 8일
편집: Walter Roberson 2018년 4월 8일
We don't know. You have not shown the code for cannon_trajectory.
You are calling plot() once for every firing_angle. Your equation for s ignores the t, x, y results from cannon_trajectory, so we can deduce that s is going to be a scalar. You are trying to plot() a single point each time, with no marker. Nothing is going to show up on the plot if you plot a single point with no marker.
Question: is your s definitely something / 2 all multiplied by g, or should it be something / (2 * g) ?
With you plotting 91 (0 to 90 is 91) different points for each value of k, you are going to be plotting 364 points (all invisible). But you are only legend() four strings. The legend is going to get pretty crowded with 'data5' through 'data364' being automatically generated by legend()
Instead of plotting once for each point, you should be recording the angle and the distance into a vector, and plotting the entire vector after the for firing_angle loop. That would leave you with a total of four plots.
The legend() call should go after all plotting is done.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by