RungeKutta4.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: RungeKutta4.cpp 70 2006-08-01 18:36:21Z ehagen $"
00002 
00003 
00004 
00005 //============================================================================
00006 //
00007 //  This file is part of GPSTk, the GPS Toolkit.
00008 //
00009 //  The GPSTk is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU Lesser General Public License as published
00011 //  by the Free Software Foundation; either version 2.1 of the License, or
00012 //  any later version.
00013 //
00014 //  The GPSTk is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 //  GNU Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with GPSTk; if not, write to the Free Software Foundation,
00021 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //  
00023 //  Copyright 2004, The University of Texas at Austin
00024 //
00025 //============================================================================
00026 
00027 #include "RungeKutta4.hpp"
00028 
00029 /*
00030  * @file RungeKutta4.hpp
00031  * Implementation of a Runge Kutta integrator.
00032  */
00033 
00034 void gpstk::RungeKutta4::integrateTo (double nextTime, 
00035                                       double stepSize) 
00036 {
00037    if (stepSize == 0) 
00038       stepSize = nextTime - currentTime;
00039 
00040    bool done = false;
00041    
00042    while (!done)
00043    {
00044       // Time steps
00045       double ctPlusDeltaT = currentTime + stepSize;
00046       double ctPlusHalfDeltaT = currentTime + (stepSize * .5);
00047 
00048       // k1
00049       k1 = stepSize * derivative(currentTime, currentState, k1);
00050       tempy = currentState + (.5 * k1);
00051    
00052       // k2
00053       k2 = stepSize * derivative(ctPlusHalfDeltaT, tempy, k2);
00054       tempy = currentState + (.5 * k2);
00055    
00056       // k3
00057       k3 = stepSize * derivative(ctPlusHalfDeltaT, tempy, k3);
00058 
00059       // k4
00060       k4 = stepSize * derivative(ctPlusDeltaT, tempy, k4);
00061       currentState += (k1 + 2. * (k2 + k3) + k4) / 6. ;
00062 
00063       // If we are within teps of the goal time, we are done.
00064       if (fabs(currentTime + stepSize - nextTime) < teps) 
00065          done = true;
00066  
00067       // If we are about to overstep, change the stepsize appropriately
00068       // to hit our target final time; 
00069       if ((currentTime + stepSize) > nextTime) 
00070          stepSize = (nextTime - currentTime);
00071 
00072       currentTime += stepSize;
00073    }
00074 
00075    currentTime = nextTime;
00076 }
00077 
00078 void gpstk::RungeKutta4::integrateTo (double nextTime,
00079                                       Matrix<double>& error,
00080                                       double stepSize) 
00081 {
00082    double deltaT = nextTime - currentTime;
00083    
00084       // Save the current state and time for the second step.
00085    double savedTime = currentTime;
00086    gpstk::Matrix<double> savedState = currentState; 
00087 
00088       // First, take the integration using two steps.
00089    integrateTo(currentTime + (deltaT * 0.5), stepSize);
00090    integrateTo(nextTime, stepSize);
00091 
00092       // Save the results.
00093    gpstk::Matrix<double> twoStepState = currentState;
00094 
00095       // Restore the original state.
00096    currentTime = savedTime;
00097    currentState = savedState;
00098    
00099       // Now, take the integration using only one step.
00100    integrateTo(nextTime, stepSize);
00101    gpstk::Matrix<double> oneStepState = currentState;
00102 
00103    error = oneStepState - twoStepState;
00104    
00105    currentState = twoStepState + (twoStepState - oneStepState) / 15.0;
00106 
00107 }
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118 
00119 
00120 

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