MATLAB Answers

graph won't plot in correct spot

1 view (last 30 days)
Aidan Palermo
Aidan Palermo on 19 Sep 2021
Edited: Walter Roberson on 19 Sep 2021
% plot mountain
Mn_x = [0, 1000, 3014.8];
Mn_y = [0, 6, 1000];
plot (Mn_x, Mn_y)
hold on
% known variables
Xo = 0;
Yo = 1;
V = 585; % m/s
G = 9.81; % m/s^2
t = linspace(0, 8, 800);
th1 = 10; % degrees
th2 = 12; % degrees
th3 = 14; % degrees
% calculate trajectory of howitzer
x_th1 = Xo + (V * cos(th1)) * t
y_th1 = Yo + (V * sin(th1)) * t - 1/2 * G * (t.^2)
plot (x_th1, y_th1, 'r')
Plot (x_th1, y_th1) is supposed to start at (0,1) but actually starts at (-3900,-2800) I've tried fixing the equations but I can't figure out whats wrong.

Answers (1)

Walter Roberson
Walter Roberson on 19 Sep 2021
Edited: Walter Roberson on 19 Sep 2021
If you do not want negative values shown, then clip them out of the plot by adjusting xlim and ylim.
% plot mountain
Mn_x = [0, 1000, 3014.8];
Mn_y = [0, 6, 1000];
plot (Mn_x, Mn_y)
hold on
% known variables
Xo = 0;
Yo = 1;
V = 585; % m/s
G = 9.81; % m/s^2
t = linspace(0, 8, 800);
th1 = 10; % degrees
th2 = 12; % degrees
th3 = 14; % degrees
% calculate trajectory of howitzer
x_th1 = Xo + (V * cos(th1)) * t
x_th1 = 1×800
0 -4.9147 -9.8294 -14.7441 -19.6588 -24.5736 -29.4883 -34.4030 -39.3177 -44.2324 -49.1471 -54.0618 -58.9765 -63.8913 -68.8060 -73.7207 -78.6354 -83.5501 -88.4648 -93.3795 -98.2942 -103.2089 -108.1237 -113.0384 -117.9531 -122.8678 -127.7825 -132.6972 -137.6119 -142.5266
y_th1 = Yo + (V * sin(th1)) * t - 1/2 * G * (t.^2)
y_th1 = 1×800
1.0000 -2.1870 -5.3750 -8.5639 -11.7539 -14.9448 -18.1367 -21.3296 -24.5235 -27.7184 -30.9142 -34.1111 -37.3089 -40.5077 -43.7075 -46.9082 -50.1100 -53.3127 -56.5164 -59.7211 -62.9268 -66.1335 -69.3411 -72.5498 -75.7594 -78.9700 -82.1816 -85.3941 -88.6077 -91.8222
plot (x_th1, y_th1, 'r')
XL = xlim();
YL = ylim();
xlim( [max(0, XL(1)), XL(2)]);
ylim( [max(1, YL(1)), YL(2)]);
  1 Comment
Walter Roberson
Walter Roberson on 19 Sep 2021
Reminder: cos() and sin() expect radians . If you want to work with degrees, you need cosd() and sind()

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!