C++ Free Fall Velocity Homework

In summary: It should be stated explicitly in the problem statement.In summary, the conversation discusses using ROOT (c++) to graph a numerical representation of the equation of motion. Initial conditions, such as initial velocity and initial height, are set and the code calculates velocity using the formula V_n = V_(n-1) -9.81*0.05. However, the resulting graph is incorrect and the conversation suggests that the issue may be due to not properly considering the horizontal component of velocity. The conversation also includes suggestions for improving the code, such as using [code=c] and indenting control structures. Finally, it is noted that the problem statement should explicitly state the reason for the decreasing velocity.
  • #1
RJLiberator
Gold Member
1,095
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
  • #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
 
  • Like
Likes RJLiberator
  • #3
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
 
  • #4
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 RJLiberator
  • #5
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 [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:
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 RJLiberator
  • #6
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 RJLiberator
  • #7
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.
 
  • #8
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.
 
  • #9
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 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 RJLiberator

1. What is the formula for calculating free fall velocity in C++?

The formula for calculating free fall velocity is v = sqrt(2 * g * h), where g is the acceleration due to gravity and h is the height of the object.

2. How do I input the necessary values for the free fall velocity equation in C++?

In C++, you can use the cin function to prompt the user for input. For example, you can ask the user to enter the acceleration due to gravity and the height of the object, and store those values in variables to use in the formula.

3. Can I use different units for the values in the free fall velocity equation in C++?

Yes, as long as the units are consistent. For example, if you use meters for the height and meters per second squared for the acceleration due to gravity, the resulting velocity will be in meters per second.

4. How can I incorporate error handling in my C++ free fall velocity program?

You can use conditional statements, such as if and else, to check for any potential errors in the input values or calculations. You can also use try and catch blocks to handle any exceptions that may occur during the program's execution.

5. Are there any built-in functions or libraries in C++ that can help with free fall velocity calculations?

Yes, the cmath library contains a function called sqrt() that can be used to calculate the square root of a number. This can be helpful in calculating the free fall velocity, as the formula requires taking the square root of a value.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
31
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
836
  • Introductory Physics Homework Help
Replies
3
Views
252
  • Programming and Computer Science
Replies
1
Views
895
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
890
  • Calculus and Beyond Homework Help
Replies
10
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
6K
Back
Top