Matlab gives me a blank plot?

Hi! I am trying to write a script that plots every X and Y position of a ball after it is thrown horizontally at a wall that is slanted 60 degrees from the x axis. The ball then bounces of the wall and the plot is basically like a projectile motion plot. There is a picture and more information the the vector mechanics for engineers: dynamics 11 edition book, problem 171 in chapter 13. I do not know why I keep getting a blank plot. I looked at the results and the Y axis is what it should be at the end (0 because it lands), so I do not understand why it is returning a blank plot. I also tried putting hold on and off before and after the for loop respectively, as well as at the beginning and end of the for loop. I have also tried plot(X,Y) instead of line command and I have tried doing plot(X,t) and (Y,t) but I am not getting anything for the plot. The weirdest thing is that it works on my friend's computer and not mine. I decided to copy and paste examples of random plots online and those work, but this script gives me a blank plot for some reason.
Here is my script: Yo=3; %Initial Height
Xo=0; %Initial Distance in the X direction
Vox=25; %Initial Horizontal Velocity
r=0.9; %Coefficient of Restitution
Vw=0; %Velocity of wall before being hit by ball
Vw2=0; %Velocity of Wall after being hit by ball
g=32.2; %Gravity % Wall angled at 60% from the X-axis
% Find the distance, d, from the foot of the wall to the point B where the % ball will hit the ground after bouncing off the wall.
Vt=Vox*sind(30); %Initial Velocity Tangential to Wall
Vn=-Vox*cosd(30); %Initial Velocity Normal to Wall % Vt2=Vo2*sind(30-alpha)=12.5 which is the tangential velocity after % hitting the wall.
% r=(Vw2-Vn2)/(Vn-Vw)is the coefficient of restitution along normal to wall. % Simplified, it is: Vo2cosd(30-alpha)=19.48=Vn2
%Vt2/Vn2= tand(alpha-30)=12.5/19.48. Therefore:
alpha=atand(12.5/19.48)+30;
%Plug into Vn2 to get: Vo2=19.48/cosd(alpha-30); %Vo2 is the initial Velocity of the ball after it hits the wall
Voy2=Vo2*sind(alpha); %Voy2 is the initial Velocity in the Y direction after hitting the wall
Vox2=Vo2*cosd(alpha); %Vox2 is the initial Velocity in the X direction after hitting the wall
hmax=(Voy2)^2/(2*g); %Maximum Height the ball reaches
T1=Voy2/g; %Time from A to Maximum Height
T2=sqrt((2*(hmax+Yo))/g); %Time from Maximum Height to B (when it lands)
Ttotal=T1+T2; %Total time from when the ball hits the wall until it lands
for t=0:0.01:Ttotal Y=Yo+Voy2*t-0.5*g*t^2 %Y position of ball X=Xo+Vox2*t %X position of ball line(X,Y) end title('X Position vs. Y Position of Ball')
xlabel('X Position of Ball') %label for X-axis
ylabel('Y Position of Ball') %label for Y-axis
Thank you in advance!

 Accepted Answer

Chad Greene
Chad Greene on 2 Mar 2016
Edited: Chad Greene on 2 Mar 2016
You're overwriting Y each time through the loop. If you want to keep the loop, try this:
t=0:0.01:Ttotal ;
for k = 1:length(t)
Y(k)=Yo+Voy2*t(k)-0.5*g*t(k)^2 ;%Y position of ball X=Xo+Vox2*t %X position of ball line(X,Y)
end
However, loops are slow. It's fine for this simple case, but it's a good idea to break free of reliance on loops. A faster, simpler, easier-to-read option would be:
t = 0:0.01:Ttotal;
Y = Yo+Voy2.*t-0.5*g*t.^2 ;

1 Comment

Thank you! Your solution worked perfectly! The alternative was even better (much simpler). I personally do not like to use loops but could not figure out how to solve this problem without one. I do have one more question, though. Why is Y being overwritten each time through the loop? If I have the plot in the loop and the hold command on, then shouldn't it keep plotting each Y value on the same plot (since the line command is being looped for each new value of Y)? Wouldn't it define Y based on a value of t, plot that, define a new y based on another t, plot that, etc? Thank you for your help!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!