Anonymous user
Runge-Kutta method: Difference between revisions
→{{header|Octave}}
Line 1,593:
=={{header|Octave}}==
<lang octave>
#Applying the Runge-Kutta method (This code must be implement on a different file than the main one).
function temp = rk4(func,x,pvi,h)
K1 = h*func(x,pvi);
Line 1,614 ⟶ 1,603:
temp = pvi + (K1 + 2*K2 + 2*K3 + K4)/6;
endfunction
#Main Program.
f = @(t) (1/16)*((t.^2 + 4).^2);
Line 1,621 ⟶ 1,612:
h = 0.1;
Yn = pvi;
for x = 0:h:10-h
pvi = rk4(df,x,pvi,h);
Yn = [Yn pvi];
endfor
end</lang>▼
fprintf('Time \t Exact Value \t ODE4 Value \t Num. Error\n');
{{out}}▼
<pre>ans =▼
for i=0:10
0.00000 1.00000 0.00000▼
fprintf('%d \t %.5f \t %.5f \t %.4g \n',i,f(i),Yn(1+i*10),f(i)-Yn(1+i*10));
1.00000 1.56250 0.00000▼
endfor
2.00000 4.00000 0.00000▼
▲{{out}}
Time
0
1
2
3 10.
5 52.56250 52.56249 1.082e-005
7 175.56250 175.56248 2.352e-005
9 451.56250 451.56246 4.072e-005
10 676.00000 675.99995 5.098e-005</pre>
=={{header|PARI/GP}}==
|