00001 #pragma ident "$Id: RungeKuttaTest.cpp 1895 2009-05-12 19:34:29Z afarris $"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <iostream>
00027 #include <fstream>
00028 #include <iomanip>
00029 #include <math.h>
00030
00031 #include "RungeKutta4.hpp"
00032
00038 using namespace gpstk;
00039 using namespace std;
00040
00041
00042 class PendulumIntegrator : public RungeKutta4
00043 {
00044 public:
00045
00046 PendulumIntegrator(const Matrix<double>& initialState,
00047 double initialTime=0)
00048 : RungeKutta4(initialState, initialTime)
00049 {};
00050
00051 virtual gpstk::Matrix<double>&
00052 derivative(long double time,
00053 const gpstk::Matrix<double>& inState,
00054 gpstk::Matrix<double>& inStateDot);
00055
00056 void setPhysics(double accGrav, double length)
00057 { g = accGrav; L = length; };
00058
00059 private:
00060
00061 double g;
00062 double L;
00063 };
00064
00065 gpstk::Matrix<double>&
00066 PendulumIntegrator::derivative(long double time,
00067 const gpstk::Matrix<double>& inState,
00068 gpstk::Matrix<double>& inStateDot)
00069 {
00070 inStateDot(0,0) = inState(1,0);
00071 inStateDot(1,0) = -g/L * sin(inState(0,0));
00072 return inStateDot;
00073 }
00074
00075 int main ()
00076 {
00077 gpstk::Matrix<double> x0(2,1), truncError(2,1);
00078 x0(0,0) = 0.001;
00079 x0(1,0) = 0.0;
00080
00081 PendulumIntegrator pModel(x0,0.);
00082
00083 double g=9.81, L=1.0;
00084 pModel.setPhysics(g,L);
00085
00086
00087 cout << "# Pendulum motion result" << endl;
00088 cout << "# Columns: Time, Theta, d Theta/ dt, ";
00089 cout << "estimated error in theta, theta dot" << endl;
00090
00091 double deltaT = .01;
00092
00093 double time = 0;
00094 double Nper = 2;
00095
00096
00097
00098
00099 long count = 0;
00100 while (pModel.getTime() < Nper * (2 * 3.14159265/sqrt(g/L)))
00101 {
00102 pModel.integrateTo((count++)*deltaT,truncError);
00103
00104 cout << setprecision(12)
00105 << pModel.getTime() << " "
00106 << pModel.getState()(0,0) << " " << pModel.getState()(1,0) << " "
00107 << truncError(0,0) << " " << truncError(1,0) << endl;
00108 }
00109
00110 return(0);
00111 }