Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with Matlab

  1. Sep 3, 2009 #1
    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 (Text):
    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: May 4, 2017
  2. jcsd
  3. Sep 3, 2009 #2
    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.
     
  4. Sep 4, 2009 #3
    Actually, even better would be to simply drop the loop completely:
    Code (Text):
    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.
     
  5. Sep 4, 2009 #4
    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: May 4, 2017
  6. Sep 4, 2009 #5
    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;
     
  7. Sep 6, 2009 #6
    This is my result:

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

    My code for the loop is

    Code (Text):
    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: May 4, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help with Matlab
  1. MATLAB help (Replies: 9)

  2. MATLAB help (Replies: 2)

  3. MATLAB help (Replies: 0)

  4. MATLAB help (Replies: 7)

  5. Matlab help (Replies: 6)

Loading...