How to create a graph
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am new to Matlab and I am trying to create a graph. However, every time I generate a graph, it does not show the points. For my program, I am creating a projctial motion calculator that asks the user for intial velocity and angle. Then it uses these variables to calculate the distance it traveled in the x and y direction. Here is my program:
%This program calculates the projectial motion of a ping pong ball
%Ask user to input inital velocity and angle
vel=input("Input initial velocity in m/sec: ");
angle=input("Input the angle in launch in degrees: ");
%Converts degrees to radian
angle=angle*pi/180;
%Assigning variables to neccessery values
x(1)=0;
y(1)=0.1;
time(1)=0;
mass=0.00247;
g=-9.8;
%Calculates the x velocity with the y velocity
velx=vel*cos(angle);
Velx(1)=velx;
velFinalX=velx;
vely=vel*sin(angle);
Vely(1)=vely;
deltaTime=0.001;
height=y(1);
%Calculates for the distance the ball traveled in the x and y direction
while height<=0
index=index+1;
accelY=g;
velFinalY=Vely(index-1)+(accelY*deltaTime);
Vely(index)=velFinalY;
distx=velx*deltaTime;
disty=Vely(index)*deltaTime;
x(index)=x(index-1)+distx;
y(index)=y(index-1)+disty;
height=y(index);
end
%Display graph
plot(x,y)
title("distance traveled by ping pong ball in meters")
xlabel("horizontal distance traveled (meters)")
ylabel("vertical distance traveled (meters)")
0 Kommentare
Antworten (2)
Geoff Hayes
am 23 Sep. 2022
Bearbeitet: Geoff Hayes
am 23 Sep. 2022
@Abrahim - try changing the condition on your while loop so that you execute the code so long as the height is greater than zero
while height>0
With the condition the other way, you never enter this loop.
You will also need to initialize the index variable before you try to use it.
0 Kommentare
Image Analyst
am 23 Sep. 2022
Bearbeitet: Image Analyst
am 23 Sep. 2022
This "works"
% This program calculates the projectile motion of a ping pong ball
% Ask user to input inital velocity and angle
vel=input("Input initial velocity in m/sec: ");
angle=input("Input the angle in launch in degrees: ");
%Converts degrees to radian
angle=angle*pi/180;
% Assigning variables to neccessary values
x(1)=0;
y(1)=0.1;
time(1)=0;
mass=0.00247;
g=-9.8;
% Calculates the x velocity with the y velocity
velx=vel*cos(angle);
Velx(1)=velx;
velFinalX=velx;
vely=vel*sin(angle);
Vely(1)=vely;
deltaTime=0.001;
height=y(1);
% Calculates for the distance the ball traveled in the x and y direction
index = 1;
x(1) = 0;
y(1) = 0;
while height >= 0
index=index+1;
accelY=g;
velFinalY=Vely(index-1)+(accelY*deltaTime);
Vely(index)=velFinalY;
distx=velx*deltaTime;
disty =Vely(index)*deltaTime;
x(index)=x(index-1)+distx;
y(index)=y(index-1)+disty;
height=y(index);
end
% Display graph
plot(x,y, 'b-')
grid on;
title("Distance traveled by ping pong ball in meters", 'FontSize',fontSize)
xlabel("Horizontal distance traveled (meters)", 'FontSize',fontSize)
ylabel("vertical distance traveled (meters)", 'FontSize',fontSize)
However I think your logic is wrong. The vertical distance traveled will never decrease. The height will decrease but the total distance traveled will never decrease even when the ball is falling. You probably need to take the absolute value of the velocity.
By the way, see my attached demo on projectile that commputes just about everything you could possibly want to compute.
Siehe auch
Kategorien
Mehr zu RF Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!