11 changed files with 112 additions and 12 deletions
@ -0,0 +1,26 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include "math.hpp" |
||||||
|
|
||||||
|
namespace matrix { |
||||||
|
|
||||||
|
template<typename Type, size_t M> |
||||||
|
int integrate_rk4( |
||||||
|
Vector<Type, M> (*f)(Type, Vector<Type, M>), |
||||||
|
Vector<Type, M> & y, |
||||||
|
Type & t, |
||||||
|
Type h |
||||||
|
) |
||||||
|
{ |
||||||
|
// https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
|
||||||
|
Vector<Type, M> k1, k2, k3, k4; |
||||||
|
k1 = f(t, y); |
||||||
|
k2 = f(t + h/2, y + k1*h/2); |
||||||
|
k3 = f(t + h/2, y + k2*h/2); |
||||||
|
k4 = f(t + h, y + k3*h); |
||||||
|
y += (k1 + k2*2 + k3*2 + k4)*(h/6); |
||||||
|
t += h; |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
}; // namespace matrix
|
@ -0,0 +1,26 @@ |
|||||||
|
#include <assert.h> |
||||||
|
#include <stdio.h> |
||||||
|
|
||||||
|
#include <matrix/integration.hpp> |
||||||
|
|
||||||
|
using namespace matrix; |
||||||
|
|
||||||
|
Vector<float, 6> f(float t, Vector<float, 6> y); |
||||||
|
|
||||||
|
Vector<float, 6> f(float t, Vector<float, 6> y) { |
||||||
|
return ones<float, 6, 1>(); |
||||||
|
} |
||||||
|
|
||||||
|
int main() |
||||||
|
{ |
||||||
|
Vector<float, 6> y = ones<float, 6, 1>(); |
||||||
|
float h = 1; |
||||||
|
float t = 1; |
||||||
|
y.T().print(); |
||||||
|
integrate_rk4(f, y, t, h); |
||||||
|
y.T().print(); |
||||||
|
assert(y == (ones<float, 6, 1>()*2)); |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
/* vim: set et fenc=utf-8 ff=unix sts=0 sw=4 ts=4 : */ |
Loading…
Reference in new issue