How to create a graph

3 vues (au cours des 30 derniers jours)
Abrahim le 23 Sep 2022
Commenté : Abrahim le 23 Sep 2022
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: ");
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 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Réponses (2)

Geoff Hayes le 23 Sep 2022
Modifié(e) : Geoff Hayes le 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 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Image Analyst le 23 Sep 2022
Modifié(e) : Image Analyst le 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: ");
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.
1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Abrahim le 23 Sep 2022
Thank you very much that was very helpful.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by