Hello,
could someone convert this to Pascal (if possible Delphi)?
Thanks in advance
Code:#include <stdio.h> #include <stdlib.h> #include <math.h> #define DIM 4 // number of state quantities #define DT 1e-4 // time interval of computation #define G 9.81 // gravitational acceleration #define C .5 // constant from Newtons formula for drag #define R 20e-3 // radius of ball #define RHO 1.2 // density of air #define M 2.7e-3 // mass of ball #define DRAG (C*M_PI*R*R*RHO/M/2) typedef double tVector[DIM]; typedef void(&tFCEDER)(tVector& out, tVector in); int main(int argc, char** argv); void eulerStep( void(&rovnice)(tVector& out, tVector in), tVector &U, double &t, double dt ); void midPoint( void(&rovnice)(tVector& out, tVector in), tVector &U, double &t, double dt ); void newtowDrag( tVector& dVdt, tVector V); void nodrag( tVector& dVdt, tVector V); double throw(tFCEDER equation, double v, double angle, double dt, bool print); double optimalAngle( tFCEDER drag, double v, double dt); int main(int argc, char** argv) { // part A double v = 10.; for(int i = 2; i < 18; i++){ double u = (i/2)/18.*M_PI; throw( (i%2?nodrag:newtowDrag), v, u, DT, true); printf("\n\n\n"); } // Part B for(double v = 10; v <= 130; v+=20){ double phi_max = optimalAngle(newtowDrag, v, DT); throw(newtowDrag, v, phi_max, DT, true); printf("\n\n\n"); } // Part C for(double v = 1; v <= 200; v++){ double phi_max = optimalAngle(newtowDrag, v, DT); double x_max = throw(newtowDrag, v, phi_max, DT, false); printf("%g %g %g\n", x_max, v, phi_max/M_PI*180.); } return 0; } double optimalAngle( tFCEDER drag, double v, double dt) { double a = 0; double b = M_PI/2; double xA = 0, xB = 0, yA = 0, yB = 0; while(b-a > M_PI/180./60.){ xA = a + (b-a)/3; xB = a + 2*(b-a)/3; yA = throw(drag, v, xA, dt, false); yB = throw(drag, v, xB, dt, false); if(yA > yB) b = xB; else a = xA; } return (a+b)/2; } double throw( tFCEDER equation, double v, double angle, double dt, bool print) { double t = 0; tVector U = { 0.,0.,v*cos(angle), v*sin(angle) }; while(U[1] >= 0){ midPoint(equation, U, t, dt); if(print) printf("%g %g\n", U[0], U[1]); } return U[0]; } void newtowDrag( tVector& dVdt, tVector V) { dVdt[0] = V[2]; dVdt[1] = V[3]; dVdt[2] = -DRAG*V[2]*hypot(V[2],V[3]); dVdt[3] = -DRAG*V[3]*hypot(V[2],V[3])-G; } void nodrag( tVector& dVdt, tVector V) { dVdt[0] = V[2]; dVdt[1] = V[3]; dVdt[2] = 0; dVdt[3] = -G; } void midPoint( tFCEDER equation, tVector &U, double &t, double dt ) { tVector df; equation(df, U); tVector midP; for(int i = 0; i < DIM; i++) midP[i] = U[i] + df[i]*dt/2.; equation(df, midP); for(int i = 0; i < DIM; i++) U[i] += df[i]*dt; } void eulerStep( tFCEDER equation, tVector &U, double &t, double dt ) { tVector df; equation(df, U); for(int i = 0; i < DIM; i++) U[i] += df[i]*dt; t += dt; }
Last edited by WingedPanther; 08-25-2009 at 01:31 PM. Reason: add code tags (the # button)
First observation: that isn't standard C code.
Second observation: it shouldn't be that hard to convert to Pascal/Delphi/Lazarus.
What do you have so far?
Free hint: #defines will turn into a const block.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks