GteOdeRungeKutta4.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
11 
12 // The TVector template parameter allows you to create solvers with
13 // Vector<N,Real> when the dimension N is known at compile time or
14 // GVector<Real> when the dimension N is known at run time. Both classes
15 // have 'int GetSize() const' that allow OdeSolver-derived classes to query
16 // for the dimension.
17 
18 namespace gte
19 {
20 
21 template <typename Real, typename TVector>
22 class OdeRungeKutta4 : public OdeSolver<Real,TVector>
23 {
24 public:
25  // Construction and destruction.
26  virtual ~OdeRungeKutta4();
27  OdeRungeKutta4(Real tDelta,
28  std::function<TVector(Real, TVector const&)> const& F);
29 
30  // Estimate x(t + tDelta) from x(t) using dx/dt = F(t,x). You may allow
31  // xIn and xOut to be the same object.
32  virtual void Update(Real tIn, TVector const& xIn, Real& tOut,
33  TVector& xOut);
34 };
35 
36 
37 template <typename Real, typename TVector>
39 {
40 }
41 
42 template <typename Real, typename TVector>
44  std::function<TVector(Real, TVector const&)> const& F)
45  :
46  OdeSolver<Real, TVector>(tDelta, F)
47 {
48 }
49 
50 template <typename Real, typename TVector>
51 void OdeRungeKutta4<Real, TVector>::Update(Real tIn, TVector const& xIn,
52  Real& tOut, TVector& xOut)
53 {
54  // Compute the first step.
55  Real halfTDelta = ((Real)0.5) * this->mTDelta;
56  TVector fTemp1 = this->mFunction(tIn, xIn);
57  TVector xTemp = xIn + halfTDelta * fTemp1;
58 
59  // Compute the second step.
60  Real halfT = tIn + halfTDelta;
61  TVector fTemp2 = this->mFunction(halfT, xTemp);
62  xTemp = xIn + halfTDelta * fTemp2;
63 
64  // Compute the third step.
65  TVector fTemp3 = this->mFunction(halfT, xTemp);
66  xTemp = xIn + this->mTDelta * fTemp3;
67 
68  // Compute the fourth step.
69  Real sixthTDelta = this->mTDelta / (Real)6;
70  tOut = tIn + this->mTDelta;
71  TVector fTemp4 = this->mFunction(tOut, xTemp);
72  xOut = xIn + sixthTDelta * (
73  fTemp1 + ((Real)2)*(fTemp2 + fTemp3) + fTemp4);
74 }
75 
76 
77 }
std::function< TVector(Real, TVector const &)> mFunction
Definition: GteOdeSolver.h:46
OdeRungeKutta4(Real tDelta, std::function< TVector(Real, TVector const &)> const &F)
virtual void Update(Real tIn, TVector const &xIn, Real &tOut, TVector &xOut)


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01