Anonymous user
Runge-Kutta method: Difference between revisions
m
→{{header|REXX}}: changed/added comments and whitespace, optimized the sqrt and RK4 functions, changed the fmt function for better output alignment.
(Add Nim code for RK task) |
m (→{{header|REXX}}: changed/added comments and whitespace, optimized the sqrt and RK4 functions, changed the fmt function for better output alignment.) |
||
Line 2,158:
</pre>
<lang rexx>/*REXX program uses the Runge─Kutta method to solve the equation: y'(t)=t² √[y(t)] */
numeric digits 40; f=digits() % 4
x0=0; x1=10;
n=1 + (x1-x0) / dx
y.=1;
end /*m*/
w=digits() % 2
do i=0 to n-1 by 10; x=(x0 + dx*i) / 1; $=y.i/(x*x/4+1)**2 -1
▲say center('X', f, "═") center('Y', w+2, "═") center("relative error", w+8, '═')
▲ say center(x,f) fmt(y.i) left('', 2 + ($>=0)) fmt($)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
fmt: z=right( format( arg(1), w, f), w); hasE=pos('E', z)\==0;
return translate(right(z, (z>=0) + w + 5*hasE + 2*(jus & (z<0) ) ), 'e', "E")
/*──────────────────────────────────────────────────────────────────────────────────────*/
return y + (k1 + k2*2
▲ k4= dx * rate( x +dx , y +k3 )
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g * .5'e'_ % 2
Programming note: the '''fmt''' function is used to
align the output with attention paid to the different ways some
'''output''' when using Regina REXX:▼
<br>REXXes format numbers that are in floating point representation.
<pre>
════X═════ ══════════Y═══════════ ═══════relative error═══════
Line 2,204 ⟶ 2,205:
10 675.9999490167 -7.5419068846e-8
</pre>
<pre>
════X═════ ══════════Y═══════════ ═══════relative error═══════
0 1 0
1 1.5624998543 -0.0000000933
2 3.9999990805 -0.0000002299
3 10.5624970904 -0.0000002755
4 24.9999937651 -0.0000002494
5 52.5624891803 -0.0000002058
6 99.9999834054 -0.0000001659
7 175.5624764823 -0.000000134
8 288.9999684348 -0.0000001092
9 451.5624592768 -0.0000000902
10 675.9999490167 -0.0000000754
</pre>
|