From 7656385ea1d3f0a374a8146430bc63cf02e66d6b Mon Sep 17 00:00:00 2001 From: jgoppert Date: Thu, 12 Nov 2015 10:19:30 -0500 Subject: [PATCH] Changed rk4 signature. --- matrix/integration.hpp | 18 +++++++++--------- test/integration.cpp | 13 +++++++------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/matrix/integration.hpp b/matrix/integration.hpp index 2582819078..1979bd8753 100644 --- a/matrix/integration.hpp +++ b/matrix/integration.hpp @@ -7,19 +7,19 @@ namespace matrix { template int integrate_rk4( Vector (*f)(Type, Vector), - Vector & y, - Type & t, - Type h + const Vector & y0, + Type t0, + Type h, + Vector & y1 ) { // https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods Vector 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; + k1 = f(t0, y0); + k2 = f(t0 + h/2, y0 + k1*h/2); + k3 = f(t0 + h/2, y0 + k2*h/2); + k4 = f(t0 + h, y0 + k3*h); + y1 = y0 + (k1 + k2*2 + k3*2 + k4)*(h/6); return 0; } diff --git a/test/integration.cpp b/test/integration.cpp index c1dea3b81d..cdb8eb2466 100644 --- a/test/integration.cpp +++ b/test/integration.cpp @@ -8,9 +8,10 @@ using namespace matrix; // instantiate template to ensure coverage check template int integrate_rk4( Vector (*f)(float, Vector), - Vector & y, - float & t, - float h + const Vector & y0, + float t0, + float h, + Vector & y1 ); Vector f(float t, Vector y); @@ -22,12 +23,12 @@ Vector f(float t, Vector y) { int main() { Vector y = ones(); - float h = 1; float t = 1; + float h = 0.1f; y.T().print(); - integrate_rk4(f, y, t, h); + integrate_rk4(f, y, t, h, y); y.T().print(); - assert(y == (ones()*2)); + assert(y == (ones()*1.1f)); return 0; }