MATLAB Solving a Problem with Matlab: An Object Thrown in the Air

  • Thread starter Thread starter roam
  • Start date Start date
  • Tags Tags
    Air Matlab
AI Thread Summary
The discussion centers around a Matlab code issue related to simulating the trajectory of an object thrown in the air. The initial code provided results in a plot showing only a single point due to the way x and y coordinates are defined and plotted within a loop. The solution proposed includes removing the loop and directly calculating x and y for a range of time values, which allows for a proper plot of the object's trajectory. The user initially set the simulation duration to 50,000 seconds, which was deemed excessive, and after adjusting it to 20 seconds, they sought to find the time when the object hits the ground, indicated by the height reaching zero. It was suggested to further refine the time intervals for better accuracy. The discussion highlights the importance of correctly structuring the code and understanding the initial conditions of the simulation.
roam
Messages
1,265
Reaction score
12
I want to solve the following problem using Matlab:

Here's the question regarding an object thrown in the air:

http://img197.imageshack.us/img197/7797/97452743.gif

My code:

Code:
theta=pi/4; v=10;  g=9.81;   a=0.1; 
nfinal=50000;
for t=1:nfinal
    x=((1-exp(-a*t))/a)*v*cos(theta);
    y=((1-exp(-a*t))/a)*((g/a)+v*sin(theta))-((g*t)/a);
end
plot(t,y,'+')

It doesn't produce a proper plot, all I can see is a dot. What is the problem with my code?
 
Last edited by a moderator:
Physics news on Phys.org
The way you are doing it, x and y are single points and you keep redefining them before you plot them. If you want to do it that way, you need to put
plot(t,y,'+'), hold on
inside the loop. The hold on is important in order to keep the previous point on the plot.
 
Actually, even better would be to simply drop the loop completely:
Code:
theta=pi/4; v=10;  g=9.81;   a=0.1; 
nfinal=50000;

t=1:nfinal;
x=((1-exp(-a*t))/a)*v*cos(theta);
y=((1-exp(-a*t))/a)*((g/a)+v*sin(theta))-((g*t)/a);
plot(t,y,'+')

BTW, 50000 seconds is way too long.
 
Oh thanks Matonski! Yeah, it's too long and I changed it to 20 seconds, so here's the graph:

http://img261.imageshack.us/img261/2768/33588615.png

I'm looking for the time when it hits the ground (when the height =0, or the root/x-intercept of the graph). But this graph shows that height=0 after 1 second, I'm not so sure if this is right... :confused:
 
Last edited by a moderator:
you need to break the time interval down shorter. Try setting nfinal to like 2 seconds and have t go in smaller steps like this:
t=1:.01:nfinal;
 
This is my result:

http://img141.imageshack.us/img141/8242/46816894.th.jpg

My code for the loop is

Code:
for t = 0 : 0.1 : 2
     <calculate and plot>
end

When t = 0, the height is zero. It means that the object starts being thrown. Therefore, I think your code has no problem but you just don't consider the initial event of the object.
 
Last edited by a moderator:

Similar threads

Replies
4
Views
1K
Replies
2
Views
3K
Replies
5
Views
2K
Replies
4
Views
2K
Replies
8
Views
3K
Replies
9
Views
5K
Replies
18
Views
4K
Back
Top