# MatLab Code for Baseball Not Working

## Homework Statement

I am trying to create this plot in MatLab

I'm starting out doing the simplest one, no drag, no wind. simple kinematics, right? but I can not get my code to run.

## Homework Equations

Kinematic Equations

## The Attempt at a Solution

Code:
close all

clear all

clc

%%defining variables

tMin = 0 %seconds

angleWithHorizontal = 35 %degrees

initialVelocity = 100 * .44704 %converting mph to m/s

tailWind = 10 * .44704 %converting mph to m/s

headWind = 10 *.44704 %converting mph to m/s

gravity = -9.8 %m/s^2

dt = .1 %change in time

mass = 1%unknown

initialVelocityY = initialVelocity * sin(angleWithHorizontal)

initialVelocityX = initialVelocity * cos(angleWithHorizontal)

timeToMaxHeight = -initialVelocity / gravity

tMax = 2*timeToMaxHeight

initialPositionX = 0

initialPositionY = 0

accelerationX = 0

distanceTraveledX = initialPositionX + initialVelocityX * tMax + .5 * accelerationX * tMax^2

%%Position in Space Without Drag nor Wind

t = tMin:dt:tMax; % time steps

[j,k] = size(t);

velocityWithoutDrag(1) = initialVelocityY;

positionX(1) = initialPositionX;

positionY(1) = initialPositionY;

% calculate instantenous speeds and position

for i = 2:k

velocityWithoutDrag(i) = velocityWithoutDrag(i-1) + (gravity*dt)

positionX(i) = positionX(i-1) + velocityWithoutDrag(i)*dt + .5*gravity*dt^2

positionY(i) = positionY(i-1) + velocityWithoutDrag(i)*dt + .5*gravity*dt^2

end

%%Plots

figure(1)

hold on

box on

plot(positionX,positionY,'-k')

set(gca,'Xtick',linspace(0,150,4),'fontsize',18,'Ytick',linspace(0,30,4))

set(gcf,'Color','w');

set(gcf,'Resize','on');

hold off

whenever I try to plot, I get a linear line. and the graph doesn't even have the correct domain and range or increments. It is weird because I borrowed the code from a previous problem I did, and just changed a bit of it.

I'm certain the problem is probably with my matrices, but I am still new to this. At this point I am just tackling the baseball in a vacuum part of the plot.

#### Attachments

• Screen Shot 2018-03-04 at 11.25.32 PM.png
37.8 KB · Views: 657
• ?temp_hash=508bdd4a90a57a3982ca08ab7b90103b.png
37.8 KB · Views: 658
Last edited:

FactChecker
Gold Member
Your X and Y equations are essentially identical, so the plot will be linear. Gravity does not effect the X velocity (if that is horizontal).

Your X and Y equations are essentially identical, so the plot will be linear. Gravity does not effect the Y velocity (if that is horizontal).

But they aren't identical. the acceleration in the X equation is 0 so that term is 0. the y equation has an acceleration of -9.8 so it should go up and then come back down.

What did I do wrong

edit: I see what I did. I see what you are referring to. right variable in one part. wrong variable in another. for x

FactChecker
Gold Member
But they aren't identical. the acceleration in the X equation is 0 so that term is 0. the y equation has an acceleration of -9.8 so it should go up and then come back down.

What did I do wrong
Regardless of how they start and the initial rates, the horizontal position and velocity (I guess the X values) should not be affected by gravity.

PS. I changed the horizontal from Y to X in my post #2.

@FactChecker

Ok I corrected the typo in my look. the acceleration in the X position formula has been corrected. my plot is still incorrect though.

Matlab:
close all
clear all
clc

%%defining variables
tMin = 0 %seconds
angleWithHorizontal = 35 %degrees
initialVelocity = 100 * .44704 %converting mph to m/s
tailWind = 10 * .44704 %converting mph to m/s
headWind = 10 *.44704 %converting mph to m/s
gravity = -9.8 %m/s^2
dt = .1 %change in time
mass = 1%unknown
initialVelocityY = initialVelocity * sin(angleWithHorizontal)
initialVelocityX = initialVelocity * cos(angleWithHorizontal)
timeToMaxHeight = -initialVelocity / gravity
tMax = 2*timeToMaxHeight
initialPositionX = 0
initialPositionY = 0
accelerationX = 0
distanceTraveledX = initialPositionX + initialVelocityX * tMax + .5 * accelerationX * tMax^2

%%Position in Space Without Drag nor Wind
t = tMin:dt:tMax; % time steps
[j,k] = size(t);
velocityWithoutDrag(1) = initialVelocityY;
positionX(1) = initialPositionX;
positionY(1) = initialPositionY;

% calculate instantenous speeds and position
for i = 2:k
velocityWithoutDrag(i) = velocityWithoutDrag(i-1) + (gravity.*dt)
positionX(i) = positionX(i-1) + velocityWithoutDrag(i)*dt + .5*accelerationX.*dt.^2
positionY(i) = positionY(i-1) + velocityWithoutDrag(i)*dt + .5*gravity.*dt.^2
end

%%Plots
figure(1)
hold on
box on
plot(positionX,positionY,'-k')
set(gca,'Xtick',linspace(0,150,4),'fontsize',18,'Ytick',linspace(0,30,4))
set(gcf,'Color','w');
set(gcf,'Resize','on');
hold off

Last edited by a moderator:
Regardless of how they start and the initial rates, the horizontal position and velocity (I guess the X values) should not be affected by gravity.
that was a typo. I have a variable accelerationX that was supposed to be in the positionX formula. thanks for pointing it out. but the problem persists...

edit: I'm looking at my formula again, and using the same value for velocity in both equations. that isn't what I meant to do either. yikes!

let me rework this. and come back. thanks for your help!

FactChecker
Gold Member
Be very careful and methodical about which positions and velocities you apply gravity to. Keep track of the X and Y values separately for position and velocity. I think that will make it work.

Be very careful and methodical about which velocities and accelerations you apply gravity to. Keep track of the X and Y values separately for position and velocity. I think that will make it work.

yeah. you can see from the start of my code that I began by properly creating the components. but in trying to reuse code I don't completely understand that modeled a 1-D problem. I got so caught up and figuring out how to adapt the code properly, the physics got skewed.

I'm going to create two separate loops I think. Will probably need a different matrix for the 2 velocities.
edit: x velocity doesn't change. lol. overcomplicating things.

@FactChecker

OK! Now we are making progress. I corrected my formulas, and the graph is parabolic the way it should be. but it is not showing the correct domain and range. it's like from something to 0 in both the domain and range. I don't get it.

Matlab:
close all
clear all
clc

%%defining variables
tMin = 0 %seconds
angleWithHorizontal = 35 %degrees
initialVelocity = 100 * .44704 %converting mph to m/s
tailWind = 10 * .44704 %converting mph to m/s
headWind = 10 *.44704 %converting mph to m/s
gravity = -9.8 %m/s^2
dt = .1 %change in time
mass = 1%unknown
initialVelocityY = initialVelocity * sin(angleWithHorizontal)
initialVelocityX = initialVelocity * cos(angleWithHorizontal)
timeToMaxHeight = -initialVelocity / gravity
tMax = 2*timeToMaxHeight
initialPositionX = 0
initialPositionY = 0
accelerationX = 0
distanceTraveledX = initialPositionX + initialVelocityX * tMax + .5 * accelerationX * tMax^2

%%Position in Space Without Drag nor Wind
t = tMin:dt:tMax; % time steps
[j,k] = size(t);
velocityWithoutDragY(1) = initialVelocityY;
positionX(1) = initialPositionX;
positionY(1) = initialPositionY;

% calculate instantenous speeds and position
for i = 2:k
velocityWithoutDragY(i) = velocityWithoutDragY(i-1) + (gravity.*dt)
positionX(i) = positionX(i-1) + initialVelocityX*dt + .5*accelerationX.*dt.^2
positionY(i) = positionY(i-1) + velocityWithoutDragY(i)*dt + .5*gravity.*dt.^2
end

%%Plots
figure(1)
hold on
box on
plot(positionX,positionY,'-k')
set(gca,'Xtick',linspace(0,150,4),'fontsize',18,'Ytick',linspace(0,30,4))
set(gcf,'Color','w');
set(gcf,'Resize','on');
hold off

Last edited by a moderator:
FactChecker
Gold Member
The thing to do is to nurse it through step-by-step and see that each step calculates the values that make sense. Running the whole thing fills up the screen and all I can see is that all your X and Y values are negative and get more negative with time. That is wrong. Try one command at a time from the beginning.

FactChecker
Gold Member
sin() and cos() usually need inputs in radians, not degrees.

sin() and cos() usually need inputs in radians, not degrees.

that's it. I did not know that. but converting degrees to radians brings me very close to my desired plot.

it's not perfect though. it looks weird. it is plotting more points than I told it to, and seems to be calculating more than what I think I told it to.

Orodruin
Staff Emeritus
Homework Helper
Gold Member
timeToMaxHeight = -initialVelocity / gravity
You might want to rethink this.

grandpa2390 and FactChecker
You might want to rethink this.

darn Typos. Even using descriptive variable names, I still miss them. That seems to do the trick. the domain and range are still a bit weird though. It isn't going to match the picture yet because the picture has drag and wind effects applied. but why is the y axis starting so high?

FactChecker
Gold Member
Where is your Y axis starting? It's difficult to know what your current code is (if you re-post, please remove all those accumulated blank lines.) If I make the changes that I think you should have made based on the comments, the plot looks reasonable to me.

Where is your Y axis starting? It's difficult to know what your current code is (if you re-post, please remove all those accumulated blank lines.) If I make the changes that I think you should have made based on the comments, the plot looks reasonable to me.

ugh. I don't know where the blank spaces come from.
Code:
close all
clear all
clc

%%defining variables
tMin = 0 %seconds
angleWithHorizontal = 35*0.0174533 %degrees to radians
initialVelocity = 100 * .44704 %converting mph to m/s
tailWind = 10 * .44704 %converting mph to m/s
headWind = 10 *.44704 %converting mph to m/s
gravity = -9.8 %m/s^2
dt = .1 %change in time
mass = 1%unknown
initialVelocityY = initialVelocity * sin(angleWithHorizontal)
initialVelocityX = initialVelocity * cos(angleWithHorizontal)
timeToMaxHeight = -initialVelocityY / gravity
tMax = 2*timeToMaxHeight
initialPositionX = 0
initialPositionY = 0
accelerationX = 0
distanceTraveledX = initialPositionX + initialVelocityX * tMax + .5 * accelerationX * tMax^2

%%Position in Space Without Drag nor Wind
t = tMin:dt:tMax; % time steps
[j,k] = size(t);
velocityWithoutDragY(1) = initialVelocityY;
positionX(1) = initialPositionX;
positionY(1) = initialPositionY;

% calculate instantenous speeds and position??for i = 2:k
for i = 2:k
velocityWithoutDragY(i) = velocityWithoutDragY(i-1) + (gravity.*dt)
positionX(i) = positionX(i-1) + initialVelocityX*dt + .5*accelerationX.*dt^2
positionY(i) = positionY(i-1) + velocityWithoutDragY(i)*dt + .5*gravity.*dt^2
end

%%Plots

figure(1)
hold on
box on
plot(positionX,positionY,'-k')
set(gca,'Xtick',linspace(0,150,4),'fontsize',18,'Ytick',linspace(0,30,4))
set(gcf,'Color','w');
set(gcf,'Resize','on');
hold off

FactChecker
Gold Member
That's the same plot that I expected. The Y axis goes up to 40 because it is marking in increments of 10 and the Y values go slightly over 30. Usually I initially let MATLAB decide it's own axis limits so I can easily get the whole picture and then I specify different limits like ylim([0 32]) if desired.

That's the same plot that I expected. The Y axis goes up to 40 because it is marking in increments of 10 and the Y values go slightly over 30. Usually I initially let MATLAB decide it's own axis limits so I can easily get the whole picture and then I specify different limits like ylim([0 32]) if desired.

So how do you do that? where do type ylim([0 32]) at in the code? I thought linspace was supposed to handle that. but it isn't do a good job of it for me.

FactChecker
Gold Member
After the plot and before the "hold off". I don't know why the linspace doesn't work, but you have two of them. The documentation I see for linspace does not apply to a plot.

grandpa2390
After the plot and before the "hold off". I don't know why the linspace doesn't work, but you have two of them. The documentation I see for linspace does not apply to a plot.

Beautiful!

Don't worry, I'm not crazy. The actual plot I am working toward will include drag. And the ball will hit its peak at about X=70 and land somewhere at about X=120. But first things first. Have to figure out how to do the simple things (like simple kinematics) before I start tossing in more complicated things like drag :) :) :)

#### Attachments

• Screen Shot 2018-03-06 at 5.11.16 PM.png
9.4 KB · Views: 320
• ?temp_hash=0368d0b1322201e4e4b887a096f8180a.png
9.4 KB · Views: 163
FactChecker
@FactChecker I hate to open an all-new thread for a dumb (or extremely simple) question. When am I supposed to end a line with a semi-colon. I've been throwing semi-colons behind lines that declare matrices because, that is just the way I have seen it done. is that correct?

and

and what exactly is this line doing?
Code:
t = tMin:dt:tMax; % time steps
[j,k] = size(t);

it looks like I am declaring a variable, t, and saying that it is equal to (tMax-tMin)/dt thereby giving me the total number of steps to be performed
then I am declaring a matrix j hat and k hat, each with a size equal to t.
so from 0 to 2 with dt=.5, t =4 and the matrix [j,k] has 4 positions for both j and k. which is then used in my for loop for i=2:k (which is 4). incidentally it seems, if I am following correctly, the j was extra that I didn't need.

but if I delete j and just have:
Code:
[k]=size(t);
I don't get an error, but I don't get a plot either.

Forgive me, I'm not trying to be a nuisance. : )

Mark44
Mentor
(if you re-post, please remove all those accumulated blank lines.)
I removed most of the blank lines from the earlier code posted.

@grandpa2390, when you post code, add the type of code that it is (like C, Java, matlab, etc.). To do this, inside the opening code tag, add =matlab, with no spaces before or after =.

Like this:
[code=matlab]
% some code
[/code]

I removed most of the blank lines from the earlier code posted.

@grandpa2390, when you post code, add the type of code that it is (like C, Java, matlab, etc.). To do this, inside the opening code tag, add =matlab, with no spaces before or after =.

Like this:
[code=matlab]
% some code
[/code]

let me try
Matlab:
t = tMin:dt:tMax; % time steps
[j,k] = size(t);

why is j and size in the same color? and clickable, but k isn't?

edit: I clicked on it and am reading the page it linked to :)

let me try
Matlab:
t = tMin:dt:tMax; % time steps
[j,k] = size(t);

why is j and size in the same color? and clickable, but k isn't?

edit: I clicked on it and am reading the page it linked to :)
@FactChecker

ok so based on this link
Matlab:
size(t);
it seems I figured accurately what it meant. but I still don't understand why a matrix with two rows of size 4 is necessary when only one row is used? or I don't understand how it is being used?

edit: or maybe I don't understand what "Returns the number" means.

FactChecker
Gold Member
A semicolon at the end of a MATLAB code line stops MATLAB from printing the result of the line. You want to do that so it will not fill the screen with data on lines that you already trust and do not need to see the results. When you are debugging, you might want to remove a semicolon at the end of a MATLAB line to see the results and make sure they are as expected. For instance, to see what is going on in your definition of t and size(t), remove the trailing semicolons to see that t is a 1xN matrix and the size function sets i and j to the dimensions. 'tMin:dt:tMax' is a loop from tMin to tMax in steps of dt.

Matlab:
>> tMin=0;
>> tMax=5;
>> dt = 1;
>> t = tMin:dt:tMax

t =

0     1     2     3     4     5

>> [j,k] = size(t)

j =

1

k =

6

I remove trailing semicolons to debug lines and then put them back when I'm confident of that line.

PS. A semicolon inside a matrix definition is different. It starts a new matrix definition row. There may be other uses of the semicolon inside a MATLAB line; I can't think of any now.

grandpa2390