0% found this document useful (0 votes)
71 views

Using Namespace

The document contains 3 code examples that model different physics problems by numerically solving differential equations over time. The first models the volume of a sphere decreasing over time. It prints the volume every 0.25 seconds as it decreases from 63.88 mm^3 to 19.58 mm^3 over 10 seconds. The second models liquid depth changing over time based on inflow and outflow rates, printing the depth every 0.5 seconds as it fluctuates between -0.18 and 1.76 over 10 seconds. The third similarly models liquid depth changing over time based on gravitational acceleration and outflow rate, printing the depth every 0.5 seconds as it increases from 4.91 to 46.37 over 10 seconds.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Using Namespace

The document contains 3 code examples that model different physics problems by numerically solving differential equations over time. The first models the volume of a sphere decreasing over time. It prints the volume every 0.25 seconds as it decreases from 63.88 mm^3 to 19.58 mm^3 over 10 seconds. The second models liquid depth changing over time based on inflow and outflow rates, printing the depth every 0.5 seconds as it fluctuates between -0.18 and 1.76 over 10 seconds. The third similarly models liquid depth changing over time based on gravitational acceleration and outflow rate, printing the depth every 0.5 seconds as it increases from 4.91 to 46.37 over 10 seconds.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Problem# 1.

13
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#define STEP .25 //step size in units of d as defined per problem statement
#define k .08 //area is constant in units of mm/min
#define Pi 3.141592
double volume(double r, double v){
/*
The problem statement gives us the
dV/dt = -k * A
A = 4*Pi*r^2 we can substitute this into our previous equation
dV/dt = - k * 4 * Pi * r^2
*/
v = (-k * 4 * Pi*r * r)*STEP + v;
return v;
}
int main(){
double r = 2.5; //radius initially 2.5 mm
double t = 0; //initialize time at 0
double v = 4 * Pi / 3 * 2.5*2.5*2.5;
cout << "\t Time
cout << fixed << right;
cout << setprecision(2);
while (t <= 10){
v = volume(r, v);

Volume" << endl;

cout << "\t" << setw(5) << t << "seconds


" << v <<" mm
cubed"<< endl;
//Here we must find the now current radius, based on the current volume
approximation.
r = pow(v * 3 / (4 * Pi), 1/3.);
t += STEP;
}
return 0;
}

Time
Volume
0.00seconds
63.88 mm cubed
0.25seconds
62.33 mm cubed
0.50seconds
60.81 mm cubed
0.75seconds
59.32 mm cubed
1.00seconds
57.85 mm cubed
1.25seconds
56.40 mm cubed
1.50seconds
54.98 mm cubed
1.75seconds
53.58 mm cubed
2.00seconds
52.20 mm cubed
2.25seconds
50.85 mm cubed
2.50seconds
49.53 mm cubed
2.75seconds
48.22 mm cubed
3.00seconds
46.94 mm cubed
3.25seconds
45.68 mm cubed
3.50seconds
44.45 mm cubed
3.75seconds
43.23 mm cubed
4.00seconds
42.04 mm cubed
4.25seconds
40.87 mm cubed
4.50seconds
39.72 mm cubed
4.75seconds
38.60 mm cubed
5.00seconds
37.49 mm cubed
5.25seconds
36.41 mm cubed
5.50seconds
35.35 mm cubed
5.75seconds
34.31 mm cubed
6.00seconds
33.28 mm cubed
6.25seconds
32.28 mm cubed
6.50seconds
31.30 mm cubed
6.75seconds
30.34 mm cubed
7.00seconds
29.40 mm cubed
7.25seconds
28.48 mm cubed
7.50seconds
27.58 mm cubed
7.75seconds
26.69 mm cubed
8.00seconds
25.83 mm cubed
8.25seconds
24.99 mm cubed
8.50seconds
24.16 mm cubed
8.75seconds
23.35 mm cubed
9.00seconds
22.56 mm cubed
9.25seconds
21.79 mm cubed
9.50seconds
21.03 mm cubed
9.75seconds
20.30 mm cubed
10.00seconds
19.58 mm cubed
Program ended with exit code: 0

Problem# 1.9
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#define STEP .5 //step size in units of d as defined per problem statement
#define A 1250 //area is constant in units of m^2
#define Q 450 // Liquid is drawn at a constant rate in units of m^3/d
double depth(double t, double y){
double d;
/*
The equation given to use is equation 1.13: d(yA)/dt = 3Q*sin^2(t) - Q/A.
Since A is a constant, we divide both sides by A, yielding the equation we
will use:
dy/dt = 3(Q/A)*sin^2(t) - Q/A
*/
d = (3 *sin(t)*sin(t) - 1) * Q / A *STEP + y;
return d;
}
int main(){
double y = 0; //depth initially 0
double t = 0; //initialize time at 0
cout << "Time
Height" << endl;
cout << fixed;
cout << setprecision(2);
while (t <= 10){
y = depth(t, y);
cout << setw(3) << t << "
t += STEP;

Time
0.00
0.50

Height
-0.18
-0.24

" << y << endl;

1.00
1.50
2.00
2.50
3.00
3.50
4.00
4.50
5.00
5.50
6.00
6.50
7.00
7.50
8.00
8.50
9.00
9.50
10.00
Program

-0.03
0.32
0.59
0.60
0.43
0.32
0.45
0.79
1.10
1.19
1.05
0.90
0.95
1.25
1.60
1.76
1.67
1.49
1.47
ended with exit code: 0

Problem# 1.2

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;


#define STEP .5 //step size in units of d as defined per problem statement
#define g 9.81 //area is constant in units of m^2
#define c 12.5 // Liquid is drawn at a constant rate in units of m^3/d
#define m 68.1
double depth(double t, double y){
double d;
/*
The equation given to use is equation 1.13: d(yA)/dt = 3Q*sin^2(t) - Q/A.
Since A is a constant, we divide both sides by A, yielding the equation we
will use:
dy/dt = 3(Q/A)*sin^2(t) - Q/A
*/
d = (g-c*y/m) *STEP + y;
return d;
}
int main(){
double y = 0; //depth initially 0
double t = 0; //initialize time at 0
cout << "Time
Height" << endl;
cout << fixed;
cout << setprecision(2);
while (t <= 10){
y = depth(t, y);
cout << setw(3) << t << "
t += STEP;

" << y << endl;

}
cout<<"step size "<< STEP<<" seconds"<<endl;

return 0;
}
Time
0.00
0.50
1.00
1.50

Height
4.91
9.36
13.41
17.08

2.00
20.42
2.50
23.45
3.00
26.20
3.50
28.70
4.00
30.97
4.50
33.04
5.00
34.91
5.50
36.61
6.00
38.15
6.50
39.56
7.00
40.83
7.50
41.99
8.00
43.04
8.50
44.00
9.00
44.86
9.50
45.65
10.00
46.37
step size 0.50 seconds

Time
Height
0.00
9.81
1.00
17.82
2.00
24.36
3.00
29.70
4.00
34.06
5.00
37.62
6.00
40.52
7.00
42.89
8.00
44.83
9.00
46.41
10.00
47.70
step size 1 seconds

You might also like