Plot function only plots one coordinate

1 view (last 30 days)
For my matlab introduction class Im trying to plot a graph by feeding it coordinates obtained by the (x =) and (y=) equations. The problem is that the obtained graph has no points on it; At the moment I dont know how to proceed.
clear
clc
fprintf('Parabolic motion 1\n');
fprintf(1,'Provide an input for the following constants\n');
V0 = input('Initial velocity = ');
b0 = input('Angle = ');
C = input('"C" = Proyectile mass / air resistance coefficient = ');
t = input('Number of desired plotted points = ');
vy = V0*cosd(b0)
vx = V0*sind(b0)
x=0
y=0
i=0
while (i <= t)
x= C * vx * (1-2.718^(-(i)/C));
y= (C * vy + 9.8 * ((C)^2))*(1-2.718^(-(i)/C))-9.8 * C * (i);
disp(x)
disp(y)
plot(x,y)
xlabel('Traveled distance (m)')
ylabel('Altitude (m)')
i++ ;
end
If anyone has a few minutes to take a look at this, I'd really appreciate it. I can't seem to find a solution. Thank you!

Accepted Answer

Star Strider
Star Strider on 8 Feb 2022
MATLAB does not use C increment syntax.
One approach would be to subscript them and plot them at the end. That is definitely the more efficient approach.
% clear
% clc
fprintf('Parabolic motion 1\n');
Parabolic motion 1
fprintf(1,'Provide an input for the following constants\n');
Provide an input for the following constants
% V0 = input('Initial velocity = ');
% b0 = input('Angle = ');
% C = input('"C" = Proyectile mass / air resistance coefficient = ');
% t = input('Number of desired plotted points = ');
V0 = 420;
b0 = 60;
C = pi;
t = 25;
vy = V0*cosd(b0)
vy = 210
vx = V0*sind(b0)
vx = 363.7307
x=0
x = 0
y=0
y = 0
i=1
i = 1
while (i <= t)
x(i) = C * vx * (1-2.718^(-(i)/C));
y(i) = (C * vy + 9.8 * ((C)^2))*(1-2.718^(-(i)/C))-9.8 * C * (i);
disp(x)
disp(y)
% plot(x,y)
% xlabel('Traveled distance (m)')
% ylabel('Altitude (m)')
i = i+1 ;
end
311.4967
175.4214
311.4967 538.0799
175.4214 294.6306
311.4967 538.0799 702.8968
175.4214 294.6306 372.9508
311.4967 538.0799 702.8968 822.7848
175.4214 294.6306 372.9508 421.5283
311.4967 538.0799 702.8968 822.7848 909.9915
175.4214 294.6306 372.9508 421.5283 448.4711
311.4967 538.0799 702.8968 822.7848 909.9915 973.4258
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294 1.1330
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475 288.2540
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294 1.1330 1.1357
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475 288.2540 259.2079
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294 1.1330 1.1357 1.1376
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475 288.2540 259.2079 229.6870
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294 1.1330 1.1357 1.1376 1.1390
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475 288.2540 259.2079 229.6870 199.8209
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294 1.1330 1.1357 1.1376 1.1390 1.1400
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475 288.2540 259.2079 229.6870 199.8209 169.7035
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294 1.1330 1.1357 1.1376 1.1390 1.1400 1.1407
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475 288.2540 259.2079 229.6870 199.8209 169.7035 139.4035
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294 1.1330 1.1357 1.1376 1.1390 1.1400 1.1407 1.1413
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475 288.2540 259.2079 229.6870 199.8209 169.7035 139.4035 108.9705
1.0e+03 * 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294 1.1330 1.1357 1.1376 1.1390 1.1400 1.1407 1.1413 1.1417
175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475 288.2540 259.2079 229.6870 199.8209 169.7035 139.4035 108.9705 78.4408
1.0e+03 * Columns 1 through 22 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294 1.1330 1.1357 1.1376 1.1390 1.1400 1.1407 1.1413 1.1417 Column 23 1.1419
Columns 1 through 22 175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475 288.2540 259.2079 229.6870 199.8209 169.7035 139.4035 108.9705 78.4408 Column 23 47.8409
1.0e+03 * Columns 1 through 22 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294 1.1330 1.1357 1.1376 1.1390 1.1400 1.1407 1.1413 1.1417 Columns 23 through 24 1.1419 1.1421
Columns 1 through 22 175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475 288.2540 259.2079 229.6870 199.8209 169.7035 139.4035 108.9705 78.4408 Columns 23 through 24 47.8409 17.1898
1.0e+03 * Columns 1 through 22 0.3115 0.5381 0.7029 0.8228 0.9100 0.9734 1.0196 1.0531 1.0775 1.0953 1.1082 1.1176 1.1245 1.1294 1.1330 1.1357 1.1376 1.1390 1.1400 1.1407 1.1413 1.1417 Columns 23 through 25 1.1419 1.1421 1.1423
Columns 1 through 22 175.4214 294.6306 372.9508 421.5283 448.4711 459.6766 459.4349 450.8663 436.2409 417.2097 394.9738 370.4066 344.1438 316.6475 288.2540 259.2079 229.6870 199.8209 169.7035 139.4035 108.9705 78.4408 Columns 23 through 25 47.8409 17.1898 -13.4986
figure
plot(x,y)
grid
xlabel('Traveled distance (m)')
ylabel('Altitude (m)')
The disp calls in the loop are not necessary. It would be beter to display the stored values after the loop finishes.
.
  2 Comments
Tomas White
Tomas White on 9 Feb 2022
Thank you for your time! I understood your explaination pretty clearly
Star Strider
Star Strider on 9 Feb 2022
As always, my pleasure!
Thank you!

Sign in to comment.

More Answers (1)

David Hill
David Hill on 8 Feb 2022
fprintf('Parabolic motion 1\n');
fprintf(1,'Provide an input for the following constants\n');
V0 = input('Initial velocity = ');
b0 = input('Angle = ');
C = input('"C" = Proyectile mass / air resistance coefficient = ');
t = input('Number of desired plotted points = ');
T=linspace(0,10,t);%how long do you want (I assumed 10 seconds)
vy = V0*cosd(b0);
vx = V0*sind(b0);
x= C * vx * (1-2.718.^(-(T)/C));
y= (C * vy + 9.8 * ((C)^2))*(1-2.718.^(-(T)/C))-9.8 * C * T;
plot(x,y);

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!