Plot Question: Why is y(size(t)) used? Is there a better option?

1 visualizzazione (ultimi 30 giorni)
Disclaimer: I am just learning Matlab.
I have the same question as Alex did on the same code. It doesn't look like he got a follow up answer after posting all of the code.
This was an example from the Hahn book. I am not 100% clear on why y(size(t)) is used.
The desired result is to plot the graph for x,y and then add two markers: one that will show max height and the other that will show max distance.
If I were to do the following:
plot (x,y,'k',xmax,y,'o',xMax/2,ymax,'o');
I get a funky figure that has markers all the way up and down what appears to be a horizontal line at xmax
But using the following line from Hahn example...
plot(x,y,'k',xmax,y(size(t)),'o',xmax/2,ymax,'o')
...the resulting figure is what is expected if you only look at everything at or above the y-launch point. However if you show the plot beyond that point it also puts a marker at (xmax, -47) :
Questions
  1. It appears that the y(size(t) causes the markers to be placed at only at the first and last value of y - is that what it's doing?
  2. Is there a better way to approach this?
Additional Problem with this method
I was having a problem with the legend not matching the colors with the marker colors.
legend ('Path', 'Max Distance','Max Height');
It wasn't until I extended t to be beyond the (xmax,y=0) that I noticed the third marker...and then realized why my legend was so messed up. I was creating a legend for three plot items when in actuality there were four.
The way I fixed this is by including a variable that is the launch point...using that variable in the plot instead of the y(size(t))
iY = 40;
plot(x,y,'k',xmax,iY, 'o', xmax/2, ymax, 'o')
Question
3. If there is not a better way to do the plot, how do I prevent the additional marker to be placed on the figure in the event
that I want to show the trajectory below the horizontal?
Thank you!
mm
Hahn example code:
% in a gravitational field with constant g.
%
% Written by D. T. Valentine ........ September 2006
% Revised by D. T. Valentine ........ November 2008
% An eight-step structure plan applied in MATLAB:
%
% 1. Definition of the input variables.
%
g = 9.81; % Gravity in m/s/s.
vo = input('What is the launch speed in m/s? ');
tho = input('What is the launch angle in degrees? ');
tho = pi*tho/180; % Conversion of degrees to radians.
%
% 2. Calculate the range and duration of the flight.
%
txmax = (2*vo/g) * sin(tho);
xmax = txmax * vo * cos(tho);
%
% 3. Calculate the sequence of time steps to compute trajectory.
%
dt = txmax/100;
t = 0:dt:txmax;
% 4. Compute the trajectory.
%
x = (vo * cos(tho)) .* t;
y = (vo * sin(tho)) .* t -(g/2) .* t.^2;
%
% 5. Compute the speed and angular direction of the projectile.
% Note that vx = dx/dt, vy = dy/dt.
%
vx = vo * cos(tho);
vy = vo * sin(tho) -g .* t;
v = sqrt(vx.*vx + vy.*vy);
th = (180/pi) .* atan2(vy,vx);
%
% 6. Compute the time, horizontal distance at maximum altitude.
%
tymax = (vo/g) * sin(tho);
xymax = xmax/2;
ymax = (vo/2) * tymax * sin(tho);
%
% 7. Display ouput.
%
disp([' Range in m = ',num2str(xmax), ...
'' Duration in s = , num2str(txmax)])
disp('')
disp([' Maximum altitude in m = ',num2str(ymax), ...
' Arrival in s = ', num2str(tymax)])
plot(x,y,'k',xmax,y(size(t)), 'o', xmax/2, ymax, 'o')

Risposta accettata

Walter Roberson
Walter Roberson il 27 Apr 2020
yes, you are right about what y(size(t)) is doing. More clear would be y([1 end])
  3 Commenti
Walter Roberson
Walter Roberson il 28 Apr 2020
I am not clear as to where you do or do not want the marker?
Are you wanting the marker at the peak, and also the place that it first goes below 0 ?
Walter Roberson
Walter Roberson il 28 Apr 2020
If you are wanting a marker at the place it gets to 0, then
idx = find(iY <= 0, 1);
plot(x(idx), y(idx), 'o');

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Object Properties in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by