Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

src/Utils/RungeKutta.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 Copyright (C) 2001-2  Andrew J. Turner (aturner@engineer.com)
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 *****************************************************************************/
00018 
00019 #ifndef RUNGEKUTTA_H
00020 #define RUNGEKUTTA_H
00021 
00022 #include <matrix/Matrix.h>
00023 using namespace std;
00024 using namespace O_SESSAME;
00025 
00026 inline Vector RungeKuttaSolve(ExtendedKalmanFilter* ekf)
00027 {
00028         // Extract information from EKF instance
00029         Vector initialState = ekf->GetStateVector();
00030         Vector params       = ekf->GetParameterVector();
00031         Vector controls     = ekf->GetControlVector();
00032         double initialTime  = ekf->GetTimeOfEstimate();
00033         double finalTime    = ekf->GetTimeOfMeasurements();
00034         double timeStepSize = ekf->GetPropagationStepSize();
00035 
00036         // Calculate the number of steps for the propagation
00037         int numberOfSteps = 1;
00038         
00039         double h = (finalTime - initialTime) / numberOfSteps;
00040         double t = initialTime;
00041         int numEqs = initialState.getIndexCount();
00042         Vector inputs = initialState;
00043         Vector K1(numEqs);
00044         Vector K2(numEqs);
00045         Vector K3(numEqs);
00046         Vector K4(numEqs);      
00047         Vector output(numEqs);
00048 
00049         output = initialState;
00050         
00051         Vector temp(numEqs);
00052         //int prevVal = 0; produced a warning (MCV)
00053         for (int ii = 1; ii <= numberOfSteps; ++ii)
00054         {       
00055                 K1 = h * ekf->GetStateDot(t, inputs, controls, params);
00056                 temp = inputs + K1 / 2.0;
00057                 K2 = h * ekf->GetStateDot(t + h/2, temp, controls, params);
00058                 temp = inputs + K2 / 2.0;
00059                 K3 = h * ekf->GetStateDot(t + h/2, temp, controls, params);
00060                 temp = inputs + K3;
00061                 K4 = h * ekf->GetStateDot(t + h, temp, controls, params);
00062                 for (int jj = 0; jj < numEqs; ++jj)
00063                 {
00064                         inputs(jj+1) += (K1(jj+1) + 2.0 * K2(jj+1) + 2.0 *K3(jj+1) + K4(jj+1)) / 6.0;                   
00065                 }
00066                 t = initialTime + ii * h;
00067                 output = inputs;        
00068         }
00069         
00070         return output;
00071         
00072 };
00073 
00074 inline Vector RungeKutta(Vector initialState, Vector params, Vector controls, double initialTime, double finalTime, int numberOfSteps, Vector (*ptr_rhs)(double time, Vector states, Vector controls, Vector params))
00075 {
00076         
00077         double h = (finalTime - initialTime) / numberOfSteps;
00078         double t = initialTime;
00079         int numEqs = initialState.getIndexCount();
00080         Vector inputs = initialState;
00081         Vector K1(numEqs);
00082         Vector K2(numEqs);
00083         Vector K3(numEqs);
00084         Vector K4(numEqs);      
00085         Vector output(numEqs);
00086 
00087         output = initialState;
00088         
00089         Vector temp(numEqs);
00090         for (int ii = 1; ii <= numberOfSteps; ++ii)
00091         {       
00092                 K1 = h * ptr_rhs(t, inputs, controls, params);
00093                 temp = inputs + K1 / 2.0;
00094                 K2 = h * ptr_rhs(t + h/2, temp, controls, params);
00095                 temp = inputs + K2 / 2.0;
00096                 K3 = h * ptr_rhs(t + h/2, temp, controls, params);
00097                 temp = inputs + K3;
00098                 K4 = h * ptr_rhs(t + h, temp, controls, params);
00099                 for (int jj = 0; jj < numEqs; ++jj)
00100                 {
00101                         inputs(jj+1) += (K1(jj+1) + 2.0 * K2(jj+1) + 2.0 *K3(jj+1) + K4(jj+1)) / 6.0;                   
00102                 }
00103                 t = initialTime + ii * h;
00104                 output = inputs;        
00105         }
00106 
00107         return output;
00108         
00109 }
00110 
00111 #endif /*RUNGEKUTTA_H*/

Generated on Wed Sep 5 12:54:25 2007 for DSACSS Operational Code by  doxygen 1.3.9.1