view +time/+rk/rungekuttaDiscreteData.m @ 930:34d882bffae4 feature/timesteppers

Add stepping function for RK with discrete data.
author Martin Almquist <malmquist@stanford.edu>
date Mon, 03 Dec 2018 16:26:18 -0800
parents
children
line wrap: on
line source

% Takes one time step of size dt using the rungekutta method
% starting from @arg v.
%
% discreteData contains (a part of) the forcing function, already
% evaluated on the space-time grid.
%
% ODE: dv/dt = F(v,t) + discreteData(:, nt), where nt denotes the current time-point.
%
% coeffs is a struct holding the RK coefficients
% for the specific method.
% Also returns the stage approximations (V) and stage rates (K).
function [v, V, K] = rungekuttaDiscreteData(v, t , dt, F, coeffs, discreteData, n)
    % Compute the intermediate stages k
    K = zeros(length(v), coeffs.s);
    V = zeros(length(v), coeffs.s);
    for i = 1:coeffs.s
        u = v;
        for j = 1:i-1
            u = u + dt*coeffs.a(i,j)*K(:,j);
        end
        V(:,i) = u;
        K(:,i) = F(u,t+coeffs.c(i)*dt);
        K(:,i) = K(:,i) + discreteData(:, n*coeffs.s + i);
    end
    % Compute the updated solution as a linear combination
    % of the intermediate stages.
    for i = 1:coeffs.s
        v = v + dt*coeffs.b(i)*k(:,i);
    end
end