RungeKuttaTest.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: RungeKuttaTest.cpp 1895 2009-05-12 19:34:29Z afarris $"
00002 
00003 //============================================================================
00004 //
00005 //  This file is part of GPSTk, the GPS Toolkit.
00006 //
00007 //  The GPSTk is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU Lesser General Public License as published
00009 //  by the Free Software Foundation; either version 2.1 of the License, or
00010 //  any later version.
00011 //
00012 //  The GPSTk is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU Lesser General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU Lesser General Public
00018 //  License along with GPSTk; if not, write to the Free Software Foundation,
00019 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //  
00021 //  Copyright 2009, The University of Texas at Austin
00022 //
00023 //============================================================================
00024 
00025 // C++ includes
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 // The full, nonlinear equation of motion for a simple pendulum
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; //< the acceleration due to gravity
00062    double L; //< the length of the pendulum (in meters?)
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); // velocity along x 
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; // Initial angle in radians
00079    x0(1,0)  = 0.0;  // Initial angular velocity in radians/second
00080    
00081    PendulumIntegrator pModel(x0,0.);
00082 
00083    double g=9.81, L=1.0;
00084    pModel.setPhysics(g,L); 
00085 
00086       // Formatted for easy reading into Octave (www.octave.org) 
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;  // Step size in seconds for integrator
00092 
00093    double time = 0;
00094    double Nper = 2; // number of periods
00095 
00096       // Output state over approximately Nper periods (exactly for
00097       // small oscillations)
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 }

Generated on Wed Feb 8 03:31:02 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1