-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfallingball.cpp
More file actions
106 lines (91 loc) · 2.96 KB
/
fallingball.cpp
File metadata and controls
106 lines (91 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//==============================================================
//fallingball.cpp
//Simulates a ball falling through an atmosphere,subject
//to a constant gravitational force downward, and a quadratic
//friction force in a direction opposite to its velocity direction
//===============================================================
#include <iostream>
#include <fstream>
#include <cassert>
#include <cmath>
using namespace std;
void getInput(double & g, double & vTerm, double & v, double & dt, double & yFinal);
void oneStep(double g, double vTerm, double y, double v, double dt, double & yNext, double & vNext);
double accel(double g, double vTerm, double v);
//==========>> main << =======================================
int main()
{
double g; //acceleration due to gravity
double vTerm; //terminal velocity
double v; //velocity
double dt; //time step
double yFinal; //final height of ball
getInput(g, vTerm, v, dt, yFinal);
// is data valid?
assert(yFinal<0.0);
//connect to a file
ofstream out("trajectory.dat");
double y=0.0;
double t=0.0;
while(y > yFinal)
{
out<<t<< " " <<y<< " " <<v<< endl;
oneStep(g, vTerm, y, v, dt, y, v );
t=t+dt;
}
cout<< "Done Output file written" <<endl;
return 0;
}
//========================>> oneStep<<=========================================
//Takes one time step
//Arguments
//g - acceleration of gravity
//vTerm - terminal velocity
//y - current height
//v - current velocity
//dt - time step
//yNext - place to write next height
//vNext - place to write next velocity
//==============================================================================
void oneStep(double g, double vTerm, double y, double v, double dt, double & yNext, double & vNext)
{
//compute velocity at half time step
double vHalf = v+0.5*dt*accel(g, vTerm, v);
//take a time step using data from half time step
yNext=y+dt*vHalf;
vNext=v+dt*accel(g, vTerm, vHalf);
}
//=====================>> accel <<=======================================
//Returns acceleration
//Arguments
// g - acceleration due to gravity
// vTerm - terminal velocity of the object
// v - velocity
//=========================================================================
double accel(double g, double vTerm, double v)
{
return g*(-1.0 - v*v*v/(vTerm*vTerm*fabs(v)));
}
//=============>> getInput <<==============================================
//get input data
//Arguments
//g - acceleration of gravity
//vTerm - terminal velocity
//y - current height
//v - current velocity
//dt - time step size
//yFinal - height at which to stop simulation
//==========================================================================
void getInput(double & g, double & vTerm, double & v, double & dt, double & yFinal)
{
cout<< "Input acceleration due to gravity :> ";
cin>> g;
cout<< "Input terminal velocity:> ";
cin>> vTerm;
cout<< "Input initial velocity:> ";
cin>> v;
cout<< "Input time step:> ";
cin>> dt;
cout<< "Input final height (negative):> ";
cin>> yFinal;
}