Obtain value of a variable when y = 0 in a function
1 view (last 30 days)
Show older comments
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.
Thanks for taking the time to help me, I look foward to learn more and more about this topics.
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)')
0 Comments
Accepted Answer
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');
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) & 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
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).
.
0 Comments
More Answers (0)
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!