View Full Version : Help with Matlab
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:
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?
matonski
Sep4-09, 12:41 AM
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.
matonski
Sep4-09, 01:02 AM
Actually, even better would be to simply drop the loop completely:
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:
matonski
Sep4-09, 04:52 AM
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;
ApexOfDE
Sep7-09, 12:18 AM
This is my result:
http://img141.imageshack.us/img141/8242/46816894.th.jpg
My code for the loop is
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.
vBulletin® v3.7.6, Copyright ©2000-2009, Jelsoft Enterprises Ltd.