Mercurial > repos > public > sbplib
comparison +time/+rk/rungekutta.m @ 917:878652b22157 feature/timesteppers
Make time.rk.rungekutta return stage approximations and stage rates in addition to the solution after one time step.
author | Martin Almquist <malmquist@stanford.edu> |
---|---|
date | Mon, 26 Nov 2018 16:12:27 -0800 |
parents | 8732d6bd9890 |
children |
comparison
equal
deleted
inserted
replaced
916:d1c1615bd1a5 | 917:878652b22157 |
---|---|
1 % Takes one time step of size dt using the rungekutta method | 1 % Takes one time step of size dt using the rungekutta method |
2 % starting from @arg v and where the function F(v,t) gives the | 2 % starting from @arg v and where the function F(v,t) gives the |
3 % time derivatives. coeffs is a struct holding the RK coefficients | 3 % time derivatives. coeffs is a struct holding the RK coefficients |
4 % for the specific method. | 4 % for the specific method. |
5 function v = rungekutta(v, t , dt, F, coeffs) | 5 % Also returns the stage approximations (V) and stage rates (K). |
6 function [v, V, K] = rungekutta(v, t , dt, F, coeffs) | |
6 % Compute the intermediate stages k | 7 % Compute the intermediate stages k |
7 k = zeros(length(v), coeffs.s); | 8 K = zeros(length(v), coeffs.s); |
9 V = zeros(length(v), coeffs.s); | |
8 for i = 1:coeffs.s | 10 for i = 1:coeffs.s |
9 u = v; | 11 u = v; |
10 for j = 1:i-1 | 12 for j = 1:i-1 |
11 u = u + dt*coeffs.a(i,j)*k(:,j); | 13 u = u + dt*coeffs.a(i,j)*K(:,j); |
12 end | 14 end |
13 k(:,i) = F(u,t+coeffs.c(i)*dt); | 15 V(:,i) = u; |
16 K(:,i) = F(u,t+coeffs.c(i)*dt); | |
14 end | 17 end |
15 % Compute the updated solution as a linear combination | 18 % Compute the updated solution as a linear combination |
16 % of the intermediate stages. | 19 % of the intermediate stages. |
17 for i = 1:coeffs.s | 20 for i = 1:coeffs.s |
18 v = v + dt*coeffs.b(i)*k(:,i); | 21 v = v + dt*coeffs.b(i)*k(:,i); |