Mercurial > repos > public > sbplib
annotate +time/+rk/rungekutta.m @ 985:ad6de007e7f6 feature/timesteppers
Convert Rungekutta4SecondOrder to take F(t,v,v_t) instead of matrices
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 08 Jan 2019 12:34:29 +0100 |
parents | 878652b22157 |
children |
rev | line source |
---|---|
888
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
1 % Takes one time step of size dt using the rungekutta method |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
2 % starting from @arg v and where the function F(v,t) gives the |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
3 % time derivatives. coeffs is a struct holding the RK coefficients |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
4 % for the specific method. |
917
878652b22157
Make time.rk.rungekutta return stage approximations and stage rates in addition to the solution after one time step.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
5 % Also returns the stage approximations (V) and stage rates (K). |
878652b22157
Make time.rk.rungekutta return stage approximations and stage rates in addition to the solution after one time step.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
6 function [v, V, K] = rungekutta(v, t , dt, F, coeffs) |
888
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
7 % Compute the intermediate stages k |
917
878652b22157
Make time.rk.rungekutta return stage approximations and stage rates in addition to the solution after one time step.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
8 K = zeros(length(v), coeffs.s); |
878652b22157
Make time.rk.rungekutta return stage approximations and stage rates in addition to the solution after one time step.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
9 V = zeros(length(v), coeffs.s); |
888
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
10 for i = 1:coeffs.s |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
11 u = v; |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
12 for j = 1:i-1 |
917
878652b22157
Make time.rk.rungekutta return stage approximations and stage rates in addition to the solution after one time step.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
13 u = u + dt*coeffs.a(i,j)*K(:,j); |
888
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
14 end |
917
878652b22157
Make time.rk.rungekutta return stage approximations and stage rates in addition to the solution after one time step.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
15 V(:,i) = u; |
878652b22157
Make time.rk.rungekutta return stage approximations and stage rates in addition to the solution after one time step.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
16 K(:,i) = F(u,t+coeffs.c(i)*dt); |
888
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
17 end |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
18 % Compute the updated solution as a linear combination |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
19 % of the intermediate stages. |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
20 for i = 1:coeffs.s |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
21 v = v + dt*coeffs.b(i)*k(:,i); |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
22 end |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
23 end |