/* * Analysis C */ #include #include #include "PitchBlkOut.h" #define MAXN 10 // used as the max index in alot of the array declarations #define MAX_ITERATIONS 10 double Pressure26(double alt); double Temperature26(double alt); double Celerity26(double temp, double alt); double Gravity26(double alt); double convertMilliBarsToKSM(double millibars); double func(double x[MAXN], int degree, double xVal); void Search26(int MaxIter, double endBoundConditions[], double Pert[], double Tol[]); void OneRun(double X[]); // Runge-Kutta functions void RK4_System(int n, double h, double *tref, double X[], int nsteps); void XP_System(int Neq, double T, double Y[], double DY[]); void output(int k, double T, double Y[]); // Gauss functions void gaussSolve(double a[MAXN][MAXN], double b[MAXN], double x[MAXN], int n); void gauss(double a[MAXN][MAXN], int l[MAXN], int n); void solve(double a[MAXN][MAXN], double b[MAXN], double x[MAXN], int l[MAXN], int n); double max(double a, double b); void header(); struct pair { double x; double y; }; #define MAXSTAGES 10 int numStages; int PCONTROL=0, TEMP_PCONTROL=0; int currentStage; double COT[MAXSTAGES], Thrust[MAXSTAGES], Weight[MAXSTAGES]; double Flow[MAXSTAGES], CrossSection[MAXSTAGES], CD[MAXSTAGES], TI[MAXSTAGES], AZ, PANG, VEND, PEND; double PLAT,PLONG,HEIGHT; double Velocity, Gravity, Pressure, Temperature, Celerity, Mach, Q, AF, Acceleration, Range; double C[3], F[3], G[3]={0.0, 0.0, -1.0}; // double Latitude, Longitude, Altitude, Acceleration, Range; int main() { double iLat, iLong, iHeight; double iThrust[MAXSTAGES], iWeight[MAXSTAGES], iFlow[MAXSTAGES], iCrossSection[MAXSTAGES], iDrag[MAXSTAGES], iCutOff[MAXSTAGES], iStepSize[MAXSTAGES]; double iVertRise, iPitchAngle, iPitchEnd, iLaunchAzimuth; double iDesiredValue[3], iPerturbation[3], iTolerance[3]; int i; header(); // get all the input from stdin printf("PPCONTROL values:\n"); printf("\tNumber of stages: "); scanf("%d", &numStages); printf("\tPrint PPCONTROL: "); scanf("%d", &TEMP_PCONTROL); printf("Launch Point\n"); printf("\tLatitude (degrees): "); scanf("%lf", &iLat); printf("\tLongitude (degrees): "); scanf("%lf", &iLong); printf("\tHeight (meters): "); scanf("%lf", &iHeight); for(i=0; i= 65) { return 0; } else { printf("Error: Pressure(%lf): Can't process invalid altitude.\n", alt); } } // returns temperature in Kalvens. requires altitude in meters. double Temperature26(double alt) { int n=15, i=0; double y=0; struct pair table[15]; double TemperatureAry[2][15] = { {0.0, 5000.0, 10000.0, 15000.0, 20000.0, 25000.0, 30000.0, 35000.0, 40000.0, 45000.0, 50000.0, 55000.0, 60000.0, 65000.0, 70000.0}, {290.0, 240.0, 220.0, 225.0, 230.0, 245.0, 255.0, 265.0, 270.0, 275.0, 270.0, 255.0, 235.0, 215.0, 200.0} }; if ( alt>65000.0 ) { return 180.0; } // since my lagrange function used a different format for data, I have to convert for (i=0; i= 65000.0) { return 1000.0; } else { return ( pow(1.4 * 8314.32 / 28.9644 * temp, 0.5) ); } } // returns acceleration of gravity, not force. requires altitude in meters. double Gravity26(double alt) { return (398622000000000.0/pow(6378178.0+alt,2.0)); } // converts pressure in milibars to Kilograms/square meter. double convertMilliBarsToKSM(double millibars) { return (millibars * 1000.0 * 0.010197162); } // helper function for curve fit, point evaluation. x is array of polynomial coefficients, xVal is X value to be interprated. double func(double x[MAXN], int degree, double xVal) { int i, power; double sum = 0.0; power = degree-1; for (i=0;i n, then fail. if (k>n) { return 0; } // if less than floor(k/2) data points to left or right halfk = k/2; // using an int here is on purpose halfk_top = k - halfk; // count the number of data points above and below x. if (debug) { printf("lagrange_interpolate(): calculating location of x in table.\n"); } for (i=0; i2) { printf("lagrange_interpolate(): points_less_x=%i\tpoints_greater_x=%i.\n", points_less_x, points_greater_x); } // figure out the starting data point for K points // start_point is 0 indexed if (debug) { printf("lagrange_interpolate(): calculating starting data point.\n"); } if ( halfk > points_less_x ) { // then use all of them start_point = 0; } else if ( halfk_top > points_greater_x ) { // then use all of those start_point = n - k; } else { start_point = points_less_x - halfk; } if (debug) { printf("lagrange_interpolate(): starting interpolation.\n"); } for (i=0; i2) { printf("lagrange_interpolate(): i=%i\tj=%i.\n", (start_point+i), (start_point+j)); } // double check demoinator is not zero // even though this should never happen denom = table[start_point+i].x - table[start_point+j].x; if ( denom ) { l = l * ( (x - table[start_point+j].x)/denom ); if (debug>3) { printf("lagrange_interpolate(): calculating l%i\tl=%lf.\n", (i+1), l); } } else { if (debug>2) { printf("lagrange_interpolate(): strange, demoninator in l%i is zero.\n", (i+1)); } } } } ans += table[start_point+i].y * l; if (debug>2) { printf("lagrange_interpolate(): using data point: y%i=%lf.\n", (start_point+i), table[start_point+i].y ); } if (debug>2) { printf("lagrange_interpolate(): calculating y\ty=%lf.\n", ans); } } *y = ans; return 1; } // Gauss functions void gaussSolve(double a[MAXN][MAXN], double b[MAXN], double x[MAXN], int n) { int l[MAXN]; gauss(a, l, n); solve(a, b, x, l, n); } void gauss(double a[MAXN][MAXN], int l[MAXN], int n) { double s[MAXN]; int i,j,k,p; double r,rmax,smax,xmult; //printf("n: %i\n", n); for (i=0; irmax) { rmax = r; j = i; } } p = l[k]; l[k] = l[j]; l[j] = p; for (i=(k+1); i=0; i--) { sum = b[l[i]]; for (j=(i+1); jb) ? (a):(b)); } // End Gauss functions void header() { printf("\nJuan Velasquez NN:26 Numerical Analysis\n\n"); }