I cant figure out how to make a function that correctly solves the max height of a parabola

I know height = distance * tan(angle) - (gravity * distance^2) /2 *( speed * cos(angle))^2

I try to put this in and I get like 1.2 trillion

I think im doing it right....

anyone mind helping me figure this out?

Distance = 3831.57
Angle = 35
Gravity = 9.81
Speed = 200

after inputing these in the the formula the anwser is suppose to be like 670 but I dont know how to do it let alone put it in c++

## All 18 Replies

I know height = distance * tan(angle) - (gravity * distance^2) /2 *( speed * cos(angle))^2

Did you remember to use parentheses?

height = (distance*(tan(angle)))-((gravity*(distance*distance))/(2*((speed*(cos(angle)))*(speed*(cos(angle))))))

Better to use too many parentheses then too few

``````#include<cmath>
height = (distance * tan(angle)) - (gravity * pow(distance, 2)) /2  * pow(speed * cos(angle), 2);``````

That is the formula I was using Clinton Portis and it still came out to 1.7 x 10 to the 13th

and Skeen going back and making sure I had all the parentheses got me a lot closer but still about 8000 away from the anwser

... I don't know what is going on :(

... I don't know what is going on :

Wild guess, you forgot that <cmath> is using radians for tan(), cos(), ect?

Haha, I wish it was that simple of a fix... Do you think I need to post my whole program up here?

That is the formula I was using Clinton Portis and it still came out to 1.7 x 10 to the 13th

This is what you posted:

``I know height = distance * tan(angle) - (gravity * distance^2) /2 *( speed * cos(angle))^2``

which is not the same as this:

``````#include<cmath>
height = (distance * tan(angle)) - (gravity * pow(distance, 2)) /2  * pow(speed * cos(angle), 2);``````

Haha, I wish it was that simple of a fix... Do you think I need to post my whole program up here?

You could try, donno how bad the program is?

Well this is what I have... It compiles and everything just somthing is funky when trying to find out the max height :(

``````#include <iostream>
#include <cmath>
using namespace std;

// This program is to  calculate the trajectories of projectiles and how the initial speed and
// angle from the horizontal affect the distance the projectile will travel on different planets
// and satellites.
//
//********************************************************************
//
//Example interaction with the user:

//Trajectory Plotter!
//Enter a speed of launch (mps): 200
//Enter the angle of launch (in degrees): 35
//Speed = 200.00 mps, Angle = 35.00 degrees = 0.61 radians

//Distance travelled on Earth = 3831.57 m,  max height = 670.72 m
//Distance travelled on Mars = 9970.21 m,  max height = 1745.30 m
//Distance travelled on the Moon = 23202.29 m,  max height = 4061.60 m

//Maximum distance traveled = 23202.29
//Maximum height reached = 4061.60
//For Earth, the 5 points of the parabola would be:
// x0, y0 (0.00, 0.00)
// x1, y1 (957.89, 503.04)
// x2, y2 (1915.79, 670.72)
// x3, y3 (2873.68, 503.04)
// x4, y4 (3831.57, 0.00)
//
//********************************************************************

double angtorad (double angle, double pi);
double distance_earth (double angle, int mps, double earth_gravity);
double distance_moon (double angle, int mps, double moon_gravity);
double distance_mars (double angle, int mps, double mars_gravity);
double height_earth (double radian, int mps, double earth_gravity, double earthx);
double height_mars (double radian, int mps, double mars_gravity, double marsx);
double height_moon (double radian, int mps, double moon_gravity, double moonx);
int main ()
{
int mps;
double angle;

cout << " ***  Program 3 Planetary Trajectory Simulator  *** " << endl;
cout << endl;
cout << " Enter the speed of launch (mps)	";
cin >> mps;
cout << " Enter the angle of launch (degrees)	"	;
cin >> angle;
cout << endl;

double pi =  3.1415927;
double radian = pi * ( angle / 180);
double earth_gravity = 9.81;
double moon_gravity = 1.62;
double mars_gravity = 3.69;

cout << " You have entered " << mps << " mps for the speed and " << angle << " degrees for the angle." << endl << endl;
cout << " " << angle << " degrees is equal to " << angtorad(angle, pi) << " radian(s)." << endl << endl << endl;
cout << " The distance traveled on Earth = " << distance_earth (angtorad(angle,pi),mps,earth_gravity) << " meters --";
cout << " Max Height = " << height_earth (angtorad(angle,pi), mps, earth_gravity, earthx) << endl;
cout << " The distance traveled on Mars = " << distance_mars (angtorad(angle,pi),mps,mars_gravity) << " meters -- ";
cout << " Max Height = " << height_mars (angtorad(angle,pi), mps, mars_gravity, marsx) << endl;
cout << " The distance traveled on The Moon = " << distance_moon (angtorad(angle,pi),mps,moon_gravity)<< " meters ";
cout << " Max Height = " << height_moon (angtorad(angle,pi), mps, moon_gravity, moonx) << endl;

return 0;
}
double angtorad (double angle, double pi)
{
radian =  pi * (angle / 180);
}
double distance_earth (double angle, int mps, double earth_gravity)
{
double earth_distance;
earth_distance =  sin(angle + angle)*(mps*mps) / earth_gravity;
return earth_distance;
}
double distance_moon (double angle, int mps, double moon_gravity)
{
double moon_distance;
moon_distance =  sin(angle + angle)*(mps*mps) / moon_gravity;
return moon_distance;
}
double distance_mars (double angle, int mps, double mars_gravity)
{
double mars_distance;
mars_distance = sin(angle + angle)*(mps*mps) / mars_gravity;
return mars_distance;
}
double height_earth (double radian, int mps, double earth_gravity, double earthx)
{
double max_height_earth;
return max_height_earth;
}
double height_mars (double radian, int mps, double mars_gravity, double marsx)
{
double max_height_mars;
return max_height_mars;
}
double height_moon (double radian, int mps, double moon_gravity, double moonx)
{
double max_height_moon;
return max_height_moon;``````

Well I'm getting Height = 0.000224
No matter what I'm doing (using the numbers you gave up in the start).

You said the output were supposed to be around 670, how do you know that? - Just checking

From the example problem that My teacher gave me

I put it in the souce code as comment close to the top

I'm getting 373.402

Using:

``````Solve [0 == -9.82*(-(Sqrt[Sin[35]^2*200^2 - 2*9.82*(x)] -
Sin[35]*200)/9.82) + Sin[35]*200, x]``````

http://www.cs.uky.edu/~keen/115/programs/3-new-pgm.html

Why wouldn't you just build the project in OpenGL? - And like plot in the a lot of points? - Maybe even making the program calculate while drawing using parallel programming? - That would create a sweet effect

EDIT: You could find the top point using like derivative calculations as it's given, that in the toppoint of "y", there will be no slope.

Okay I'm getting 670 now, basically then the problem isn't the fomula, it's the number for "distance" you're using in it, you should be using; "1915.79" which is where the peak in height is, instead of "3831.57" which is where the impact is.

So I think that you should make a derivative calculation based upon the function with (d/d(distance))(function), and solve with 0=(d/d(distance))(function), then use the result in "d" in the formula to find the height, just tested it, and it works in my program :)

EDIT: instead of programming a derivative function you could just make a simple test algoritm, like;

``````double Distance=0;
double Y-values[1000];
double Impact=(ImpactPointCalculatedEarlier);
while (Distance<Impact)
{
Y-values[Distance]=function(Distance);
if (Y-values[Distance]>Y-values[Distance-1]
{
distance++;
}
else
{
distance-=0.01
}
}``````

Something like that. (wont be as effecient as the derivative function at all, but will work)

Just plotted the Graphs and like, the highest point in height is right in the middle off all of them, so like, you could simply use this;

``````double mars_gravity = 3.77;  // Fixed value, you used the wrong one.
double earthx = distance_earth (angtorad(angle,pi),mps,earth_gravity)/2; // Divided by 2 (midpoint)
double marsx = distance_mars (angtorad(angle,pi),mps,mars_gravity)/2; // Divided by 2 (midpoint)
double moonx = distance_moon (angtorad(angle,pi),mps,moon_gravity)/2; // Divided by 2 (midpoint)``````

I'm not sure that the point right in the middle would apply with a different speed and angle, so you'll need to do some testing, or simply make the derivative function as described above.

You my friend are a genius, I get it now...

Or atleast I think so lol

You my friend are a genius, I get it now...

Or atleast I think so lol

About the Graphical, you could use somethin like this:

``````#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <cmath>
#include <windows.h>

#define MAXIMUM 25000

GLdouble yEarth[MAXIMUM];
GLdouble yMars[MAXIMUM];
GLdouble yMoon[MAXIMUM];

GLint InitGL(GLvoid)									// All Setup For OpenGL Goes Here
{
glClearColor(1.0f, 1.0f, 1.0f, 0.5f);				// Black Background
glClearDepth(1.0f);									// Depth Buffer Setup
glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
return TRUE;										// Initialization Went OK
}

GLvoid resize(GLint width, GLint height)		        // Resize And Initialize The GL Window
{
if (height==0)										// Prevent A Divide By Zero By
{
height=1;										// Making Height Equal One
}

glViewport(0,0,width,height);						// Reset The Current Viewport

glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
glLoadIdentity();									// Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,MAXIMUM);
glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
glLoadIdentity();									// Reset The Modelview Matrix
}

double HeightOfY(double distance, double angle, double gravity, double speed)
{
double height=0;
const double
Pi = 3.141592653589793238462643383279502884197169399375105820974944592,
Conversion = Pi/180;
double Temp0 = distance * tan(angle*Conversion);
double Temp1 = gravity * (distance*distance);
double Temp2 = 2*(((speed*cos(angle*Conversion))*(speed*cos(angle*Conversion))));
height = Temp0-(Temp1/Temp2);
return height;
}

GLvoid FPS(GLvoid)
{
static GLint FramesPerSecond = 0;         // Create a var, to hold the frame per second
static GLfloat lastTime = 0.0f;           // Create a var, to hold the last time (ticks from last second)
static char strFrameRate[256] = {0};      // Create a string, needed to post the new title
GLfloat currentTime = GetTickCount() * 0.001f;	// Get the current time, in ticks
++FramesPerSecond;                        // Raise framerate in the static var
if ((currentTime - lastTime) > 1.0f)      // When a second has elasped
{
lastTime = currentTime;                 // Base on new time
sprintf(strFrameRate, "Space (running at: FPS: %d)", FramesPerSecond);    // Update the string
glutSetWindowTitle(strFrameRate);   // Update window title
FramesPerSecond = 0;                    // Start counting form 0, FramesPerSecond
}
}

GLvoid display(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(-(MAXIMUM/2),-(MAXIMUM/5),-MAXIMUM);
glBegin (GL_LINES);
int xvalue=0;
glColor3f(1,0,0);
glVertex2f(0, 0);
glVertex2f(MAXIMUM, 0);
glVertex2f(0, MAXIMUM);
glVertex2f(0, 0);
glColor3f(0,0,1);
for (xvalue=1; xvalue<=MAXIMUM && yEarth[xvalue]>0 ; xvalue++)
{
glVertex2f((xvalue-1), yEarth[(xvalue-1)]);
glVertex2f(xvalue, yEarth[xvalue]);
}
glColor3f(0,1,1);
for (xvalue=1; xvalue<=MAXIMUM && yMars[xvalue]>0 ; xvalue++)
{
glVertex2f((xvalue-1), yMars[(xvalue-1)]);
glVertex2f(xvalue, yMars[xvalue]);
}
glColor3f(0,1,0);
for (xvalue=1; xvalue<=MAXIMUM && yMoon[xvalue]>0 ; xvalue++)
{
glVertex2f((xvalue-1), yMoon[(xvalue-1)]);
glVertex2f(xvalue, yMoon[xvalue]);
}
glEnd();
glutSwapBuffers();
}

GLvoid key(unsigned char key, GLint x, GLint y)
{
switch (key)
{
case 27 :
exit(0);
break;
}
glutPostRedisplay();
}

GLvoid idle(GLvoid)
{
FPS();
glutPostRedisplay();
}

GLvoid Initialization(int argc, char *argv[])
{
glutInit(&argc, argv);                   // Initialization of glut
glutInitWindowSize(800,600); // Set WindowSize (based upon the var loaded)
glutInitWindowPosition(0,0);                // Set default window position
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);    // Initialate DisplayMode
}

GLint main(GLint argc, char *argv[])
{
for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yEarth[xuse]=HeightOfY(xuse, 35, 9.81, 200);   // Earth
}

for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yMars[xuse]=HeightOfY(xuse, 35, 3.77, 200);   // Mars
}

for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yMoon[xuse]=HeightOfY(xuse, 35, 1.62, 200);   // The Moon
}

Initialization(argc, argv);
glutCreateWindow("Space");

glutReshapeFunc(resize);            // Check if windows is being resized
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);

glClearColor(1,1,1,1);
glEnable(GL_DEPTH_TEST);            // Enable Depth testing
glDepthFunc(GL_LESS);               // Choose Depth function

glutMainLoop();

return EXIT_SUCCESS;
}``````

This is based upon GLUT, just a little thing I made up in a couple of minuets, basically it calculates 25000points for "y" based upon x'es from 0 to 25000, and then plots those using lines in OpenGL.
The program is VERY inefficient, and you should easily be able to make it more efficient :)

As the program is right now, it simply lots the parable for the Earth (blue), Mars (teal), Moon (green).

With 35degrees angle, and like 200mps.

Hope it's helping you out, let me know if theres something else :)
EDIT: You could simply make a console program using the:

``````for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yEarth[xuse]=HeightOfY(xuse, 35, 9.81, 200);   // Earth
}

for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yMars[xuse]=HeightOfY(xuse, 35, 3.77, 200);   // Mars
}

for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yMoon[xuse]=HeightOfY(xuse, 35, 1.62, 200);   // The Moon
}``````

and then export these values to a text file and let somethin' like GNUPlot handle the plotting :).

There an update GLUT code:

``````...
for (xvalue=1; xvalue<=counter*MAXIMUM/1000 && yEarth[xvalue]>0 ; xvalue++)
{
glVertex2f((xvalue-1), yEarth[(xvalue-1)]);
glVertex2f(xvalue, yEarth[xvalue]);
}
glColor3f(0,1,1);
for (xvalue=1; xvalue<=counter*MAXIMUM/1000 && yMars[xvalue]>0 ; xvalue++)
{
glVertex2f((xvalue-1), yMars[(xvalue-1)]);
glVertex2f(xvalue, yMars[xvalue]);
}
glColor3f(0,1,0);
for (xvalue=1; xvalue<=counter*MAXIMUM/1000 && yMoon[xvalue]>0 ; xvalue++)
{
glVertex2f((xvalue-1), yMoon[(xvalue-1)]);
glVertex2f(xvalue, yMoon[xvalue]);
}
counter++;
Sleep(1);
...``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.