view +time/+rk4/rungekutta_4RV.m @ 842:619561e9ec0e feature/burgers1d

Reformulate the RK4 time stages in order to easier see how the residual should be updated.
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Fri, 14 Sep 2018 16:40:19 +0200
parents 008496ca38f3
children f63b99f0729d
line wrap: on
line source

% Takes one time step of size k using the rungekutta method
% starting from v_0 and where the function F(v,t) gives the
% time derivatives.
function v = rungekutta_4RV(v, t , k, F, RV)
    

    v1 = v;
    v2 = v1 + k/2*F(v1,t, RV.getViscosity());
    
    RV.update(v2, v1, k/2);
    v3 = v1 + k/2*F(v2,t+k/2, RV.getViscosity());
    
    RV.update(v3, v1, k/2);
    v4 = v1 + k*F(v3,t+k/2, RV.getViscosity());
    
    RV.update(v4,v1,k);
    k4 = k*F(v4,t+k, RV.getViscosity());

    v_next = 1/6*(-2*v1 + 2*v2 + 4*v3 + 2*v4 + k4);
    RV.update(v_next,v,k);
    v = v_next;
    

    % k1 = F(v, t, RV.getViscosity());
    
    % RV.update(v+0.5*k*k1, v, 0.5*k);
    % k2 = F(v+0.5*k*k1, t+0.5*k, RV.getViscosity());
    
    % RV.update(v+0.5*k*k2, v, 0.5*k);
    % k3 = F(v+0.5*k*k2, t+0.5*k, RV.getViscosity());
    
    % RV.update(v+k*k3, v, k);
    % k4 = F(v+k*k3,t+k, RV.getViscosity());
    
    % RV.update(v + (1/6)*(k1+2*(k2+k3)+k4)*k, v, k);
    % v = v + (1/6)*(k1+2*(k2+k3)+k4)*k;
end