Euler method: Difference between revisions

→‎Tcl: Added implementation
(Added to the "Mathematical operations" category.)
(→‎Tcl: Added implementation)
Line 286:
90 20.0016
100 20.0005</lang>
 
=={{header|Tcl}}==
{{trans|C++}}
<lang tcl>proc euler {f y0 a b h} {
puts "computing $f over \[$a..$b\], step $h"
set y [expr {double($y0)}]
for {set t [expr {double($a)}]} {$t < $b} {set t [expr {$t + $h}]} {
puts [format "%.3f\t%.3f" $t $y]
set y [expr {$y + $h * double([$f $t $y])}]
}
puts "done"
}</lang>
Demonstration with the Newton Cooling Law:
<lang tcl>proc newtonCoolingLaw {time temp} {
expr {-0.07 * ($temp - 20)}
}
 
euler newtonCoolingLaw 100 0 100 2
euler newtonCoolingLaw 100 0 100 5
euler newtonCoolingLaw 100 0 100 10</lang>
End of output:
<pre>
...
computing newtonCoolingLaw over [0..100], step 10
0.000 100.000
10.000 44.000
20.000 27.200
30.000 22.160
40.000 20.648
50.000 20.194
60.000 20.058
70.000 20.017
80.000 20.005
90.000 20.002
done
</pre>
Anonymous user