C++ Free Fall Velocity Homework

Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the numerical representation of free fall motion using C++ and ROOT for graphing. Participants are examining the implementation of kinematic equations and the behavior of velocity over time in a free fall scenario.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their approach to graphing free fall motion using initial conditions and a specific time step, but notes issues with the output of their code.
  • Another participant questions the representation of kinematic equations in the code, suggesting that it only captures the vertical component of velocity.
  • There is a discussion about the importance of considering both horizontal and vertical components of motion, with some participants emphasizing the need for clarity in how these components are represented in the code.
  • One participant suggests that the variable used for time step incorporates acceleration, which may lead to confusion in the code structure.
  • Several participants express uncertainty about why the velocity is decreasing if the object is in free fall, with some proposing that the choice of coordinate system (positive up) could explain the behavior of velocity.
  • Another participant acknowledges the feedback received and indicates plans to revise their code based on the suggestions made by others.

Areas of Agreement / Disagreement

Participants express differing views on the representation of motion in the code, particularly regarding the treatment of velocity and the implications of the chosen coordinate system. There is no consensus on the correct interpretation of the decreasing velocity in the context of free fall.

Contextual Notes

Some participants note potential limitations in the code, such as the mixing of time step and acceleration in variable definitions, which may affect the clarity and correctness of the implementation. The discussion also highlights unresolved aspects of the kinematic equations as applied in the code.

RJLiberator
Gold Member
Messages
1,094
Reaction score
63

Homework Statement


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.

Homework Equations

The Attempt at a Solution



Code:
  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();
 
Physics news on Phys.org
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
 
  • Like
Likes   Reactions: RJLiberator
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
 
You don't seem to have given proper consideration to the horizontal component of velocity, nor to how x(t) depends on it.
 
  • Like
Likes   Reactions: RJLiberator
RJLiberator said:

Homework Statement


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
Since the object is in freefall (which your title indicates), why is the velocity decreasing?
RJLiberator said:
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.

Homework Equations

The Attempt at a Solution



Code:
  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();

Use
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.
 
  • Like
Likes   Reactions: RJLiberator
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.
 
  • Like
Likes   Reactions: RJLiberator
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.
 
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.

Since the object is in freefall (which your title indicates), why is the velocity decreasing?

Good point, thank you for this observation.

Capture.JPG


Looking pretty solid.
 
Mark44 said:
Since the object is in freefall (which your title indicates), why is the velocity decreasing?
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.
 
  • Like
Likes   Reactions: RJLiberator
  • #10
gneill said:
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.
I agree, but we shouldn't have to guess at what the problem is about.
 
  • Like
Likes   Reactions: RJLiberator

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K