1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ Free fall velocity

  1. Jan 31, 2016 #1

    RJLiberator

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    I'm using ROOT (c++) to graph a numerical representation of the equation of motion.
    For simplicity, I am using initial velocity = 10 and initial height = 10.
    change in time = 0.05
    so we find velocity by V_n = V_(n-1) -9.81*0.05

    For some reason, this code is not giving me a correct reading.

    I graph this against the computational method, and the computational method works perfectly.


    2. Relevant equations


    3. The attempt at a solution

    Code (Text):

      float x[100], y[100];
      int n = 100;
      float timestep = 9.81*0.05;
      float v = 10;
      for (int i=0;i<n;i++) {
      x[i] = i*timestep;
      y[i] = v-timestep;
      v = v-timestep;
      }
    TGraph *gr = new TGraph(n,x,y);
      gr->SetMarkerColor(kBlue);
      gr->SetMarkerStyle(29);
      gr->Draw();
     
     
  2. jcsd
  3. Jan 31, 2016 #2
    I don't see how your code represents the kinematic equations:

    x(t) = x_0 + v_x*t
    y(t) = y_0 + v_y0*t - 0.5*g*t^2

    v_x(t) = v_x0
    v_y(t) = v_y0 - g*t
     
  4. Jan 31, 2016 #3

    RJLiberator

    User Avatar
    Gold Member

    So what you are saying is that I am only observing the v_y(t) element? As my code should represent that part of it.

    The x-axis should be time in seconds.

    This is an example graph I did earlier based on my computational graph (which works out perfectly).

    one.JPG
     
  5. Jan 31, 2016 #4
    You don't seem to have given proper consideration to the horizontal component of velocity, nor to how x(t) depends on it.
     
  6. Jan 31, 2016 #5

    Mark44

    Staff: Mentor

    Since the object is in freefall (which your title indicates), why is the velocity decreasing?
    Use [code=c] at the top of your code. This highlights C/C++ reserved words.
    Also, indenting loops and other control structures (such as if and switch) makes your code much easier to read.
    I would do it like this:
    Code (C):

    float x[100], y[100];
      int n = 100;
      float timestep = 9.81*0.05;
      float v = 10;
      for (int i=0;i<n;i++) {
      x[i] = i*timestep;
      y[i] = v-timestep;
      v = v-timestep;
      }
    TGraph *gr = new TGraph(n,x,y);
      gr->SetMarkerColor(kBlue);
      gr->SetMarkerStyle(29);
      gr->Draw();
     
    One other thing -- declaring timestep like this is not a good idea:
    float timestep = 9.81*0.05;
    Keep the 9.81 constant out -- just make the time steps actual time intervals, and use the constant when you calculate the values in your x and v arrays. And for vertical distances, I would definitely use y, not x. That confused me for a bit.
     
  7. Jan 31, 2016 #6

    gneill

    User Avatar

    Staff: Mentor

    You've incorporated the acceleration into your timestep variable, no doubt as a nod to efficiency so as not to perform the multiplication g*Δt on each iteration. But you've used timestep to keep track of the time, too.
     
  8. Jan 31, 2016 #7

    RJLiberator

    User Avatar
    Gold Member

    Excellent guys!

    That does seem to be the issue! Time for me to get back to work on this, I'll report back if that was the problem!

    @Mark44 thank you for the remarks there. I will apply them to my code once I have a working product as right now, things are a bit of a mess :). Coding the night away.
     
  9. Jan 31, 2016 #8

    RJLiberator

    User Avatar
    Gold Member

    Looking good guys! That was the problem!

    Still lots of touching up to do, but it is pretty awesome that I got the numerical and computational approaches working here.

    Good point, thank you for this observation.

    Capture.JPG

    Looking pretty solid.
     
  10. Jan 31, 2016 #9

    gneill

    User Avatar

    Staff: Mentor

    If the chosen axes have "up" as positive then I don't see a problem with an initial positive upward velocity decreasing and then going negative. The old, "rock is tossed vertically upwards at the edge of a tall cliff" scenario comes to mind.
     
  11. Jan 31, 2016 #10

    Mark44

    Staff: Mentor

    I agree, but we shouldn't have to guess at what the problem is about.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C++ Free fall velocity
  1. Free falling object (Replies: 6)

Loading...