Commit be569e23 by s-jdoyle4

### ball with .145kg

parent 5d511187
ball.cpp 0 → 100644
 // Implemented from pg 61 of book // ball -Program to compute and graph the trajectory of a baseball witha mass of .145 kg using the Euler method #include #include #include #include #include "TMultiGraph.h" void main() { //* Set initial position and velocity of the baseball double y1, speed, theta; cout << "Enter initial height (meters): "; cin >> y1; cout << "Enter initial speed (m/s): "; cin >> speed; cout << "Enter initial angle (degrees): "; cin >> theta; //* Set physical parameters (mass, Cd, etc.) double Cd = 0.35; // Drag coefficient (dimensionless) double area = 4.3e-3; // Cross-sectional area of projectile (m^2) double grav = 9.81; // Gravitational acceleration (m/s^2) double mass = 0.145; // Mass of projectile (kg) double airFlag1, airFlag2, rho; airFlag1=0; airFlag2=1; if( airFlag2 == 1 ){ rho = 1.2; // Density of air (kg/m^3) double r1[2+1], v1[2+1], r[2+1], v[2+1], accel[2+1]; cout << "Enter initial height (meters): "; cin >> y1; r1[1] = 0; r1[2] = y1; // Initial vector position const double pi = 3.141592654; v1[1] = speed*cos(theta*pi/180); // Initial velocity (x) v1[2] = speed*sin(theta*pi/180); // Initial velocity (y) r[1] = r1[1]; r[2] = r1[2]; // Set initial position and velocity v[1] = v1[1]; v[2] = v1[2]; double air_const = -0.5*Cd*rho*area/mass; // Air resistance constant //* Loop until ball hits ground or max steps completed double tau; cout << "Enter timestep, tau (sec): "; cin >> tau; int iStep, maxStep = 1000; // Maximum number of steps double *xplot, *yplot, *xNoAir, *yNoAir; xplot = new double [maxStep+1]; yplot = new double [maxStep+1]; xNoAir = new double [maxStep+1]; yNoAir = new double [maxStep+1]; for( iStep=1; iStep<=maxStep; iStep++ ) { //* Record position (computed and theoretical) for plotting xplot[iStep] = r[1]; // Record trajectory for plot yplot[iStep] = r[2]; double t = (iStep-1)*tau; // Current time xNoAir[iStep] = r1[1] + v1[1]*t; yNoAir[iStep] = r1[2] + v1[2]*t - 0.5*grav*t*t; //* Calculate the acceleration of the ball double normV = sqrt( v[1]*v[1] + v[2]*v[2] ); accel[1] = air_const*normV*v[1]; // Air resistance accel[2] = air_const*normV*v[2]; // Air resistance accel[2] -= grav; // Gravity //* Calculate the new position and velocity using Euler method r[1] += tau*v[1]; // Euler step r[2] += tau*v[2]; v[1] += tau*accel[1]; v[2] += tau*accel[2]; //* If ball reaches ground (y<0), break out of the loop if( r[2] < 0 ) { xplot[iStep+1] = r[1]; // Record last values computed yplot[iStep+1] = r[2]; break; // Break out of the for loop } } //* Print maximum range and time of flight cout << "Maximum range is " << r[1] << " meters" << endl; cout << "Time of flight is " << iStep*tau << " seconds" << endl; //* Print out the plotting variables: // xplot, yplot, xNoAir, yNoAir ofstream xplotOut("xplot.txt"), yplotOut("yplot.txt"); int i; for( i=1; i<=iStep+1; i++ ) { xplotOut << xplot[i] << endl; yplotOut << yplot[i] << endl; } gNoAir = new TGraph(maxStep, xplot, yplot); } if( airFlag1=0 ){ rho = 0; // No air resistance double r1[2+1], v1[2+1], r[2+1], v[2+1], accel[2+1]; cout << "Enter initial height (meters): "; cin >> y1; r1[1] = 0; r1[2] = y1; // Initial vector position const double pi = 3.141592654; v1[1] = speed*cos(theta*pi/180); // Initial velocity (x) v1[2] = speed*sin(theta*pi/180); // Initial velocity (y) r[1] = r1[1]; r[2] = r1[2]; // Set initial position and velocity v[1] = v1[1]; v[2] = v1[2]; //* Loop until ball hits ground or max steps completed double tau; cout << "Enter timestep, tau (sec): "; cin >> tau; int iStep, maxStep = 1000; // Maximum number of steps double *xplot, *yplot, *xNoAir, *yNoAir; xplot = new double [maxStep+1]; yplot = new double [maxStep+1]; xNoAir = new double [maxStep+1]; yNoAir = new double [maxStep+1]; for( iStep=1; iStep<=maxStep; iStep++ ) { //* Record position (computed and theoretical) for plotting xplot[iStep] = r[1]; // Record trajectory for plot yplot[iStep] = r[2]; double t = (iStep-1)*tau; // Current time xNoAir[iStep] = r1[1] + v1[1]*t; yNoAir[iStep] = r1[2] + v1[2]*t - 0.5*grav*t*t; //* Calculate the acceleration of the ball double normV = sqrt( v[1]*v[1] + v[2]*v[2] ); accel[1] = air_const*normV*v[1]; // Air resistance accel[2] = air_const*normV*v[2]; // Air resistance accel[2] -= grav; // Gravity //* Calculate the new position and velocity using Euler method r[1] += tau*v[1]; // Euler step r[2] += tau*v[2]; v[1] += tau*accel[1]; v[2] += tau*accel[2]; //* If ball reaches ground (y<0), break out of the loop if( r[2] < 0 ) { xplot[iStep+1] = r[1]; // Record last values computed yplot[iStep+1] = r[2]; break; // Break out of the for loop } } //* Print maximum range and time of flight cout << "Maximum range is " << r[1] << " meters" << endl; cout << "Time of flight is " << iStep*tau << " seconds" << endl; //* Print out the plotting variables: // xplot, yplot, xNoAir, yNoAir ofstream xplotOut("xplot.txt"), yplotOut("yplot.txt"); yplotOut << yplot[i] << endl; } g2 = new TGraph(maxStep, xplot, yplot); TCanvas *cl = new TCanvas("cl"); cl->SetGrid(); TMultiGraph *mg = new TMultiGraph(); mg->SetTitle("Baseball Trajectory"); g2->SetLineColor(4); mg->Add(g2); mg->Add(gNoAir); mg->Draw("AC"); delete [] xplot, yplot; // Release memory } }
ball1.cpp 0 → 100644
 #include #include #include #include #include "TMultiGraph.h" void main() { //* Set initial position and velocity of the baseball double y1, speed, theta; double r1[2+1], v1[2+1], r[2+1], v[2+1], accel[2+1]; cout << "Enter initial height (meters): "; cin >> y1; cout << "Enter initial speed (m/s): "; cin >> speed; cout << "Enter initial angle (degrees): "; cin >> theta; const double pi = 3.141592654; //* Set physical parameters (mass, Cd, etc.) double Cd = 0.35; // Drag coefficient (dimensionless) double area = 4.3e-3; // Cross-sectional area of projectile (m^2) double grav = 9.81; // Gravitational acceleration (m/s^2) double mass = 0.145; // Mass of projectile (kg) double airFlag, rho; airFlag=0; for( airFlag = 0; airFlag<1; airFlag++ ) { r1[1] = 0; r1[2] = y1; // Initial vector position const double pi = 3.141592654; v1[1] = speed*cos(theta*pi/180); // Initial velocity (x) v1[2] = speed*sin(theta*pi/180); // Initial velocity (y) r[1] = r1[1]; r[2] = r1[2]; // Set initial position and velocity v[1] = v1[1]; v[2] = v1[2]; rho = 0; // No air resistance double air_const = -0.5*Cd*rho*area/mass; // Air resistance constant //* Loop until ball hits ground or max steps completed double tau; cout << "Enter timestep, tau (sec): "; cin >> tau; int iStep, maxStep = 1000; // Maximum number of steps double *xplot, *yplot, *xNoAir, *yNoAir; xplot = new double [maxStep+1]; yplot = new double [maxStep+1]; xNoAir = new double [maxStep+1]; yNoAir = new double [maxStep+1]; for( iStep=1; iStep<=maxStep; iStep++ ) { //* Record position (computed and theoretical) for plotting xplot[iStep] = r[1]; // Record trajectory for plot yplot[iStep] = r[2]; double t = (iStep-1)*tau; // Current time xNoAir[iStep] = r1[1] + v1[1]*t; yNoAir[iStep] = r1[2] + v1[2]*t - 0.5*grav*t*t; //* Calculate the acceleration of the ball double normV = sqrt( v[1]*v[1] + v[2]*v[2] ); accel[1] = air_const*normV*v[1]; // Air resistance accel[2] = air_const*normV*v[2]; // Air resistance accel[2] -= grav; // Gravity //* Calculate the new position and velocity using Euler method r[1] += tau*v[1]; // Euler step r[2] += tau*v[2]; v[1] += tau*accel[1]; v[2] += tau*accel[2]; //* If ball reaches ground (y<0), break out of the loop if( r[2] < 0 ) { xplot[iStep+1] = r[1]; // Record last values computed yplot[iStep+1] = r[2]; break; // Break out of the for loop } } //* Print maximum range and time of flight cout << "Maximum range is " << r[1] << " meters" << endl; cout << "Time of flight is " << iStep*tau << " seconds" << endl; } r1[1] = 0; r1[2] = y1; // Initial vector position const double pi = 3.141592654; v1[1] = speed*cos(theta*pi/180); // Initial velocity (x) v1[2] = speed*sin(theta*pi/180); // Initial velocity (y) r[1] = r1[1]; r[2] = r1[2]; // Set initial position and velocity v[1] = v1[1]; v[2] = v1[2]; if( airFlag = 1 ) { r1[1] = 0; r1[2] = y1; // Initial vector position const double pi = 3.141592654; v1[1] = speed*cos(theta*pi/180); // Initial velocity (x) v1[2] = speed*sin(theta*pi/180); // Initial velocity (y) r[1] = r1[1]; r[2] = r1[2]; // Set initial position and velocity v[1] = v1[1]; v[2] = v1[2]; rho = 1.2; // No air resistance double air_const = -0.5*Cd*rho*area/mass; // Air resistance constant //* Loop until ball hits ground or max steps completed double tau; cout << "Enter timestep, tau (sec): "; cin >> tau; int iStep, maxStep = 1000; // Maximum number of steps double *xplot1, *yplot1, *xNoAir1, *yNoAir1; xplot1 = new double [maxStep+1]; yplot1 = new double [maxStep+1]; xNoAir1 = new double [maxStep+1]; yNoAir1 = new double [maxStep+1]; for( iStep=1; iStep<=maxStep; iStep++ ) { //* Record position (computed and theoretical) for plotting xplot1[iStep] = r[1]; // Record trajectory for plot yplot1[iStep] = r[2]; double t = (iStep-1)*tau; // Current time xNoAir1[iStep] = r1[1] + v1[1]*t; yNoAir1[iStep] = r1[2] + v1[2]*t - 0.5*grav*t*t; //* Calculate the acceleration of the ball double normV = sqrt( v[1]*v[1] + v[2]*v[2] ); accel[1] = air_const*normV*v[1]; // Air resistance accel[2] = air_const*normV*v[2]; // Air resistance accel[2] -= grav; // Gravity //* Calculate the new position and velocity using Euler method r[1] += tau*v[1]; // Euler step r[2] += tau*v[2]; v[1] += tau*accel[1]; v[2] += tau*accel[2]; //* If ball reaches ground (y<0), break out of the loop if( r[2] < 0 ) { xplot1[iStep+1] = r[1]; // Record last values computed yplot1[iStep+1] = r[2]; break; // Break out of the for loop } } //* Print maximum range and time of flight cout << "Maximum range is " << r[1] << " meters" << endl; cout << "Time of flight is " << iStep*tau << " seconds" << endl; } //* Print out the plotting variables: cout << "Maximum range is " << r[1] << " meters" << endl; cout << "Time of flight is " << iStep*tau << " seconds" << endl; } //* Print out the plotting variables: // xplot, yplot, xNoAir, yNoAir, ofstream xplotOut("xplot.txt"), yplotOut("yplot.txt"), xNoAirOut("xNoAir.txt"), yNoAirOut("yNoAir.txt"); int i; for( i=1; i<=iStep+1; i++ ) { xplotOut << xplot[i] << endl; yplotOut << yplot[i] << endl; } for( i=1; i<=iStep; i++ ) { xNoAirOut << xNoAir[i] << endl; yNoAirOut << yNoAir[i] << endl; } int i; for( i=1; i<=iStep+1; i++ ) { xplotOut << xplot1[i] << endl; yplotOut << yplot1[i] << endl; } for( i=1; i<=iStep; i++ ) { xNoAirOut << xNoAir1[i] << endl; yNoAirOut << yNoAir1[i] << endl; } TCanvas *cl = new TCanvas("cl"); cl->SetGrid(); TMultiGraph *mg = new TMultiGraph(); mg->SetTitle("Baseball Trajectory"); TGraph *gNoAir = new TGraph(maxStep+1, xNoAir, yNoAir); gNoAir->SetLineColor(2); TGraph *g2 = new TGraph(maxStep, xplot1, yplot1); mg->Add(g2); mg->Add(gNoAir); mg->Draw("AC"); delete [] xplot, yplot, xNoAir, yNoAir; // Release memory }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!