summaryrefslogtreecommitdiff
path: root/60/DEPENDENCIES/rkm2.sce
blob: 633ecf26526a30a27a63e4ab496cbc9e4b61126d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function [u,t] = RK2(u0,t0,tn,h,f)

//  RK2 method solving ODE
//  du/dt = f(u,t), with initial  
//conditions u=u0 at t=t0.  The 
//solution is obtained for t = [t0:h:tn]
//and returned in u

umaxAllowed = 1e+100;

t = [t0:h:tn]; u = zeros(t); n = length(u); u(1) = u0;

for j = 1:n-1
    k1=h*f(t(j),u(j));
    k2=h*f(t(j)+h,u(j)+k1);
    u(j+1) = u(j) + (1/2)*(k1+k2);
   if u(j+1) > umaxAllowed then
           disp('Euler 1 - WARNING: underflow or overflow');
      disp('Solution sought in the following range:');
           disp([t0 h tn]);
      disp('Solution evaluated in the following range:');
      disp([t0 h t(j)]);
           n = j; t = t(1,1:n); u = u(1,1:n);
       break;
 end;
end;