Hello. I am just learning C++ and I am having troubles solving this problem statement:
Your program should start by reading in the values of the initial pointing altitude and muzzle velocity of the launcher: h0, v0, and a launch angle theta (in deg), then set the initial conditions for the model as h(0) = h0, w(0) = v0*sin(theta), x(0) = 0, and v(0) = v0*cos(theta). Have your program also read in a value for the drag constant c so that you can explore the effect of different projectile mass/shape properties. Let's assume dt = 0.1 in the simulation, and let's simulate forward for 1800 time steps (equivalent to 3 minutes simulated fight time). Thus h(180), w(180), x(180), and v(180) would be the last data calculated, corresponding to h[1800], w[1800], x[1800], and v[1800] in your C++ arrays.
After filling the arrays with the data generated from the time step propagation, your program should request a time range, then display on the screen the altitude h(t) and down range distance x(t) for any values of t in the indicated range computed by your iterative calculation. Lastly, you program should display the time, down range position, and impact speed ( sqrt(w(th)^2 + v(th)^2 ) which occur when the projectile hits the ground. Here, th is the time just before the projectile hits the ground.
We can write the time step propagation equations in C++ as:
h[k+1] = h[k] + dt*w[k];
w[k+1] = w[k] + dt*((-c * fabs(w[k]*w[k])) - 9.81);
x[k+1] = x[k] + dt*v[k];
v[k+1] = v[k] + dt*((-c * fabs(v[k]*v[k])) - 9.81);
In my code, I am having trouble printing out the altitude and down range positions for the specified range and also the impact values. My code is listed below.
Your help is greatly appreciated.
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
|
#include <iostream>
#include <cmath>
using namespace std;
int main(void) {
float h0, v0, theta, theta2, c, tmin, tmax, dt, k;
float h[1801], w[1801], x[1801], v[1801];
cout << "Enter h0, v0, and theta: ";
cin >> h0 >> v0 >> theta;
cout << "Enter drag constant c: ";
cin >> c;
cout << "Enter time range for display: ";
cin >> tmin >> tmax;
// Convert to radians
theta2=theta*(acos(-1)/180);
// Initial conditions
h[0]=h0, w[0]=v0*sin(theta2), x[0]=0, v[0]=v0*cos(theta2), dt=0.1;
for (int k=0; k<1800; k++) {
h[k+1] = h[k] + dt*w[k];
w[k+1] = w[k] + dt*((-c*fabs(w[k])*w[k])-9.81);
x[k+1] = x[k] + dt*v[k];
v[k+1] = v[k] + dt*((-c*fabs(v[k])*v[k])-9.81);
}
// Altitude and downrange position for specified range
for (int k=tmin; k>tmax; k++) {
cout << "At t=" << k << endl;
cout << " h=" << h[k] << endl;
cout << " x=" << x[k] << endl;
}
//Impact
if (h[k]<=0) {
cout << "Impact occured at t=" << k;
cout << "with distance x=" << x[k];
cout << "and velocity v=" << v[k] << endl;
}
return 0;
}
|