- #1
RJLiberator
Gold Member
- 1,095
- 63
Homework Statement
Here's my problem. I am working with ROOT which uses C++ and I am making 3 different graphs. This particular part of the code is for the parabolic motion with density calculation.
The problem is, that I follow the rules in the powerpoint provided to me, the other two of my plots work beautifully (with no air resistance, with air resistance are the other two plots).
This particular code provides me a crash, where it causes my ROOT to shut down.
I have messed with it for the past two hours to find that the error is in the calculation somewhere. It's possible a divide by 0 error, or some other error.
Here's the thing:
When I input fx[0]=0.208
and fy[0]=0.208
then switch the fy[ i ]'s to fy[i+1]'s everywhere, I get the program to run (it does not graph, but it runs).
When I switch the fy[0] and fx[0] components to 0.0208 it no longer runs.
So I have a feeling that there is something critically wrong with my equations, but I can't find it.
Any help looking into my code equations is appreciated.
Homework Equations
The Attempt at a Solution
C:
void cannonefff(float h=10, float vx=10, float vy=25, float time = 0.05)
{
double g = 9.81;
float B = 0.004; // this counts for b/m
float yax = h+(vy*vy)/(2*g);
float root = sqrt(vy*vy+2*h*g);
float t0 = (vy+root)/g;
float xax = t0*vx;
cout<<"The time that the projectile will be in the air is: "<<xax<<endl;
TCanvas *c1 = new TCanvas("c1","Parabolic Motion",200,10,700,500);
c1->SetGrid();
// draw a frame to define the range
TH1F *hr = c1->DrawFrame(-100,-300,xax*1.2,300);
hr->SetTitle("Parabolic Motion");
hr->SetXTitle("Time [s]");
hr->SetYTitle("Position [m]");
c1->GetFrame()->SetBorderSize(12);
float x[2000], y[2000], fx[2000], fy[2000], vx[2000], vy[2000];
vx[0]=vx;
vy[0]=vy;
x[0]=0;
y[0]=h;
//float fx[0]=-0.0208
//float fy[0]=-0.0208
float z = 10000;
float v;
Int_t n=2000;
for(Int_t i=0; i<n-1; i++);
{
v = sqrt(vx[ i ]*vx[ i ]+vy[i]*vy[ i ]);
fx[i+1] = -exp(-y[ i ]/z)*B*v*vx[ i ];
fy[i+1] = -exp(-y[ i ]/z)*B*v*vy[ i ];
x[i+1]=x[ i ]+vx[ i ]*time;
vx[i+1] = vx[ i ]+fx[i+1]*time;
y[i+1]=y[ i ]+vy[ i ]*time;
vy[i+1]=vy[ i ]-g*time+fy[i+1]*time;
}
Last edited by a moderator: