Mercurial > repos > public > sbplib
changeset 1114:f2988a63c3aa feature/timesteppers
Add tests for time.Rungekutta4 and time.Rungekutta4SecondOrder
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 10 Apr 2019 23:10:13 +0200 |
parents | 47e86b5270ad |
children | |
files | +time/+rk/Test.m +time/Rungekutta4SecondOrder.m +time/Test.m |
diffstat | 3 files changed, 58 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+time/+rk/Test.m Wed Apr 10 23:10:13 2019 +0200 @@ -0,0 +1,11 @@ +function tests = Test() + tests = functiontests(localfunctions); +end + +function testRkExplicit(tc) + tc.verifyFail(); +end + +function testRkExplicitSecondOrder(tc) + tc.verifyFail(); +end
--- a/+time/Rungekutta4SecondOrder.m Wed Apr 10 22:40:55 2019 +0200 +++ b/+time/Rungekutta4SecondOrder.m Wed Apr 10 23:10:13 2019 +0200 @@ -22,7 +22,7 @@ end function [v,t] = getV(obj) - v = obj.v + v = obj.v; t = obj.t; end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+time/Test.m Wed Apr 10 23:10:13 2019 +0200 @@ -0,0 +1,46 @@ +function tests = Test() + tests = functiontests(localfunctions); +end + +function testRungekutta4(tc) + F = @(t,y) -0.5*y; + dt = 0.01; + t0 = 0; + y0 = 1; + T = 2; + + ts1 = time.Rungekutta4(F, dt, t0, y0); + ts1.stepN(T/dt); + + ts2 = time.Rungekutta4(F, dt/2, t0, y0); + ts2.stepN(T/(dt/2)); + + y_true = exp(-0.5*T); + + e1 = y_true - ts1.getV(); + e2 = y_true - ts2.getV(); + + tc.verifyEqual(e1/e2, 2^4, 'AbsTol', 0.5); +end + +function testRungekutta4SecondOrder(tc) + F = @(t,y,y_t) -4*y - 1/4*y_t; + dt = 0.01; + t0 = 0; + y0 = 1; + y0t = 1/2; + T = 2; + + ts1 = time.Rungekutta4SecondOrder(F, dt, t0, y0, y0t); + ts1.stepN(T/dt); + + ts2 = time.Rungekutta4SecondOrder(F, dt/2, t0, y0, y0t); + ts2.stepN(T/(dt/2)); + + y_true = 1/51*exp(-T/8)*(sqrt(255)*sin(sqrt(255)/8*T) + 51*cos(sqrt(255)/8*T)); + + e1 = y_true - ts1.getV(); + e2 = y_true - ts2.getV(); + + tc.verifyEqual(e1/e2, 2^4, 'AbsTol', 0.5); +end