# Obtain value of a variable when y = 0 in a function

1 view (last 30 days)
Tomas White on 9 Feb 2022
Answered: Star Strider on 9 Feb 2022
Im going trought matlab intro in college and got this project that is making me crazy.
The X and Y equations listed below on the code represent the X and Y components of coordinate pairs that simulate the position at an specific time while an object is going through parabolic motion. I must generate various (X,Y) components that plot the trayectory with the inputed constants whom will satisfy such functions. My problem is that I do not know to make this code stop plotting when it "reaches the ground" (X,0) and calculate the time it takes to do that (time is represented by "i" in the X and Y equation). The only way that I got the program to plot something was by generating a loop that placed an iteration limit (represented by "u" on the code); I have not been able to obtain the "i" value when the coordinate (X,0) is generated.
clear
clc
%Data inputs
fprintf('parabolic motion 1\n');
fprintf(1,'Provide an entry for the following constants\n');
V0 = input('Initial velocity = ');
b0 = input('Theta = ');
C = input('"C" = Proyectile mass / air resistance coefficient = ');
u = input('Number of desired plotted points = ');
vy = V0*cosd(b0)
vx = V0*sind(b0)
x=0
y=0
i=1
while (i <= u)
%X and Y equations that make up the coordinate pair
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)
i = i+1 ;
end
figure
plot(x,y)
grid
xlabel('Distance (m)')
ylabel('Altitude (m)')

Star Strider on 9 Feb 2022
One option is to add a test for the value of ‘y’ as I did here.
V0 = 420;
b0 = 60;
C = pi;
u = 50;
%Data inputs
fprintf('parabolic motion 1\n');
parabolic motion 1
fprintf(1,'Provide an entry for the following constants\n');
Provide an entry for the following constants
% V0 = input('Initial velocity = ');
% b0 = input('Theta = ');
% C = input('"C" = Proyectile mass / air resistance coefficient = ');
% u = input('Number of desired plotted points = ');
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 <= u) & all(y >= 0)
%X and Y equations that make up the coordinate pair
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)
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('Distance (m)')
ylabel('Altitude (m)')
Since the ‘y’ test only stops when the first value of ‘y’ is negative, one option is to plot only ‘t(1:end-1)’ and ‘y(1:end-1)’ so ‘y’ always remains positive on the plot (even though using that approach it may never actually reach 0 and so is left floating above the ground for all eternity as the result).
.

R2020a

### Community Treasure Hunt

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

Start Hunting!