Help with Matlab

  • MATLAB
  • Thread starter roam
  • Start date
  • #1
1,266
11

Main Question or Discussion Point

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 [Broken]

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:

Answers and Replies

  • #2
166
0
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.
 
  • #3
166
0
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.
 
  • #4
1,266
11
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 [Broken]

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:
  • #5
166
0
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;
 
  • #6
122
0
This is my result:

http://img141.imageshack.us/img141/8242/46816894.th.jpg [Broken]

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 dont consider the initial event of the object.
 
Last edited by a moderator:

Related Threads for: Help with Matlab

  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
3
Views
8K
Replies
1
Views
550
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
0
Views
1K
  • Last Post
Replies
2
Views
2K
Top