Euler method: Difference between revisions

Added Xbasic
m (→‎version 2: used a template for the output section.)
(Added Xbasic)
 
(66 intermediate revisions by 26 users not shown)
Line 62:
A reference solution ([[#Common Lisp|Common Lisp]]) can be seen below.   We see that bigger step sizes lead to reduced approximation accuracy.
[[Image:Euler_Method_Newton_Cooling.png|center|750px]]
 
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">F euler(f, y0, a, b, h)
V t = a
V y = y0
L t <= b
print(‘#2.3 #2.3’.format(t, y))
t += h
y += h * f(t, y)
 
V newtoncooling = (time, temp) -> -0.07 * (temp - 20)
 
euler(newtoncooling, 100.0, 0.0, 100.0, 10.0)</syntaxhighlight>
{{out}}
<pre>
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
100.000 20.000
</pre>
 
=={{header|Ada}}==
The solution is generic, usable for any floating point type. The package specification:
<syntaxhighlight lang="ada">
<lang Ada>
generic
type Number is digits <>;
Line 77 ⟶ 105:
) return Waveform;
end Euler;
</syntaxhighlight>
</lang>
The function Solve returns the solution of the differential equation for each of N+1 points, starting from the point T0. The implementation:
<syntaxhighlight lang="ada">
<lang Ada>
package body Euler is
function Solve
Line 97 ⟶ 125:
end Solve;
end Euler;
</syntaxhighlight>
</lang>
The test program:
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
with Euler;
Line 118 ⟶ 146:
end loop;
end Test_Euler_Method;
</syntaxhighlight>
</lang>
Sample output:
<pre>
Line 133 ⟶ 161:
100: 2.00005E+01
</pre>
 
=={{header|ALGOL 68}}==
{{trans|D}} Note: This specimen retains the original [[#D|D]] coding style.
Line 138 ⟶ 167:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<langsyntaxhighlight lang="algol68">#
Approximates y(t) in y'(t)=f(t,y) with y(a)=y0 and
t=a..b and the step size h.
Line 161 ⟶ 190:
main: (
euler(newton cooling law, 100, 0, 100, 10)
)</langsyntaxhighlight>
Ouput:
<pre>
Line 176 ⟶ 205:
done
</pre>
 
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
Which is
{{Trans|D}}
<syntaxhighlight lang="algolw">begin % Euler's method %
% Approximates y(t) in y'(t)=f(t,y) with y(a)=y0 and t=a..b and the step size h. %
real procedure euler ( real procedure f; real value y0, a, b, h ) ;
begin
real y, t;
y := y0;
t := a;
while t < b do begin
write( r_format := "A", r_w := 8, r_d := 4, s_w := 0, t, ": ", y );
y := y + ( h * f(t, y) );
t := t + h
end while_t_lt_b ;
write( "done" );
y
end euler ;
 
% Example: Newton's cooling law %
real procedure newtonCoolingLaw ( real value time, t ) ; -0.07 * (t - 20);
 
euler( newtonCoolingLaw, 100, 0, 100, 10 )
end.</syntaxhighlight>
{{out}}
<pre>
0.0000: 100.0000
10.0000: 44.0000
20.0000: 27.2000
30.0000: 22.1600
40.0000: 20.6480
50.0000: 20.1944
60.0000: 20.0583
70.0000: 20.0175
80.0000: 20.0052
90.0000: 20.0015
done
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">euler: function [f, y0, a, b, h][
[t,y]: @[a, y0]
 
while [t < b][
print [to :string .format:".3f" t, to :string .format:".3f" y]
t: t + h
y: y + h * call f @[t,y]
]
]
 
newtoncooling: function [ti, te]->
(neg 0.07) * te - 20
 
euler 'newtoncooling 100.0 0.0 100.0 10.0</syntaxhighlight>
 
{{out}}
 
<pre>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</pre>
 
=={{header|ATS}}==
{{trans|Ol}}
 
The following program's output should be fed to [[Gnuplot]], which will produce a PNG (using either the font that is specified by the ATS program or a fallback substitute). You can run the program with a command such as this: <code>patscc -g -O2 -std=gnu2x -DATS_MEMALLOC_LIBC euler_method_task.dats && ./a.out | gnuplot</code>
 
All data requiring allocation is allocated as linear types, and so is not leaked. That is, roughly speaking: it requires no garbage collection.
 
<syntaxhighlight lang="ats">
#include "share/atspre_staload.hats"
staload UN = "prelude/SATS/unsafe.sats"
 
#define NIL list_vt_nil ()
#define :: list_vt_cons
 
(* Approximate y(t) in dy/dt=f(t,y), y(a)=y0, t going from a to b with
positive step size h. This implementation of euler_method requires
f to be a unboxed linear closure. *)
extern fn {tk : tkind}
euler_method (f : &(g0float tk, g0float tk) -<clo1> g0float tk,
y0 : g0float tk,
a : g0float tk,
b : g0float tk,
h : g0float tk) : List1_vt @(g0float tk, g0float tk)
 
implement {tk}
euler_method (f, y0, a, b, h) =
let
typedef point_pair = @(g0float tk, g0float tk)
 
fun
loop (f : &(g0float tk, g0float tk) -<clo1> g0float tk,
t : g0float tk,
y : g0float tk,
point_pairs : List0_vt point_pair)
: List1_vt point_pair =
let
val point_pairs = @(t, y) :: point_pairs
in
if b <= t then
reverse<point_pair> point_pairs
else
loop (f, t + h, y + (h * f (t, y)), point_pairs)
end
in
loop (f, a, y0, NIL)
end
 
fun {tk : tkind}
write_point_pairs
(outf : FILEref,
point_pairs : !List0_vt @(g0float tk, g0float tk))
: void =
case+ point_pairs of
| NIL => ()
| (@(t, y) :: tl) =>
begin
fprint_val<g0float tk> (outf, t);
fprint! (outf, " ");
fprint_val<g0float tk> (outf, y);
fprintln! (outf);
write_point_pairs (outf, tl)
end
 
implement
main0 () =
let
(* Implement f as a stack-allocated linear closure. *)
var f =
lam@ (t : double, y : double) : double => ~0.07 * (y - 20.0)
 
val data2 = euler_method<dblknd> (f, 100.0, 0.0, 100.0, 2.0)
and data5 = euler_method<dblknd> (f, 100.0, 0.0, 100.0, 5.0)
and data10 = euler_method<dblknd> (f, 100.0, 0.0, 100.0, 10.0)
 
val outf = stdout_ref
in
fprintln! (outf, "set encoding utf8");
fprintln! (outf, "set term png size 1000,750 font 'RTF Amethyst Pro,16'");
fprintln! (outf, "set output 'newton-cooling-ATS.png'");
fprintln! (outf, "set grid");
fprintln! (outf, "set title 'Newton’s Law of Cooling'");
fprintln! (outf, "set xlabel 'Elapsed time (seconds)'");
fprintln! (outf, "set ylabel 'Temperature (Celsius)'");
fprintln! (outf, "set xrange [0:100]");
fprintln! (outf, "set yrange [15:100]");
fprintln! (outf, "y(x) = 20.0 + (80.0 * exp (-0.07 * x))");
fprintln! (outf, "plot y(x) with lines title 'Analytic solution', \\");
fprintln! (outf, " '-' with linespoints title 'Euler method, step size 2s', \\");
fprintln! (outf, " '-' with linespoints title 'Step size 5s', \\");
fprintln! (outf, " '-' with linespoints title 'Step size 10s'");
write_point_pairs (outf, data2);
fprintln! (outf, "e");
write_point_pairs (outf, data5);
fprintln! (outf, "e");
write_point_pairs (outf, data10);
fprintln! (outf, "e");
 
free data2;
free data5;
free data10
end
</syntaxhighlight>
 
{{out}}
[[File:Euler method ATS--newton-cooling.2023.04.26.12.24.43.png|thumb|none|alt=The output of the ATS program, plotted by Gnuplot.]]
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{trans|GW-BASIC}}
<syntaxhighlight lang="qbasic">100 HOME
110 PRINT "Time ";
120 FOR S = 0 TO 100.1 STEP 10
130 PRINT S; " ";
140 NEXT S
150 PRINT
160 PRINT "Dif eq ";
170 FOR S = 0 TO 100.1 STEP 10
180 LET T = 20+(100-20)*EXP(-.07*S)
190 PRINT LEFT$(STR$(T),5); " ";
200 NEXT S
210 PRINT
220 LET P = 2 : GOSUB 260
230 LET P = 5 : GOSUB 260
240 LET P = 10 : GOSUB 260
250 END
260 REM Euler(paso)
270 LET S = 0
280 LET T = 100
290 PRINT "Step ";P; " ";
300 FOR S = 0 TO 100 STEP P
310 IF S - (S/10) * 10 = 0 THEN PRINT LEFT$(STR$(T),5); " ";
320 LET T = T+(P)*(-.07*(T-20))
330 IF S > 100 THEN GOTO 350
340 NEXT S
350 PRINT
360 RETURN</syntaxhighlight>
 
==={{header|BASIC256}}===
{{trans|XPL0}}
<syntaxhighlight lang="vbnet">print "Time ";
tiempo = 0.0
while tiempo <= 100.1
print rjust(string(tiempo), 5); " ";
tiempo += 10.0
end while
print
 
print "Dif eq ";
tiempo = 0.0
while tiempo <= 100.1
temperatura = 20.0 + (100.0-20.0) * exp(-0.07*tiempo)
print rjust(left(string(temperatura), 5), 5); " ";
tiempo += 10.0
end while
print
 
call Euler(2)
call Euler(5)
call Euler(10)
end
 
subroutine Euler(paso)
tiempo = 0
temperatura = 100.0
print "Step "; rjust(string(paso), 2); " ";
 
while tiempo <= 100
if (tiempo mod 10) = 0 then print rjust(left(string(temperatura), 5), 5); " ";
temperatura += float(paso) * (-0.07*(temperatura-20.0))
tiempo += paso
end while
print
end subroutine</syntaxhighlight>
 
==={{header|BBC BASIC}}===
<langsyntaxhighlight lang="bbcbasic"> PROCeuler("-0.07*(y-20)", 100, 0, 100, 2)
PROCeuler("-0.07*(y-20)", 100, 0, 100, 5)
PROCeuler("-0.07*(y-20)", 100, 0, 100, 10)
Line 193 ⟶ 465:
t += s
ENDWHILE
ENDPROC</langsyntaxhighlight>
'''Output:'''
<pre>
Line 215 ⟶ 487:
100.000 20.000
</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
Same code as [[#GW-BASIC|GW-BASIC]]
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">precision 4
 
let s = 2
gosub euler
 
let s = 5
gosub euler
 
let s = 10
gosub euler
 
end
 
sub euler
 
cls
cursor 1, 1
wait
print "step: ", s
 
let b = 100
let y = 100
 
for t = 0 to b step s
 
print t, " : ", y
 
let y = y + s * (-0.07 * (y - 20))
 
gosub delay
 
next t
 
alert "step ", s, " finished"
 
return
 
sub delay
 
let w = clock
 
do
 
wait
 
loop clock < w + 200
 
return</syntaxhighlight>
{{out| Output}}<pre>step: 2
100 88.8000 79.1680 70.8845 63.7607 57.6342 52.3654 47.8342 43.9374 40.5862 37.7041 35.2255 33.0939 31.2608 29.6843 28.3285 27.1625 26.1597 25.2973 24.5557 23.9179 23.3694 22.8977 22.4920 22.1431 21.8431 21.5851 21.3632 21.1724 21.0083 20.8671 20.7457 20.6413 20.5515 20.4743 20.4079 20.3508 20.3017 20.2595 20.2232 20.1920 20.1651 20.1420 20.1221 20.1050 20.0903 20.0777 20.0668 20.0574 20.0494 20.0425
 
step: 5
100 72 53.8000 41.9700 34.2805 29.2823 26.0335 23.9218 22.5492 21.6570 21.0771 20.7001 20.4551 20.2958 20.1923 20.1250 20.0813 20.0528 20.0343 20.0223 20.0145
 
step: 10
100 44 27.2000 22.1600 20.6480 20.1944 20.0583 20.0175 20.0053 20.0016 20.0005 </pre>
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight FreeBASIClang="freebasic">'Freebasic .9
'Custom rounding
#define round(x,N) Rtrim(Rtrim(Left(Str((x)+(.5*Sgn((x)))/(10^(N))),Instr(Str((x)+(.5*Sgn((x)))/(10^(N))),".")+(N)),"0"),".")
Line 241 ⟶ 575:
Euler(-.07*(y-20),100,0,100,10,"print")
Sleep
</syntaxhighlight>
</lang>
outputs (steps 5 and 10)
<pre>
Line 293 ⟶ 627:
 
</pre>
 
==={{header|Gambas}}===
{{trans|XPL0}}
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim tiempo As Float, temperatura As Float
 
Print "Time ";
tiempo = 0.0
While tiempo <= 100.1
Print Format$(tiempo, "#######");
tiempo += 10.0
Wend
Print
Print "Dif eq ";
tiempo = 0.0
While tiempo <= 100.1
temperatura = 20.0 + (100.0 - 20.0) * Exp(-0.07 * tiempo)
Print Format$(temperatura, "####.#0");
tiempo += 10.0
Wend
Print
Euler(2)
Euler(5)
Euler(10)
End
 
Public Sub Euler(paso As Integer)
 
Dim tiempo As Integer = 0
Dim temperatura As Float = 100.0
 
Print "Step "; Format$(paso, "##"); " ";
Do While tiempo <= 100
If (tiempo Mod 10) = 0 Then Print Format$(temperatura, "####.#0");
temperatura += (paso) * (-0.07 * (temperatura - 20.0))
tiempo += paso
Loop
Print
 
End Sub</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{trans|XPL0}}
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">100 CLS
110 PRINT "Time ";
120 FOR TIEMPO = 0 TO 100.1 STEP 10
130 PRINT USING " ###";TIEMPO;
140 NEXT TIEMPO
150 PRINT
160 PRINT "Dif eq ";
170 FOR TIEMPO = 0 TO 100.1 STEP 10
180 TEMPERATURA = 20+(100-20)*EXP(-.07*TIEMPO)
190 PRINT USING "###.##";TEMPERATURA;
200 NEXT TIEMPO
210 PRINT
220 PASO = 2 : GOSUB 260
230 PASO = 5 : GOSUB 260
240 PASO = 10 : GOSUB 260
250 END
260 REM Euler(paso)
270 TIEMPO = 0
280 TEMPERATURA = 100
290 PRINT USING "Step ## ";PASO;
300 FOR TIEMPO = 0 TO 100 STEP PASO
310 IF (TIEMPO MOD 10) = 0 THEN PRINT USING "###.##";TEMPERATURA;
320 TEMPERATURA = TEMPERATURA+(PASO)*(-.07*(TEMPERATURA-20))
330 IF TIEMPO > 100 THEN EXIT FOR
340 NEXT TIEMPO
350 PRINT
360 RETURN</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
Same code as [[#GW-BASIC|GW-BASIC]]
 
==={{header|QBasic}}===
{{trans|XPL0}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">DECLARE SUB Euler (paso AS INTEGER)
 
CLS
PRINT "Time ";
tiempo = 0!
WHILE tiempo <= 100.1
PRINT USING "######"; tiempo;
tiempo = tiempo + 10!
WEND
PRINT
 
PRINT "Dif eq ";
tiempo = 0!
WHILE tiempo <= 100.1
temperatura = 20! + (100! - 20!) * EXP(-.07 * tiempo)
PRINT USING "###.##"; temperatura;
tiempo = tiempo + 10!
WEND
PRINT
 
Euler (2)
Euler (5)
Euler (10)
END
 
SUB Euler (paso AS INTEGER)
tiempo = 0
temperatura = 100!
PRINT USING "Step ## "; paso;
DO WHILE tiempo <= 100
IF (tiempo MOD 10) = 0 THEN PRINT USING "###.##"; temperatura;
temperatura = temperatura + paso * (-.07 * (temperatura - 20!))
tiempo = tiempo + paso
LOOP
PRINT
END SUB</syntaxhighlight>
 
==={{header|Run BASIC}}===
<langsyntaxhighlight lang="rinbasic">x = euler(-0.07,-20, 100, 0, 100, 2)
x = euler-0.07,-20, 100, 0, 100, 5)
x = euler(-0.07,-20, 100, 0, 100, 10)
Line 308 ⟶ 767:
t = t + s
WEND
END FUNCTION</langsyntaxhighlight>
<pre>===== da:-0.07 db:-20 y:100 a:0 b:100 s:2 ===================
0 100
Line 329 ⟶ 788:
70 20.017496
80 20.0052488</pre>
 
==={{header|True BASIC}}===
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">SUB euler (paso)
LET tiempo = 0
LET temperatura = 100
PRINT USING "Step ## ": paso;
DO WHILE tiempo <= 100
IF (REMAINDER(tiempo,10)) = 0 THEN PRINT USING "###.##": temperatura;
LET temperatura = temperatura+paso*(-.07*(temperatura-20))
LET tiempo = tiempo+paso
LOOP
PRINT
END SUB
 
PRINT "Time ";
LET tiempo = 0
DO WHILE tiempo <= 100.1
PRINT USING "######": tiempo;
LET tiempo = tiempo+10
LOOP
PRINT
PRINT "Dif eq ";
LET tiempo = 0
DO WHILE tiempo <= 100.1
LET temperatura = 20+(100-20)*EXP(-.07*tiempo)
PRINT USING "###.##": temperatura;
LET tiempo = tiempo+10
LOOP
PRINT
 
CALL Euler (2)
CALL Euler (5)
CALL Euler (10)
END</syntaxhighlight>
{{out}}
<pre>Same as QBasic entry.</pre>
 
==={{header|XBasic}}===
{{trans|BASIC256}}
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Euclidean rhythm"
VERSION "0.0001"
IMPORT "xma"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION Euler (paso)
 
FUNCTION Entry ()
PRINT "Time ";
tiempo! = 0.0
DO WHILE tiempo! <= 100.1
PRINT FORMAT$ ("#######", tiempo!);
tiempo! = tiempo! + 10.0
LOOP
PRINT
 
PRINT "Dif eq ";
tiempo! = 0.0
DO WHILE tiempo! <= 100.1
temperatura! = 20.0 + (100.0 - 20.0) * EXP(-0.07 * tiempo!)
PRINT FORMAT$ ("####.##", temperatura!);
tiempo! = tiempo! + 10.0
LOOP
PRINT
 
Euler(2)
Euler(5)
Euler(10)
END FUNCTION
 
FUNCTION Euler (paso)
tiempo! = 0
temperatura! = 100.0
PRINT FORMAT$ ("Step ## ", paso);
 
DO WHILE tiempo! <= 100
IF (tiempo! MOD 10) = 0 THEN PRINT FORMAT$ ("####.##", temperatura!);
temperatura! = temperatura! + SINGLE(paso) * (-0.07 * (temperatura! - 20.0))
tiempo! = tiempo! + paso
LOOP
PRINT
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|XPL0}}
<syntaxhighlight lang="vbnet">print "Time ";
tiempo = 0.0
while tiempo <= 100.1
print tiempo using "#######";
tiempo = tiempo + 10.0
wend
print
 
print "Dif eq ";
tiempo = 0.0
while tiempo <= 100.1
temperatura = 20.0 + (100.0-20.0) * exp(-0.07*tiempo)
print temperatura using "####.##";
tiempo = tiempo + 10.0
wend
print
 
Euler_(2)
Euler_(5)
Euler_(10)
end
 
sub Euler_(paso)
local tiempo, temperatura
tiempo = 0
temperatura = 100.0
print "Step ", paso using "##", " ";
while tiempo <= 100
if mod(tiempo, 10) = 0 print temperatura using "####.##";
temperatura = temperatura + (paso) * (-0.07*(temperatura-20.0))
tiempo = tiempo + paso
end while
print
end sub</syntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 374 ⟶ 955:
 
return 0;
}</langsyntaxhighlight>output<syntaxhighlight lang="text"> Time: 0 10 20 30 40 50 60 70 80 90 100
Analytic: 100.000 59.727 39.728 29.797 24.865 22.416 21.200 20.596 20.296 20.147 20.073
Step 2: 100.000 57.634 37.704 28.328 23.918 21.843 20.867 20.408 20.192 20.090 20.042
Step 5: 100.000 53.800 34.280 26.034 22.549 21.077 20.455 20.192 20.081 20.034 20.014
Step 10: 100.000 44.000 27.200 22.160 20.648 20.194 20.058 20.017 20.005 20.002 20.000</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
 
namespace prog
{
class MainClass
{
const float T0 = 100f;
const float TR = 20f;
const float k = 0.07f;
readonly static float[] delta_t = {2.0f,5.0f,10.0f};
const int n = 100;
public delegate float func(float t);
static float NewtonCooling(float t)
{
return -k * (t-TR);
}
public static void Main (string[] args)
{
func f = new func(NewtonCooling);
for(int i=0; i<delta_t.Length; i++)
{
Console.WriteLine("delta_t = " + delta_t[i]);
Euler(f,T0,n,delta_t[i]);
}
}
public static void Euler(func f, float y, int n, float h)
{
for(float x=0; x<=n; x+=h)
{
Console.WriteLine("\t" + x + "\t" + y);
y += h * f(y);
}
}
}
}</syntaxhighlight>
 
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 413 ⟶ 1,034:
euler(newtonCoolingLaw, 100, 0, 100, 5);
euler(newtonCoolingLaw, 100, 0, 100, 10);
}</langsyntaxhighlight>
Last part of output:
<pre>
Line 429 ⟶ 1,050:
done
</pre>
=={{header|C sharp}}==
<lang csharp>using System;
 
namespace prog
{
class MainClass
{
const float T0 = 100f;
const float TR = 20f;
const float k = 0.07f;
readonly static float[] delta_t = {2.0f,5.0f,10.0f};
const int n = 100;
public delegate float func(float t);
static float NewtonCooling(float t)
{
return -k * (t-TR);
}
public static void Main (string[] args)
{
func f = new func(NewtonCooling);
for(int i=0; i<delta_t.Length; i++)
{
Console.WriteLine("delta_t = " + delta_t[i]);
Euler(f,T0,n,delta_t[i]);
}
}
public static void Euler(func f, float y, int n, float h)
{
for(float x=0; x<=n; x+=h)
{
Console.WriteLine("\t" + x + "\t" + y);
y += h * f(y);
}
}
}
}</lang>
 
=={{header|Clay}}==
 
<syntaxhighlight lang="clay">
<lang Clay>
import printer.formatter as pf;
 
Line 488 ⟶ 1,070:
}
}
</syntaxhighlight>
</lang>
 
Example output:
Line 503 ⟶ 1,085:
80 20.0052488
90 20.00157464
</pre>
 
=={{header|Clojure}}==
{{trans|Python}}
<syntaxhighlight lang="lisp">(ns newton-cooling
(:gen-class))
 
(defn euler [f y0 a b h]
"Euler's Method.
Approximates y(time) in y'(time)=f(time,y) with y(a)=y0 and t=a..b and the step size h."
(loop [t a
y y0
result []]
(if (<= t b)
(recur (+ t h) (+ y (* (f (+ t h) y) h)) (conj result [(double t) (double y)]))
result)))
 
(defn newton-coolling [t temp]
"Newton's cooling law, f(t,T) = -0.07*(T-20)"
(* -0.07 (- temp 20)))
 
; Run for case h = 10
(println "Example output")
(doseq [q (euler newton-coolling 100 0 100 10)]
(println (apply format "%.3f %.3f" q)))
</syntaxhighlight>
{{Output}}
<pre>
Example output
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
100.000 20.000
</pre>
 
Line 510 ⟶ 1,132:
{{works with|Visual COBOL}}
The following is in the Managed COBOL dialect:
<langsyntaxhighlight lang="cobol"> DELEGATE-ID func.
PROCEDURE DIVISION USING VALUE t AS FLOAT-LONG
RETURNING ret AS FLOAT-LONG.
Line 554 ⟶ 1,176:
END-PERFORM
END METHOD.
END CLASS.</langsyntaxhighlight>
 
Example output:
Line 569 ⟶ 1,191:
x = 80.0000, y = 20.0052
x = 90.0000, y = 20.0016
</pre>
 
=={{header|Clojure}}==
{{trans|Python}}
<lang lisp>(ns newton-cooling
(:gen-class))
 
(defn euler [f y0 a b h]
"Euler's Method.
Approximates y(time) in y'(time)=f(time,y) with y(a)=y0 and t=a..b and the step size h."
(loop [t a
y y0
result []]
(if (<= t b)
(recur (+ t h) (+ y (* (f (+ t h) y) h)) (conj result [(double t) (double y)]))
result)))
 
(defn newton-coolling [t temp]
"Newton's cooling law, f(t,T) = -0.07*(T-20)"
(* -0.07 (- temp 20)))
 
; Run for case h = 10
(println "Example output")
(doseq [q (euler newton-coolling 100 0 100 10)]
(println (apply format "%.3f %.3f" q)))
</lang>
{{Output}}
<pre>
Example output
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
100.000 20.000
</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">;; 't' usually means "true" in CL, but we need 't' here for time/temperature.
(defconstant true 'cl:t)
(shadow 't)
Line 637 ⟶ 1,219:
(euler #'newton-cooling 100 0 100 2)
(euler #'newton-cooling 100 0 100 5)
(euler #'newton-cooling 100 0 100 10)</langsyntaxhighlight>
 
<langsyntaxhighlight lang="lisp">;; slightly more idiomatic Common Lisp version
 
(defun newton-cooling (time temperature)
Line 651 ⟶ 1,233:
(loop for time from a below b by h
for y = y0 then (+ y (* h (funcall f time y)))
do (format t "~6,3F ~6,3F~%" time y)))</langsyntaxhighlight>
 
<pre>
Line 669 ⟶ 1,251:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.range, std.traits;
 
/// Approximates y(t) in y'(t)=f(t,y) with y(a)=y0 and t=a..b and the step size h.
Line 690 ⟶ 1,272:
euler(newtonCoolingLaw, 100, 0, 100, 5);
euler(newtonCoolingLaw, 100, 0, 100, 10);
}</langsyntaxhighlight>
Last part of the output:
<pre>...
Line 704 ⟶ 1,286:
90.000 20.002
done</pre>
 
=={{header|Dart}}==
{{trans|Swift}}
<syntaxhighlight lang="Dart">
import 'dart:math';
import "dart:io";
 
const double k = 0.07;
const double initialTemp = 100.0;
const double finalTemp = 20.0;
const int startTime = 0;
const int endTime = 100;
 
void ivpEuler(double Function(double, double) function, double initialValue, int step) {
stdout.write(' Step ${step.toString().padLeft(2)}: ');
var y = initialValue;
for (int t = startTime; t <= endTime; t += step) {
if (t % 10 == 0) {
stdout.write(y.toStringAsFixed(3).padLeft(7));
}
y += step * function(t.toDouble(), y);
}
print('');
}
 
void analytic() {
stdout.write(' Time: ');
for (int t = startTime; t <= endTime; t += 10) {
stdout.write(t.toString().padLeft(7));
}
stdout.write('\nAnalytic: ');
for (int t = startTime; t <= endTime; t += 10) {
var temp = finalTemp + (initialTemp - finalTemp) * exp(-k * t);
stdout.write(temp.toStringAsFixed(3).padLeft(7));
}
print('');
}
 
double cooling(double t, double temp) {
return -k * (temp - finalTemp);
}
 
void main() {
analytic();
ivpEuler(cooling, initialTemp, 2);
ivpEuler(cooling, initialTemp, 5);
ivpEuler(cooling, initialTemp, 10);
}
</syntaxhighlight>
{{out}}
<pre>
Time: 0 10 20 30 40 50 60 70 80 90 100
Analytic: 100.000 59.727 39.728 29.797 24.865 22.416 21.200 20.596 20.296 20.147 20.073
Step 2: 100.000 57.634 37.704 28.328 23.918 21.843 20.867 20.408 20.192 20.090 20.042
Step 5: 100.000 53.800 34.280 26.034 22.549 21.077 20.455 20.192 20.081 20.034 20.014
Step 10: 100.000 44.000 27.200 22.160 20.648 20.194 20.058 20.017 20.005 20.002 20.000
 
</pre>
 
=={{header|Delphi}}==
 
[[Euler_method#Pascal | Pascal]]
 
=={{header|EasyLang}}==
[https://easylang.online/show/#cod=jZDNbsIwEITv+xQjKlX8CGsJiiBV8wTcUO4oBAORQhyZpSF9+moNpYA49ObZn/HOly2RImJaIMWYDc9oe6oL5HVedVIWyBgCQwC8lZOvkS0xQj9jjJEtBxiicS0iM5vMozn6CwwhAzLUyVmQIomp8a7Axuft6maaYw1zcS1c5TyYExUH92UxZ+iy6mAyThGrEHsW9H49elraOg/9JYc4rLUCoCprC/lLMGFGcDNkSO/Y+XJDOtWWG9mDzZTU+1h+W0zp8VLWdXrDhUptW3H1qnCuKusdxB6aBzaavh+qAQ6Zu/D2VFmPjkP4o9hGoz9SKFz1Twq6/4Ee3oNTqCuGMI0UHeur3ZeVkvh8RtNdpWCU3gzC6lVj+By1uwd4zaJgAx9MGAnz616MOI5ftyJwwkQ/ Run it]
 
<syntaxhighlight>
TR = 20
K = -0.07
func analytic T0 t .
return TR + (T0 - TR) * pow 2.71828 (K * t)
.
ytxt = 95
proc draw_analytic a b . .
color 009
move 80 ytxt
ytxt -= 5
text "analytic"
for t = a to b
line t analytic 100 t
.
.
drawgrid
linewidth 0.3
textsize 3
draw_analytic 0 100
#
func newton_cooling temp .
return K * (temp - TR)
.
proc draw_euler y0 a b step col . .
color col
move 80 ytxt
ytxt -= 5
text "step: " & step
t = a
y = y0
while t < b
line t y
t += step
y += step * newton_cooling y
.
.
draw_euler 100 0 100 10 900
draw_euler 100 0 100 5 555
draw_euler 100 0 100 2 090
</syntaxhighlight>
 
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule Euler do
def method(_, _, t, b, _) when t>b, do: :ok
def method(f, y, t, b, h) do
Line 723 ⟶ 1,408:
IO.puts "\nStep = #{step}"
Euler.method(f, 100.0, 0.0, 100.0, step)
end)</langsyntaxhighlight>
 
{{out}}
Line 819 ⟶ 1,504:
=={{header|Erlang}}==
 
<langsyntaxhighlight lang="erlang">
-module(euler).
-export([main/0, euler/5]).
Line 858 ⟶ 1,543:
euler(fun cooling/2, 100, 0, 10, 100),
ok.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 874 ⟶ 1,559:
=={{header|Euler Math Toolbox}}==
 
<syntaxhighlight lang="euler math toolbox">
<lang Euler Math Toolbox>
>function dgleuler (f,x,y0) ...
$ y=zeros(size(x)); y[1]=y0;
Line 896 ⟶ 1,581:
>adaptiverunge("f",x,TS)[-1] // Adaptive Runge Method
20.0729505572
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let euler f (h : float) t0 y0 =
(t0, y0)
|> Seq.unfold (fun (t, y) -> Some((t,y), ((t + h), (y + h * (f t y)))))
Line 915 ⟶ 1,600:
|> Seq.takeWhile (fun (t,_) -> t <= b)
|> Seq.iter (printfn "%A")
0</langsyntaxhighlight>
Output for the above (step size 10)
<pre>(0.0, 100.0)
Line 928 ⟶ 1,613:
(90.0, 20.00157464)
(100.0, 20.00047239)</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: formatting fry io kernel locals math math.ranges
sequences ;
IN: rosetta-code.euler-method
 
:: euler ( quot y! a b h -- )
a b h <range> [
:> t
t y "%7.3f %7.3f\n" printf
t y quot call h * y + y!
] each ; inline
 
: cooling ( t y -- x ) nip 20 - -0.07 * ;
 
: euler-method-demo ( -- )
2 5 10 [ '[ [ cooling ] 100 0 100 _ euler ] call nl ] tri@ ;
 
MAIN: euler-method-demo</syntaxhighlight>
{{out}}
<pre>
. . .
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
100.000 20.000
</pre>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: newton-cooling-law ( f: temp -- f: temp' )
20e f- -0.07e f* ;
 
Line 943 ⟶ 1,662:
100e ' newton-cooling-law 2 100 euler cr
100e ' newton-cooling-law 5 100 euler cr
100e ' newton-cooling-law 10 100 euler cr</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|2008}}
<langsyntaxhighlight lang="fortran">program euler_method
use iso_fortran_env, only: real64
implicit none
Line 1,003 ⟶ 1,722:
end function
 
end program</langsyntaxhighlight>
Output for <code>h = 10</code>:
<pre>
Line 1,026 ⟶ 1,745:
determined temperature (so we are computing the error).
 
<syntaxhighlight lang="futhark">
<lang Futhark>
let analytic(t0: f64) (time: f64): f64 =
import "futlib/math"
 
fun analytic(t0: f64) (time: f64): f64 =
20.0 + (t0 - 20.0) * f64.exp(-0.07*time)
 
funlet cooling(_time: f64) (temperature: f64): f64 =
-0.07 * (temperature-20.0)
 
funlet main(t0: f64) (a: f64) (b: f64) (h: f64): []f64 =
let steps = i32.f64 ((b-a)/h)
let temps = replicate steps 0.0
let (_,temps) = loop ((t,temps)=(t0,temps)) = for i < steps do
let x = a + f64(.i32 i) * h
let temps[i] = f64.abs(t-analytic t0 x)
in (t + h * cooling x t,
temps)
in temps
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,114 ⟶ 1,831:
fmt.Println()
}
}</langsyntaxhighlight>
Output, truncated:
<pre>
Line 1,148 ⟶ 1,865:
 
The ''eulerMapping'' closure produces an entire approximating sequence, expressed as a Map object. Here, ''x<sub>0</sub>'' and ''y<sub>0</sub>'' together are the first point in the sequence, the ODE initial conditions. ''h'' and ''dydx'' are again the step distance and the derivative closure. ''stopCond'' is a closure representing a "stop condition" that causes the the ''eulerMapping'' closure to stop after a finite number of steps; the given default value causes ''eulerMapping'' to stop after 100 steps.
<langsyntaxhighlight lang="groovy">def eulerStep = { xn, yn, h, dydx ->
(yn + h * dydx(xn, yn)) as BigDecimal
}
Line 1,162 ⟶ 1,879:
yMap
}
assert eulerMapping.maximumNumberOfParameters == 5</langsyntaxhighlight>
 
 
'''Specific Euler Method Solution for the "Temperature Diffusion" Problem''' (with Newton's derivative formula and constants for environment temperature and object conductivity given)
<langsyntaxhighlight lang="groovy">def dtdsNewton = { s, t, tR, k -> k * (tR - t) }
assert dtdsNewton.maximumNumberOfParameters == 4
 
Line 1,173 ⟶ 1,890:
 
def tEulerH = eulerMapping.rcurry(dtds) { s, t -> s >= 100 }
assert tEulerH.maximumNumberOfParameters == 3</langsyntaxhighlight>
 
 
'''Newton's Analytic Temperature Diffusion Solution''' (for comparison)
<langsyntaxhighlight lang="groovy">def tNewton = { s, s0, t0, tR, k ->
tR + (t0 - tR) * Math.exp(k * (s0 - s))
}
Line 1,183 ⟶ 1,900:
 
def tAnalytic = tNewton.rcurry(0, 100, 20, 0.07)
assert tAnalytic.maximumNumberOfParameters == 1</langsyntaxhighlight>
 
 
'''Specific solutions for 3 step sizes''' (and initial time and temperature)
<langsyntaxhighlight lang="groovy">[10, 5, 2].each { h ->
def tEuler = tEulerH.rcurry(h)
assert tEuler.maximumNumberOfParameters == 2
Line 1,200 ⟶ 1,917:
printf('%5.0f %8.4f %8.4f %9.6f\n', s, tA, tE, relError)
}
}</langsyntaxhighlight>
 
 
Line 1,242 ⟶ 1,959:
=={{header|Haskell}}==
Modular solution which separates the solver and a method. Moreover it works on a given mesh which can be irregular.
<langsyntaxhighlight Haskelllang="haskell">-- the solver
dsolveBy _ _ [] _ = error "empty solution interval"
dsolveBy method f mesh x0 = zip mesh results
where results = scanl (method f) x0 intervals
intervals = zip mesh (tail mesh)</langsyntaxhighlight>
 
It is better to use strict <code>Data.List.scanl'</code> in the solver but avoiding highlighting problems we leave lazy <code>scanl</code> function.
Line 1,252 ⟶ 1,969:
Some possible methods:
 
<langsyntaxhighlight lang="haskell">-- 1-st order Euler
euler f x (t1,t2) = x + (t2 - t1) * f t1 x
 
Line 1,265 ⟶ 1,982:
k3 = f (t1 + h/2) (x + h/2*k2)
k4 = f (t1 + h) (x + h*k3)
h = t2 - t1</langsyntaxhighlight>
 
Graphical output, using EasyPlot:
 
<langsyntaxhighlight lang="haskell">import Graphics.EasyPlot
 
newton t temp = -0.07 * (temp - 20)
Line 1,291 ⟶ 2,008:
where sol1 = dsolveBy euler newton [0,10..100] 100
sol2 = dsolveBy rk2 newton [0,10..100] 100
sol3 = dsolveBy rk4 newton [0,10..100] 100</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,299 ⟶ 2,016:
This solution works in both Icon and Unicon. It takes advantage of the <code>proc</code> procedure, which converts a string naming a procedure into a call to that procedure.
 
<syntaxhighlight lang="icon">
<lang Icon>
invocable "newton_cooling" # needed to use the 'proc' procedure
 
Line 1,322 ⟶ 2,039:
euler ("newton_cooling", 100, 0, 100, step_size)
end
</syntaxhighlight>
</lang>
 
Sample output:
Line 1,342 ⟶ 2,059:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">NB.*euler a Approximates Y(t) in Y'(t)=f(t,Y) with Y(a)=Y0 and t=a..b and step size h.
euler=: adverb define
'Y0 a b h'=. 4{. y
Line 1,351 ⟶ 2,068:
 
ncl=: _0.07 * -&20 NB. Newton's Cooling Law
</syntaxhighlight>
</lang>
'''Example:'''
<langsyntaxhighlight lang="j"> ncl euler 100 0 100 2
... NB. output redacted for brevity
ncl euler 100 0 100 5
Line 1,368 ⟶ 2,085:
80 20.0052
90 20.0016
100 20.0005</langsyntaxhighlight>
 
=={{header|Java}}==
 
<langsyntaxhighlight lang="java">
public class Euler {
private static void euler (Callable f, double y0, int a, int b, int h) {
Line 1,406 ⟶ 2,123:
}
}
</syntaxhighlight>
</lang>
 
Output for step = 10;
Line 1,426 ⟶ 2,143:
=={{header|JavaScript}}==
{{trans|Python}}
<langsyntaxhighlight lang="javascript">
// Function that takes differential-equation, initial condition,
// ending x, and step size as parameters
Line 1,456 ⟶ 2,173:
 
eulersMethod(cooling, 0, 100, 100, 10);
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
{{works with|jq|1.4}}
<langsyntaxhighlight lang="jq"># euler_method takes a filter (df), initial condition
# (x1,y1), ending x (x2), and step size as parameters;
# it emits the y values at each iteration.
Line 1,483 ⟶ 2,201:
# The following solves the task:
# (2,5,10) | [., [ euler_method(cooling; 0; 100; 100; .) ] ]
</syntaxhighlight>
</lang>
For brevity, we modify euler_method so that it only shows the final value of y:
<syntaxhighlight lang="jq">
<lang jq>
def euler_solution(df; x1; y1; x2; h):
def recursion(exp): reduce recurse(exp) as $x (.; $x);
Line 1,495 ⟶ 2,213:
else empty
end )
| .[1] ;</langsyntaxhighlight>
'''Example''':
<langsyntaxhighlight lang="jq">(1,2,5,10,20) | [., [ euler_solution(cooling; 0; 100; 100; .) ] ]</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight lang="sh">$ jq -M -n -c -f Euler_method.jq
[1,[20.05641373347389]]
[2,[20.0424631833732]]
[5,[20.01449963666907]]
[10,[20.000472392]]
[20,[19.180799999999998]]</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|1.0.63}}
<langsyntaxhighlight lang="julia">euler(f::Function, T::Number, t0::Int, t1::Int, h::Int) = collect(begin T += h * f(T); T end for t in t0:h:t1)
 
# Prints a series of arbitrary values in a tabular form, left aligned in cells with a given width
Line 1,519 ⟶ 2,237:
t = 0
for T in euler(y -> -0.07 * (y - 20.0), 100.0, 0, 100, h)
tabular(15, t, round(T,digits=6), round(20.0 + 80.0 * exp(-0.07t), digits=6))
t += h
end
println()
end</langsyntaxhighlight>
 
{{out}}
Line 1,568 ⟶ 2,286:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
typealias Deriv = (Double) -> Double // only one parameter needed here
Line 1,599 ⟶ 2,317:
for (i in listOf(2, 5, 10))
euler(::cooling, 100.0, i, 100)
}</langsyntaxhighlight>
 
{{out}}
Line 1,609 ⟶ 2,327:
Step 10: 100.000 44.000 27.200 22.160 20.648 20.194 20.058 20.017 20.005 20.002 20.000
</pre>
 
=={{header|Lambdatalk}}==
Translation of Python
<syntaxhighlight lang="scheme">
{def eulersMethod
{def eulersMethod.r
{lambda {:f :b :h :t :y}
{if {<= :t :b}
then {tr {td :t} {td {/ {round {* :y 1000}} 1000}}}
{eulersMethod.r :f :b :h {+ :t :h} {+ :y {* :h {:f :t :y}}}}
else}}}
{lambda {:f :y0 :a :b :h}
{table {eulersMethod.r :f :b :h :a :y0}}}}
 
{def cooling
{lambda {:time :temp}
{* -0.07 {- :temp 20}}}}
 
{eulersMethod cooling 100 0 100 10}
->
0 100
10 44
20 27.2
30 22.16
40 20.648
50 20.194
60 20.058
70 20.017
80 20.005
90 20.002
100 20
</syntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">T0 = 100
TR = 20
k = 0.07
Line 1,633 ⟶ 2,383:
Euler( NewtonCooling, T0, n, delta_t[i] )
end
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
Build-in function with Euler:
<syntaxhighlight lang="maple">with(Student[NumericalAnalysis]);
k := 0.07:
TR := 20:
Euler(diff(T(t), t) = -k*(T(t) - TR), T(0) = 100, t = 100, numsteps = 50); # step size = 2
Euler(diff(T(t), t) = -k*(T(t) - TR), T(0) = 100, t = 100, numsteps = 20); # step size = 5
Euler(diff(T(t), t) = -k*(T(t) - TR), T(0) = 100, t = 100, numsteps = 10); # step size = 10</syntaxhighlight>
{{out}}
<pre>
20.04
20.01
20.00
</pre>
Hard-coded procedure:
<syntaxhighlight lang="maple">f := y -> (-0.07) * (y - 20):
 
EulerMethod := proc(f, start_time, end_time, y0, h) # y0: initial value #h: step size
local cur, y, rate:
cur := start_time;
y := y0;
while cur <= end_time do
printf("%g %g\n", cur, y);
cur := cur + h;
rate := f(y);
y := y + h * rate;
end do;
return y;
end proc:
 
# step size = 2
printf("Step Size = %a\n", 2);
EulerMethod(f, 0, 100, 100, 2);
 
# step size = 5
printf("\nStep Size = %a\n", 5);
EulerMethod(f, 0, 100, 100, 5);
 
# step size = 10
printf("\nStep Size = %a\n", 10);
EulerMethod(f, 0, 100, 100, 10);</syntaxhighlight>
{{out}}
<pre style="height: 40ex; overflow: scroll">
Step Size = 2
0 100
2 88.8
4 79.168
6 70.8845
8 63.7607
10 57.6342
12 52.3654
14 47.8342
16 43.9374
18 40.5862
20 37.7041
22 35.2255
24 33.094
26 31.2608
28 29.6843
30 28.3285
32 27.1625
34 26.1598
36 25.2974
38 24.5558
40 23.918
42 23.3694
44 22.8977
46 22.492
48 22.1432
50 21.8431
52 21.5851
54 21.3632
56 21.1723
58 21.0082
60 20.867
62 20.7457
64 20.6413
66 20.5515
68 20.4743
70 20.4079
72 20.3508
74 20.3017
76 20.2594
78 20.2231
80 20.1919
82 20.165
84 20.1419
86 20.122
88 20.105
90 20.0903
92 20.0776
94 20.0668
96 20.0574
98 20.0494
100 20.0425
 
Step Size = 5
0 100
5 72
10 53.8
15 41.97
20 34.2805
25 29.2823
30 26.0335
35 23.9218
40 22.5492
45 21.657
50 21.077
55 20.7001
60 20.455
65 20.2958
70 20.1923
75 20.125
80 20.0812
85 20.0528
90 20.0343
95 20.0223
100 20.0145
 
Step Size = 10
0 100
10 44
20 27.2
30 22.16
40 20.648
50 20.1944
60 20.0583
70 20.0175
80 20.0052
90 20.0016
100 20.0005
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Better methods for differential equation solving are built into Mathematica, so the typical user would omit the Method and StartingStepSize options in the code below. However since the task requests Eulers method, here is the bad solution...
<syntaxhighlight lang="mathematica">
<lang Mathematica>
euler[step_, val_] := NDSolve[
euler[step_, val_] := NDSolve[{T'[t] == -0.07 (T[t] - 20), T[0] == 100}, T, {t, 0, 100}, Method -> "ExplicitEuler", StartingStepSize -> step][[1, 1, 2]][val]
{T'[t] == -0.07 (T[t] - 20), T[0] == 100},
</lang>
T, {t, 0, 100},
Method -> "ExplicitEuler",
StartingStepSize -> step
][[1, 1, 2]][val]
</syntaxhighlight>
{{out}}
<pre>euler[2, 100]
Line 1,649 ⟶ 2,537:
euler[10, 100]
20.0005</pre>
 
=={{header|MATLAB}}==
{{trans|Julia}}
<syntaxhighlight lang="MATLAB}}">
clear all;close all;clc;
format longG;
 
% Main Script
for h = [5, 10]
fprintf('Step %d:\n\n', h);
tabular(15, 'Time', 'Euler', 'Analytic');
T0 = 100.0;
t0 = 0;
t1 = 100;
T = euler(@(T) -0.07 * (T - 20.0), T0, t0, t1, h);
for i = 1:length(T)
t = (i-1) * h;
analytic = 20.0 + 80.0 * exp(-0.07 * t);
tabular(15, t, round(T(i), 6), round(analytic, 6));
end
fprintf('\n');
end
 
function T = euler(f, T0, t0, t1, h)
% EULER A simple implementation of Euler's method for solving ODEs
% f - function handle for the derivative
% T0 - initial temperature
% t0, t1 - start and end times
% h - step size
T = T0;
for t = t0:h:t1
T(end+1) = T(end) + h * f(T(end));
end
end
 
function tabular(width, varargin)
% TABULAR Prints a series of values in a tabular form
% width - cell width
% varargin - variable number of arguments representing cells
for i = 1:length(varargin)
fprintf('%-*s', width, num2str(varargin{i}));
end
fprintf('\n');
end
</syntaxhighlight>
{{out}}
<pre>
Step 5:
 
Time Euler Analytic
0 100 100
5 72 76.375
10 53.8 59.7268
15 41.97 47.995
20 34.2805 39.7278
25 29.2823 33.9019
30 26.0335 29.7965
35 23.9218 26.9035
40 22.5492 24.8648
45 21.657 23.4282
50 21.077 22.4158
55 20.7001 21.7024
60 20.455 21.1996
65 20.2958 20.8454
70 20.1923 20.5957
75 20.125 20.4198
80 20.0812 20.2958
85 20.0528 20.2085
90 20.0343 20.1469
95 20.0223 20.1035
100 20.0145 20.073
105 20.0094 20.0514
 
Step 10:
 
Time Euler Analytic
0 100 100
10 44 59.7268
20 27.2 39.7278
30 22.16 29.7965
40 20.648 24.8648
50 20.1944 22.4158
60 20.0583 21.1996
70 20.0175 20.5957
80 20.0052 20.2958
90 20.0016 20.1469
100 20.0005 20.073
110 20.0001 20.0362
</pre>
 
=={{header|Maxima}}==
<langsyntaxhighlight Maximalang="maxima">euler_method(f, y0, a, b, h):= block(
[t: a, y: y0, tg: [a], yg: [y0]],
unless t>=b do (
Line 1,689 ⟶ 2,666:
[xlabel, "t / seconds"],
[ylabel, "Temperature / C"]);
</syntaxhighlight>
</lang>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">П2 С/П П3 С/П П4 ПП 19 ИП3 * ИП4
+ П4 С/П ИП2 ИП3 + П2 БП 05 ...
... ... ... ... ... ... ... ... ... В/О</langsyntaxhighlight>
 
Instead of dots typed calculation program equation ''f(u, t)'', where the arguments are ''t'' = Р2, ''u'' = Р4.
Line 1,703 ⟶ 2,680:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import strutils
 
proc euler(f: proc (x,y: float): float; y0, a, b, h: float) =
Line 1,712 ⟶ 2,689:
y += h * f(t,y)
 
proc newtoncooling(time, temp: float): float =
-0.07 * (temp - 20)
 
euler(newtoncooling, 100.0, 0.0, 100.0, 10.0)</langsyntaxhighlight>
 
Output:
{{out}}
<pre>0.000 100.000
10.000 44.000
Line 1,729 ⟶ 2,707:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
class EulerMethod {
T0 : static : Float;
Line 1,762 ⟶ 2,740:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 1,779 ⟶ 2,757:
40 20.648
</pre>
 
=={{header|ObjectIcon}}==
 
Output is a PNG produced by [[Gnuplot]], which is run as a child process. The program demonstrates both UTF-8 support and <code>ipl.functional</code>.
 
Note the string written with <code>u"</code> because it contains a non-ASCII character (the apostrophe).
 
<syntaxhighlight lang="objecticon">
import
io(open),
ipl.functional(_a, lambda)
 
$encoding UTF-8
 
procedure main ()
local f, plot, ty
local data2, data5, data10
 
# Newton's cooling law, f(t,Temp) = -0.07*(Temp-20)
f := lambda { -0.07 * (_a[2] - 20.0) }
 
data2 := euler_method (f, 100, 0, 100, 2)
data5 := euler_method (f, 100, 0, 100, 5)
data10 := euler_method (f, 100, 0, 100, 10)
 
plot := open ("gnuplot", "pw")
plot.write ("set encoding utf8")
plot.write ("set term png size 1000,750 font 'Fanwood Text,18'")
plot.write ("set output 'newton-cooling-OI.png'")
plot.write ("set grid")
plot.write (u"set title 'Newton’s Law of Cooling'")
plot.write ("set xlabel 'Elapsed time (seconds)'")
plot.write ("set ylabel 'Temperature (Celsius)'")
plot.write ("set xrange [0:100]")
plot.write ("set yrange [15:100]")
plot.write ("y(x) = 20.0 + (80.0 * exp (-0.07 * x))")
plot.write ("plot y(x) with lines title 'Analytic solution', \\")
plot.write (" '-' with linespoints title 'Euler method, step size 2s', \\")
plot.write (" '-' with linespoints title 'Step size 5s', \\")
plot.write (" '-' with linespoints title 'Step size 10s'")
every plot.write (ty := !data2 & ty[1] || " " || ty[2])
plot.write ("e")
every plot.write (ty := !data5 & ty[1] || " " || ty[2])
plot.write ("e")
every plot.write (ty := !data10 & ty[1] || " " || ty[2])
plot.write ("e")
plot.close()
end
 
# Approximate y(t) in dy/dt=f(t,y), y(a)=y0, t going from a to b with
# positive step size h.
procedure euler_method (f, y0, a, b, h)
local t, y, results
 
t := a
y := y0
results := [[t, y]]
while t + h <= b do
{
y +:= h * f(t, y)
t +:= h
put (results, [t, y])
}
return results
end
</syntaxhighlight>
 
{{out}}
[[File:Euler method OI--newton-cooling.2023.04.26.09.17.33.png|thumb|none|alt=A plot of the curves of the Euler method task.]]
 
=={{header|OCaml}}==
 
<langsyntaxhighlight OCamllang="ocaml">(* Euler integration by recurrence relation.
* Given a function, and stepsize, provides a function of (t,y) which
* returns the next step: (t',y'). *)
Line 1,791 ⟶ 2,838:
 
(* analytic solution for Newton cooling *)
let analytic_solution ~k ~tr ~t0 t = tr +. (t0 -. tr) *. exp (-.k *. t)</langsyntaxhighlight>
 
Using the above functions to produce the task results:
<langsyntaxhighlight OCamllang="ocaml">(* Wrapping up the parameters in a "cool" function: *)
let cool = euler (newton_cooling ~k:0.07 ~tr:20.)
 
Line 1,817 ⟶ 2,864:
results (cool ~step:10.)
results (cool ~step:5.)
results (cool ~step:2.)</langsyntaxhighlight>
 
Example output:
Line 1,839 ⟶ 2,886:
=={{header|Oforth}}==
 
<langsyntaxhighlight lang="oforth">: euler(f, y, a, b, h)
| t |
a b h step: t [
System.Out t <<wjp(6, JUSTIFY_RIGHT, 3) " : " << y << cr
t y f perform h * y + ->y
] ;</langsyntaxhighlight>
 
Usage :
 
<langsyntaxhighlight lang="oforth">: newtonCoolingLaw(t, y)
y 20 - -0.07 * ;
 
Line 1,854 ⟶ 2,901:
euler(#newtonCoolingLaw, 100.0, 0.0, 100.0, 2)
euler(#newtonCoolingLaw, 100.0, 0.0, 100.0, 5)
euler(#newtonCoolingLaw, 100.0, 0.0, 100.0, 10) ;</langsyntaxhighlight>
 
{{out}}
Line 1,871 ⟶ 2,918:
100 : 20.000472392
</pre>
 
=={{header|Ol}}==
{{trans|ObjectIcon}}
See also [[#Scheme|Scheme]].
 
The output is meant to be fed into [[Gnuplot]]. You can run the program like this: <code>ol name-you-saved-the-program-as.scm | gnuplot</code>
 
(Gnuplot may substitute a different font.)
 
<syntaxhighlight lang="Scheme">
(define (euler-method f y0 a b h)
;; Approximate y(t) in dy/dt=f(t,y), y(a)=y0, t going from a to b
;; with positive step size h. Produce a list of point pairs as
;; output.
(let loop ((t a)
(y y0)
(point-pairs '()))
(let ((point-pairs (cons (cons t y) point-pairs)))
(if (<= b t)
(reverse point-pairs)
(loop (+ t h) (+ y (* h (f t y))) point-pairs)))))
 
(define (newton-cooling-step t Temperature)
;; Newton's cooling law, with temperature in Celsius:
;;
;; f(t, Temperature) = -0.07*(Temperature - 20)
;;
(* -0.07 (- Temperature 20)))
 
(define data-for-stepsize=2
(euler-method newton-cooling-step 100.0 0.0 100.0 2.0))
 
(define data-for-stepsize=5
(euler-method newton-cooling-step 100.0 0.0 100.0 5.0))
 
(define data-for-stepsize=10
(euler-method newton-cooling-step 100.0 0.0 100.0 10.0))
 
(define (display-point-pairs point-pairs)
(let loop ((p point-pairs))
(if (pair? p)
(begin
(display (inexact (caar p)))
(display " ")
(display (inexact (cdar p)))
(newline)
(loop (cdr p))))))
 
(display "set encoding utf8") (newline)
(display "set term png size 1000,750 font 'Farao Book,16'") (newline)
(display "set output 'newton-cooling-Scheme.png'") (newline)
(display "set grid") (newline)
(display "set title 'Newton’s Law of Cooling'") (newline)
(display "set xlabel 'Elapsed time (seconds)'") (newline)
(display "set ylabel 'Temperature (Celsius)'") (newline)
(display "set xrange [0:100]") (newline)
(display "set yrange [15:100]") (newline)
(display "y(x) = 20.0 + (80.0 * exp (-0.07 * x))") (newline)
(display "plot y(x) with lines title 'Analytic solution', \\") (newline)
(display " '-' with linespoints title 'Euler method, step size 2s', \\") (newline)
(display " '-' with linespoints title 'Step size 5s', \\") (newline)
(display " '-' with linespoints title 'Step size 10s'") (newline)
(display-point-pairs data-for-stepsize=2)
(display "e") (newline)
(display-point-pairs data-for-stepsize=5)
(display "e") (newline)
(display-point-pairs data-for-stepsize=10)
(display "e") (newline)
</syntaxhighlight>
 
{{out}}
[[File:Euler method Scheme--newton-cooling.2023.04.26.10.30.19.png|thumb|none|alt=A plot of the cooling data, for all the cases.]]
 
=={{header|Pascal}}==
Line 1,877 ⟶ 2,996:
Euler code for Free Pascal - Delphi mode. Apart from the function-pointer calling convention for the NewtonCooling method, this example is ISO-7185 standard Pascal.
 
<syntaxhighlight lang="pascal">
<lang Pascal>
 
{$mode delphi}
Line 1,929 ⟶ 3,048:
END.
 
</syntaxhighlight>
</lang>
 
 
Line 1,944 ⟶ 3,063:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">sub euler_method {
my ($t0, $t1, $k, $step_size) = @_;
my @results = ( [0, $t0] );
Line 1,976 ⟶ 3,095:
$an;
}
</syntaxhighlight>
</lang>
Output:<pre>Time 2 err(%) 5 err(%) 10 err(%) Analytic
----------------------------------------------------------------------------
Line 1,990 ⟶ 3,109:
90 20.090 -0.281 20.034 -0.559 20.002 -0.721 20.147
100 20.042 -0.152 20.014 -0.291 20.000 -0.361 20.073</pre>
 
=={{header|Perl 6}}==
<lang perl6>sub euler ( &f, $y0, $a, $b, $h ) {
my $y = $y0;
my @t_y;
for $a, * + $h ... * > $b -> $t {
@t_y[$t] = $y;
$y += $h * f( $t, $y );
}
return @t_y;
}
 
constant COOLING_RATE = 0.07;
constant AMBIENT_TEMP = 20;
constant INITIAL_TEMP = 100;
constant INITIAL_TIME = 0;
constant FINAL_TIME = 100;
 
sub f ( $time, $temp ) {
return -COOLING_RATE * ( $temp - AMBIENT_TEMP );
}
 
my @e;
@e[$_] = euler( &f, INITIAL_TEMP, INITIAL_TIME, FINAL_TIME, $_ ) for 2, 5, 10;
 
say 'Time Analytic Step2 Step5 Step10 Err2 Err5 Err10';
 
for INITIAL_TIME, * + 10 ... * >= FINAL_TIME -> $t {
 
my $exact = AMBIENT_TEMP + (INITIAL_TEMP - AMBIENT_TEMP)
* (-COOLING_RATE * $t).exp;
 
my $err = sub { @^a.map: { 100 * abs( $_ - $exact ) / $exact } }
 
my ( $a, $b, $c ) = map { @e[$_][$t] }, 2, 5, 10;
 
say $t.fmt('%4d '), ( $exact, $a, $b, $c )».fmt(' %7.3f'),
$err.([$a, $b, $c])».fmt(' %7.3f%%');
}</lang>
 
Output:<pre>Time Analytic Step2 Step5 Step10 Err2 Err5 Err10
0 100.000 100.000 100.000 100.000 0.000% 0.000% 0.000%
10 59.727 57.634 53.800 44.000 3.504% 9.923% 26.331%
20 39.728 37.704 34.281 27.200 5.094% 13.711% 31.534%
30 29.797 28.328 26.034 22.160 4.927% 12.629% 25.629%
40 24.865 23.918 22.549 20.648 3.808% 9.313% 16.959%
50 22.416 21.843 21.077 20.194 2.555% 5.972% 9.910%
60 21.200 20.867 20.455 20.058 1.569% 3.512% 5.384%
70 20.596 20.408 20.192 20.017 0.912% 1.959% 2.808%
80 20.296 20.192 20.081 20.005 0.512% 1.057% 1.432%
90 20.147 20.090 20.034 20.002 0.281% 0.559% 0.721%
100 20.073 20.042 20.014 20.000 0.152% 0.291% 0.361%</pre>
 
=={{header|Phix}}==
{{Translibheader|CPhix/pGUI}}
{{libheader|Phix/online}}
<lang Phix>constant FMT = " %7.3f"
You can run this online [http://phix.x10.mx/p2js/euler_method.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Euler_method.exw
-- =============================
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">ivp_euler</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">end_t</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">end_t</span> <span style="color: #008080;">by</span> <span style="color: #000000;">step</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">y</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">step</span> <span style="color: #0000FF;">*</span> <span style="color: #7060A8;">call_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">analytic</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100</span> <span style="color: #008080;">by</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">20</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">80</span> <span style="color: #0000FF;">*</span> <span style="color: #7060A8;">exp</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">0.07</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">cooling</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000080;font-style:italic;">/*t*/</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">temp</span><span style="color: #0000FF;">)</span>
procedure ivp_euler(integer f, atom y, integer step, integer end_t)
<span style="color: #008080;">return</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.07</span> <span style="color: #0000FF;">*</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">temp</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">20</span><span style="color: #0000FF;">)</span>
integer t = 0;
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
printf(1, " Step %2d: ", step);
<span style="color: #008080;">constant</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span>
while t<=end_t do
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">analytic</span><span style="color: #0000FF;">(),</span>
if remainder(t,10)==0 then printf(1, FMT, y) end if
<span style="color: #000000;">e2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ivp_euler</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cooling</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span>
y += step * call_func(f,{t, y});
<span style="color: #000000;">e5</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ivp_euler</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cooling</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span>
t += step
<span style="color: #000000;">e10</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ivp_euler</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cooling</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" Time: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%7d"</span><span style="color: #0000FF;">)})</span>
printf(1, "\n");
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Analytic: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%7.3f"</span><span style="color: #0000FF;">)})</span>
end procedure
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" Step 2: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%7.3f"</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" Step 5: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%7.3f"</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" Step 10: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%7.3f"</span><span style="color: #0000FF;">)})</span>
<span style="color: #000080;font-style:italic;">-- and a simple plot</span>
procedure analytic()
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
printf(1, " Time: ");
<span style="color: #008080;">include</span> <span style="color: #7060A8;">IupGraph</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
for t = 0 to 100 by 10 do printf(1," %7g", t) end for
printf(1, "\nAnalytic: ");
for t = 0 to 100 by 10 do
printf(1, FMT, 20 + 80 * exp(-0.07 * t))
end for
printf(1,"\n");
end procedure
<span style="color: #008080;">function</span> <span style="color: #000000;">get_data</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*graph*/</span><span style="color: #0000FF;">)</span>
function cooling(atom /*t*/, atom temp)
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"NAMES"</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"analytical"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"h=2"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"h=5"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"h=10"</span><span style="color: #0000FF;">}},</span>
return -0.07 * (temp - 20);
<span style="color: #0000FF;">{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_BLUE</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e2</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_GREEN</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e5</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_BLACK</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e10</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_RED</span><span style="color: #0000FF;">}}</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant r_cooling = routine_id("cooling")
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
analytic();
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">graph</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGraph</span><span style="color: #0000FF;">(</span><span style="color: #000000;">get_data</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`RASTERSIZE=340x240,GRID=NO`</span><span style="color: #0000FF;">)</span>
ivp_euler(r_cooling, 100, 2, 100);
<span style="color: #7060A8;">IupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`XTICK=20,XMIN=0,XMAX=100,XMARGIN=25`</span><span style="color: #0000FF;">)</span>
ivp_euler(r_cooling, 100, 5, 100);
<span style="color: #7060A8;">IupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`YTICK=20,YMIN=20,YMAX=100`</span><span style="color: #0000FF;">)</span>
ivp_euler(r_cooling, 100, 10, 100);</lang>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Euler Method",MINSIZE=260x200`</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,088 ⟶ 3,181:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/math.l")
 
(de euler (F Y A B H)
Line 2,101 ⟶ 3,194:
(euler newtonCoolingLaw 100.0 0 100.0 2.0)
(euler newtonCoolingLaw 100.0 0 100.0 5.0)
(euler newtonCoolingLaw 100.0 0 100.0 10.0)</langsyntaxhighlight>
Output:
<pre>...
Line 2,116 ⟶ 3,209:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">test: procedure options (main); /* 3 December 2012 */
 
declare (x, y, z) float;
Line 2,159 ⟶ 3,252:
end f;
 
end test;</langsyntaxhighlight>
 
Only the final two outputs are shown, for brevity.
Line 2,200 ⟶ 3,293:
100 20.0729503632 20.0004730225
</pre>
 
 
=={{header|PowerShell}}==
{{works with|PowerShell|4.0}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function euler (${f}, ${y}, $y0, $t0, $tEnd) {
function f-euler ($tn, $yn, $h) {
Line 2,248 ⟶ 3,340:
}
euler f y $y0 $t0 $tEnd | Format-Table -AutoSize
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 2,267 ⟶ 3,359:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Define.d
Prototype.d Func(Time, t)
 
Line 2,291 ⟶ 3,383:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
<pre>...
85.000 20.053
Line 2,311 ⟶ 3,403:
=={{header|Python}}==
{{trans|Common Lisp}}
<langsyntaxhighlight lang="python">def euler(f,y0,a,b,h):
t,y = a,y0
while t <= b:
Line 2,322 ⟶ 3,414:
 
euler(newtoncooling,100,0,100,10)
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,335 ⟶ 3,427:
80.000 20.005
90.000 20.002
100.000 20.000
</pre>
 
=={{header|R}}==
{{trans|Python}}
<langsyntaxhighlight lang="rsplus">euler <- function(f, y0, a, b, h)
{
t <- a
Line 2,352 ⟶ 3,445:
}
 
newtoncooling <- function(time, temp){
return(-0.07*(temp-20))
}
 
euler(newtoncooling, 100, 0, 100, 10)</langsyntaxhighlight>
Output:
<pre> 0.000 100.000
Line 2,371 ⟶ 3,465:
 
The ODE solver:
<langsyntaxhighlight lang="racket">
(define (ODE-solve f init
#:x-max x-max
Line 2,378 ⟶ 3,472:
(reverse
(iterate-while (λ (x . y) (<= x x-max)) (method f h) init)))
</syntaxhighlight>
</lang>
 
It uses the default integration method <tt>euler</tt>, defined separately.
 
<langsyntaxhighlight lang="racket">
(define (euler F h)
(λ (x y) (list (+ x h) (+ y (* h (F x y))))))
</syntaxhighlight>
</lang>
 
A general-purpose procedure which evalutes a given function ''f'' repeatedly starting with argument ''x'', while all results satisfy a predicate ''test''. Returns a list of iterations.
 
<langsyntaxhighlight lang="racket">
(define (iterate-while test f x)
(let next ([result x]
Line 2,396 ⟶ 3,490:
(next (apply f result) (cons result list-of-results))
list-of-results)))
</syntaxhighlight>
</lang>
 
Textual output:
<langsyntaxhighlight lang="racket">
> (define (newton-cooling t T)
(* -0.07 (- T 20)))
Line 2,414 ⟶ 3,508:
(90 20.00157464)
(100 20.000472392))
</syntaxhighlight>
</lang>
 
Plotting results:
<langsyntaxhighlight lang="racket">
> (require plot)
> (plot
Line 2,427 ⟶ 3,521:
'(red blue black))
#:legend-anchor 'top-right)
</syntaxhighlight>
</lang>
[[File:euler1.jpg]]
 
Line 2,433 ⟶ 3,527:
[http://en.wikipedia.org/wiki/Midpoint_method 2-nd order Runge-Kutta method]:
 
<langsyntaxhighlight lang="racket">
(define (RK2 F h)
(λ (x y)
(list (+ x h) (+ y (* h (F (+ x (* 1/2 h))
(+ y (* 1/2 h (F x y)))))))))
</syntaxhighlight>
</lang>
 
[http://en.wikipedia.org/wiki/Adams_method#Two-step_Adams.E2.80.93Bashforth Two-step Adams–Bashforth method]
<langsyntaxhighlight lang="racket">
(define (adams F h)
(case-lambda
Line 2,449 ⟶ 3,543:
(let ([f (F x y)])
(list (+ x h) (+ y (* 3/2 h f) (* -1/2 h f′)) f))]))
</syntaxhighlight>
</lang>
 
[http://en.wikipedia.org/wiki/Adaptive_stepsize Adaptive one-step method] modifier using absolute accuracy ''ε''
<langsyntaxhighlight lang="racket">
(define ((adaptive method ε) F h0)
(case-lambda
Line 2,464 ⟶ 3,558:
(list x1 (+ y1 τ) (* 2 h′)))]))
 
</syntaxhighlight>
</lang>
 
Comparison of different integration methods
<langsyntaxhighlight lang="racket">
> (define (solve-newton-cooling-by m)
(ODE-solve newton-cooling '(0 100)
Line 2,486 ⟶ 3,580:
#:color 'blue #:label "Adaptive Runge-Kutta"))
#:legend-anchor 'top-right)
</syntaxhighlight>
</lang>
 
[[File:euler2.jpg]]
 
See also [[Runge-Kutta method#Racket]]
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub euler ( &f, $y0, $a, $b, $h ) {
my $y = $y0;
my @t_y;
for $a, * + $h ... * > $b -> $t {
@t_y[$t] = $y;
$y += $h × f( $t, $y );
}
@t_y
}
 
constant COOLING_RATE = 0.07;
constant AMBIENT_TEMP = 20;
constant INITIAL_TEMP = 100;
constant INITIAL_TIME = 0;
constant FINAL_TIME = 100;
 
sub f ( $time, $temp ) {
-COOLING_RATE × ( $temp - AMBIENT_TEMP )
}
 
my @e;
@e[$_] = euler( &f, INITIAL_TEMP, INITIAL_TIME, FINAL_TIME, $_ ) for 2, 5, 10;
 
say 'Time Analytic Step2 Step5 Step10 Err2 Err5 Err10';
 
for INITIAL_TIME, * + 10 ... * >= FINAL_TIME -> $t {
 
my $exact = AMBIENT_TEMP + (INITIAL_TEMP - AMBIENT_TEMP)
× (-COOLING_RATE × $t).exp;
 
my $err = sub { @^a.map: { 100 × ($_ - $exact).abs / $exact } }
 
my ( $a, $b, $c ) = map { @e[$_][$t] }, 2, 5, 10;
 
say $t.fmt('%4d '), ( $exact, $a, $b, $c )».fmt(' %7.3f'),
$err([$a, $b, $c])».fmt(' %7.3f%%');
}</syntaxhighlight>
 
Output:<pre>Time Analytic Step2 Step5 Step10 Err2 Err5 Err10
0 100.000 100.000 100.000 100.000 0.000% 0.000% 0.000%
10 59.727 57.634 53.800 44.000 3.504% 9.923% 26.331%
20 39.728 37.704 34.281 27.200 5.094% 13.711% 31.534%
30 29.797 28.328 26.034 22.160 4.927% 12.629% 25.629%
40 24.865 23.918 22.549 20.648 3.808% 9.313% 16.959%
50 22.416 21.843 21.077 20.194 2.555% 5.972% 9.910%
60 21.200 20.867 20.455 20.058 1.569% 3.512% 5.384%
70 20.596 20.408 20.192 20.017 0.912% 1.959% 2.808%
80 20.296 20.192 20.081 20.005 0.512% 1.057% 1.432%
90 20.147 20.090 20.034 20.002 0.281% 0.559% 0.721%
100 20.073 20.042 20.014 20.000 0.152% 0.291% 0.361%</pre>
 
=={{header|REXX}}==
===version 1===
{{trans|PLI}}
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* 24.05.2013 Walter Pachl translated from PL/I
**********************************************************************/
Line 2,567 ⟶ 3,714:
r=r+0
Return r
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,618 ⟶ 3,765:
===version 2===
This REXX version allows values to be specified via the command line (CL).
 
<br>It also shows the percentage difference (analytic vs. Euler's method) for each calculation.
<langsyntaxhighlight lang="rexx">/*REXX pgm solves example of Newton's cooling law via Euler's method (diff. step sizes).*/
e=2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138
numeric digits length( e() - 1) /*use the number of decimal digits in E*/
numeric digits length(e) - length(.) /*use the number of decimal digits in E*/
parse arg Ti Tr cc tt ss /*obtain optional arguments from the CL*/
if Ti=='' | Ti=="," then Ti= 100 /*given? Default: initial temp in ºC.*/
if Tr=='' | Tr=="," then Tr= 20 /* " " room " " " */
if cc=='' | cc=="," then cc= 0.07 /* " " cooling constant. */
if tt=='' | tt=="," then tt= 100 /* " " total time seconds. */
if ss ='' | ss ="," then ss=2 5 10 2 5 10 /* " " the step sizes. */
@= '═' /*the character used in title separator*/
do sSize=1 for words(ss); say; say; say center('time in' , 11)
say center('seconds' , 11, @) center('Euler method', 16, @) ,
center('analytic', 18, @) center('difference' , 14, @)
$=Ti; inc= word(ss,Ssize sSize) /*the 1st value; obtain the increment.*/
do t=0 to Ti by inc /*step through calculations by the inc.*/
a= format(Tr + (Ti-Tr)/exp(cc*t),6,910) /*calculate the analytic (exact) value.*/
say center(t,11) format($,6,3) 'ºC ' a "ºC" format(abs(a-$)/a*100,6,2) '%'
$= $ + inc * cc * (Tr-$) /*calc. next value via Euler's method. */
end /*t*/
end /*stepSizesSize*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
exp: procedure expose e; arg x; ix= x%1; if abs(x-ix)>.5 then ix=ix+sign(x); x= x-ix; z=1
e: return 2.718281828459045235360287471352662497757247093699959574966967627724076630353548
_=1; w=1; do j=1; _= _*x/j; z= (z+_)/1; if z==w then leave; w=z
/*──────────────────────────────────────────────────────────────────────────────────────*/
end /*j*/; if z\==0 then z= e**ix * z; return z</syntaxhighlight>
exp: procedure; parse arg x; ix=x%1; if abs(x-ix)>.5 then ix=ix+sign(x); x=x-ix; z=1
_=1; w=1; do j=1; _=_*x/j; z=(z+_)/1; if z==w then leave; w=z; end /*j*/
if z\==0 then z=e()**ix * z; return z</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
time in
══seconds══ ══Euler method══ ═════analytic═════ ══difference══
0 100.000 ºC 100.0000000000000000000 ºC 0.00 %
2 88.800 ºC 89.5486588805486588319 ºC 0.84 %
4 79.168 ºC 80.4626992904626993165 ºC 1.61 %
6 70.884 ºC 72.5637456105637455852 ºC 2.31 %
8 63.761 ºC 65.6967251206967251079 ºC 2.95 %
10 57.634 ºC 59.7268243107268243033 ºC 3.50 %
12 52.365 ºC 54.5368418905368418743 ºC 3.98 %
14 47.834 ºC 50.0248879100248879081 ºC 4.38 %
16 43.937 ºC 46.1023835901023835698 ºC 4.70 %
18 40.586 ºC 42.6923221206923221200 ºC 4.93 %
20 37.704 ºC 39.7277571107277571153 ºC 5.09 %
22 35.226 ºC 37.1504881101504881142 ºC 5.18 %
24 33.094 ºC 34.9099180809099180832 ºC 5.20 %
26 31.261 ºC 32.9620600709620600747 ºC 5.16 %
28 29.684 ºC 31.2686736702686736737 ºC 5.07 %
30 28.328 ºC 29.7965142607965142602 ºC 4.93 %
32 27.163 ºC 28.5166803505166803503 ºC 4.75 %
34 26.160 ºC 27.4040462004040462008 ºC 4.54 %
36 25.297 ºC 26.4367685404367685400 ºC 4.31 %
38 24.556 ºC 25.5958577405958577396 ºC 4.06 %
40 23.918 ºC 24.8648050108648050100 ºC 3.81 %
42 23.369 ºC 24.2292583002292582991 ºC 3.55 %
44 22.898 ºC 23.6767405406767405319 ºC 3.29 %
46 22.492 ºC 23.1964046601964046609 ºC 3.04 %
48 22.143 ºC 22.7788207207788207156 ºC 2.79 %
50 21.843 ºC 22.4157906804157906738 ºC 2.55 %
52 21.585 ºC 22.1001875201001875173 ºC 2.33 %
54 21.363 ºC 21.8258153108258153140 ºC 2.12 %
56 21.172 ºC 21.5872875805872875795 ºC 1.92 %
58 21.008 ºC 21.3799215303799215292 ºC 1.74 %
60 20.867 ºC 21.1996461501996461456 ºC 1.57 %
62 20.746 ºC 21.0429222600429222563 ºC 1.41 %
64 20.641 ºC 20.9066730509066730524 ºC 1.27 %
66 20.551 ºC 20.7882236807882236849 ºC 1.14 %
68 20.474 ºC 20.6852487506852487518 ºC 1.02 %
70 20.408 ºC 20.5957266505957266457 ºC 0.91 %
72 20.351 ºC 20.5178998705178998655 ºC 0.81 %
74 20.302 ºC 20.4502405104502405132 ºC 0.73 %
76 20.259 ºC 20.3914203003914202980 ºC 0.65 %
78 20.223 ºC 20.3402844603402844596 ºC 0.58 %
80 20.192 ºC 20.2958291002958290973 ºC 0.51 %
82 20.165 ºC 20.2571814602571814620 ºC 0.45 %
84 20.142 ºC 20.2235828202235828220 ºC 0.40 %
86 20.122 ºC 20.1943735701943735676 ºC 0.36 %
88 20.105 ºC 20.1689802601689802617 ºC 0.32 %
90 20.090 ºC 20.1469043801469043822 ºC 0.28 %
92 20.078 ºC 20.1277125301277125344 ºC 0.25 %
94 20.067 ºC 20.1110279401110279436 ºC 0.22 %
96 20.057 ºC 20.0965230600965230571 ºC 0.19 %
98 20.049 ºC 20.0839131100839131146 ºC 0.17 %
100 20.042 ºC 20.0729505600729505572 ºC 0.15 %
 
 
time in
══seconds══ ══Euler method══ ═════analytic═════ ══difference══
0 100.000 ºC 100.0000000000000000000 ºC 0.00 %
5 72.000 ºC 76.3750472003750471775 ºC 5.73 %
10 53.800 ºC 59.7268243107268243033 ºC 9.92 %
15 41.970 ºC 47.9950199409950199289 ºC 12.55 %
20 34.281 ºC 39.7277571107277571153 ºC 13.71 %
25 29.282 ºC 33.9019154809019154760 ºC 13.63 %
30 26.034 ºC 29.7965142607965142602 ºC 12.63 %
35 23.922 ºC 26.9034869209034869199 ºC 11.08 %
40 22.549 ºC 24.8648050108648050100 ºC 9.31 %
45 21.657 ºC 23.4281701504281701494 ºC 7.56 %
50 21.077 ºC 22.4157906804157906738 ºC 5.97 %
55 20.700 ºC 21.7023789207023789151 ºC 4.62 %
60 20.455 ºC 21.1996461501996461456 ºC 3.51 %
65 20.296 ºC 20.8453763508453763507 ºC 2.64 %
70 20.192 ºC 20.5957266505957266457 ºC 1.96 %
75 20.125 ºC 20.4198014704198014719 ºC 1.44 %
80 20.081 ºC 20.2958291002958290973 ºC 1.06 %
85 20.053 ºC 20.2084672402084672415 ºC 0.77 %
90 20.034 ºC 20.1469043801469043822 ºC 0.56 %
95 20.022 ºC 20.1035217701035217684 ºC 0.40 %
100 20.014 ºC 20.0729505600729505572 ºC 0.29 %
 
 
time in
══seconds══ ══Euler method══ ═════analytic═════ ══difference══
0 100.000 ºC 100.0000000000000000000 ºC 0.00 %
10 44.000 ºC 59.7268243107268243033 ºC 26.33 %
20 27.200 ºC 39.7277571107277571153 ºC 31.53 %
30 22.160 ºC 29.7965142607965142602 ºC 25.63 %
40 20.648 ºC 24.8648050108648050100 ºC 16.96 %
50 20.194 ºC 22.4157906804157906738 ºC 9.91 %
60 20.058 ºC 21.1996461501996461456 ºC 5.38 %
70 20.017 ºC 20.5957266505957266457 ºC 2.81 %
80 20.005 ºC 20.2958291002958290973 ºC 1.43 %
90 20.002 ºC 20.1469043801469043822 ºC 0.72 %
100 20.000 ºC 20.0729505600729505572 ºC 0.36 %
</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(3)
see euler("return -0.07*(y-20)", 100, 0, 100, 2) + nl
Line 2,756 ⟶ 3,903:
t += s
end
return y</langsyntaxhighlight>
Output:
<pre>
Line 2,766 ⟶ 3,913:
10 57.634
</pre>
 
=={{header|RPL}}==
This is a typical task for which RPL was designed.
(t<sub>n</sub>,y<sub>n</sub>) are handled as complex numbers. This makes the iterations easier to calculate and facilitates the eventual display of the resulting curve, as RPL uses this data format to designate the pixels.
≪ → t temp '-'''K'''*(temp-'''TR''')' ≫ ‘'''F'''’ STO
≪ → h fn
≪ 1 10 '''START'''
DUP h OVER C→R fn EVAL h * R→C +
'''NEXT''' 10 →LIST
≫ ≫ ‘'''EULER'''’ STO
0.07 ''''K'''' STO 20 ''''TR'''' STO
 
(0,100) 2 ''''F'''' '''EULER'''
{{out}}
<pre>
1: { (2,88.8) (4,79.168) (6,70.88448) (8,63.7606528) (10,57.634161408) (12,52.3653788109) (14,47.8342257774) (16,43.9374341685) (18,40.5861933849) (20,37.704126311) }
</pre>
===Analytical solution===
≪ { } 2 20 '''FOR''' t t ''''TR'''+(100-'''TR''')*EXP(-'''K'''*t)' EVAL R→C + '''NEXT''' ≫
{{out}}
<pre>
1: { (2,89.5486588319) (3,84.8467396776) (4,80.4626993165) (5,76.3750471775) (6,72.5637455852) (7,69.0101115348) (8,65.6967251079) (9,62.6073440806) (10,59.7268243033) (11,57.0410454649) (12,54.5368418743) (13,52.2019379227) (14,50.0248879081) (15,47.9950199289) (16,46.1023835698) (17,44.3377011253) (18,42.69232212) (19,41.158180904) (20,39.7277571153) }
</pre>
Accuracy to the degree for the first seconds of cooling needs h to be set at 0.2 s or below
 
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">def euler(y, a, b, h)
a.step(b,h) do |t|
puts "%7.3f %7.3f" % [t,y]
Line 2,780 ⟶ 3,953:
euler(100,0,100,step) {|time, temp| -0.07 * (temp - 20) }
puts
end</langsyntaxhighlight>
{{out}}
<pre style="height: 40ex; overflow: scroll">
Line 2,875 ⟶ 4,048:
=={{header|Rust}}==
{{trans|Kotlin}}
<langsyntaxhighlight Rustlang="rust">fn header() {
print!(" Time: ");
for t in (0..100).step_by(10) {
Line 2,908 ⟶ 4,081:
euler(|temp| -0.07 * (temp - 20.0), 100.0, i, 100);
}
}</langsyntaxhighlight>
{{out}}
<pre> Time: 0 10 20 30 40 50 60 70 80 90
Line 2,918 ⟶ 4,091:
=={{header|Scala}}==
 
<langsyntaxhighlight lang="scala">
object App{
 
Line 2,948 ⟶ 4,121:
 
}
</syntaxhighlight>
</lang>
 
Output for step = 10;
Line 2,966 ⟶ 4,139:
</pre>
 
=={{header|Scheme}}==
See [[#Ol|Ol]]. That implementation is valid Scheme.
 
In many Scheme implementations (Chez Scheme, Gauche Scheme, Gambit Scheme, CHICKEN Scheme if run with "-R r7rs", etc.), the program will run without modification. For some other implementations, the call to <code>inexact</code> must be either removed or changed to <code>exact->inexact</code>. (The call is necessary for ol, which otherwise would have written fractions that gnuplot does not understand.)
 
=={{header|SequenceL}}==
 
<langsyntaxhighlight lang="sequencel">import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
 
Line 2,993 ⟶ 4,170:
output when x > n
else
euler(f, newY, n, h, newX, newOutput);</langsyntaxhighlight>
Based on C# version [http://rosettacode.org/wiki/Euler_method#C.23] but using tail recursion instead of looping.
{{out}}
Line 3,016 ⟶ 4,193:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func euler_method(t0, t1, k, step_size) {
var results = [[0, t0]]
for s in (step_size..100 -> by(step_size)) {
Line 3,045 ⟶ 4,222:
r10[i][1], (r10[i][1] / an) * 100 - 100,
an)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,064 ⟶ 4,241:
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk">ODESolver>>eulerOf: f init: y0 from: a to: b step: h
| t y |
t := a.
Line 3,077 ⟶ 4,254:
 
ODESolver new eulerOf: [:time :temp| -0.07 * (temp - 20)] init: 100 from: 0 to: 100 step: 10
</syntaxhighlight>
</lang>
Transcript:
<pre>
Line 3,090 ⟶ 4,267:
80 20.005
90 20.002
</pre>
 
=={{header|Standard ML}}==
{{trans|Ol}}
{{works with|Poly/ML|5.9}}
{{works with|MLton|20210117}}
 
This program outputs commands for [[Gnuplot]], which will produce a PNG. Run the program with a command such as <code>poly --script name_you_gave_the_file.sml | gnuplot</code>.
 
<syntaxhighlight lang="sml">
(* Approximate y(t) in dy/dt=f(t,y), y(a)=y0, t going from a to b with
positive step size h. Produce a list of point pairs as output. *)
fun eulerMethod (f, y0, a, b, h) =
let
fun loop (t, y, pointPairs) =
let
val pointPairs = (t, y) :: pointPairs
in
if b <= t then
rev pointPairs
else
loop (t + h, y + (h * f (t, y)), pointPairs)
end
in
loop (a, y0, nil)
end
 
(* How to step temperature according to Newton's law of cooling. *)
fun f (t, temp) = ~0.07 * (temp - 20.0)
 
val data2 = eulerMethod (f, 100.0, 0.0, 100.0, 2.0)
and data5 = eulerMethod (f, 100.0, 0.0, 100.0, 5.0)
and data10 = eulerMethod (f, 100.0, 0.0, 100.0, 10.0)
 
fun printPointPairs pointPairs =
app (fn (t, y) => (print (Real.toString t);
print " ";
print (Real.toString y);
print "\n"))
pointPairs
 
;
 
print ("set encoding utf8\n");
print ("set term png size 1000,750 font 'Brioso Pro,16'\n");
print ("set output 'newton-cooling-SML.png'\n");
print ("set grid\n");
print ("set title 'Newton\\U+2019s Law of Cooling'\n");
print ("set xlabel 'Elapsed time (seconds)'\n");
print ("set ylabel 'Temperature (Celsius)'\n");
print ("set xrange [0:100]\n");
print ("set yrange [15:100]\n");
print ("y(x) = 20.0 + (80.0 * exp (-0.07 * x))\n");
print ("plot y(x) with lines title 'Analytic solution', \\\n");
print (" '-' with linespoints title 'Euler method, step size 2s', \\\n");
print (" '-' with linespoints title 'Step size 5s', \\\n");
print (" '-' with linespoints title 'Step size 10s'\n");
printPointPairs data2;
print ("e\n");
printPointPairs data5;
print ("e\n");
printPointPairs data10;
print ("e\n");
</syntaxhighlight>
 
{{out}}
[[File:Euler method SML--newton-cooling.2023.04.26.14.07.02.png|thumb|none|alt=The output of the Standard ML program, as plotted by Gnuplot.]]
 
=={{header|Swift}}==
{{trans|C}}
<syntaxhighlight lang="swift">import Foundation
 
let numberFormat = " %7.3f"
let k = 0.07
let initialTemp = 100.0
let finalTemp = 20.0
let startTime = 0
let endTime = 100
 
func ivpEuler(function: (Double, Double) -> Double, initialValue: Double, step: Int) {
print(String(format: " Step %2d: ", step), terminator: "")
var y = initialValue
for t in stride(from: startTime, through: endTime, by: step) {
if t % 10 == 0 {
print(String(format: numberFormat, y), terminator: "")
}
y += Double(step) * function(Double(t), y)
}
print()
}
 
func analytic() {
print(" Time: ", terminator: "")
for t in stride(from: startTime, through: endTime, by: 10) {
print(String(format: " %7d", t), terminator: "")
}
print("\nAnalytic: ", terminator: "")
for t in stride(from: startTime, through: endTime, by: 10) {
let temp = finalTemp + (initialTemp - finalTemp) * exp(-k * Double(t))
print(String(format: numberFormat, temp), terminator: "")
}
print()
}
 
func cooling(t: Double, temp: Double) -> Double {
return -k * (temp - finalTemp)
}
 
analytic()
ivpEuler(function: cooling, initialValue: initialTemp, step: 2)
ivpEuler(function: cooling, initialValue: initialTemp, step: 5)
ivpEuler(function: cooling, initialValue: initialTemp, step: 10)</syntaxhighlight>
 
{{out}}
<pre>
Time: 0 10 20 30 40 50 60 70 80 90 100
Analytic: 100.000 59.727 39.728 29.797 24.865 22.416 21.200 20.596 20.296 20.147 20.073
Step 2: 100.000 57.634 37.704 28.328 23.918 21.843 20.867 20.408 20.192 20.090 20.042
Step 5: 100.000 53.800 34.280 26.034 22.549 21.077 20.455 20.192 20.081 20.034 20.014
Step 10: 100.000 44.000 27.200 22.160 20.648 20.194 20.058 20.017 20.005 20.002 20.000
</pre>
 
=={{header|Tcl}}==
{{trans|C++}}
<langsyntaxhighlight lang="tcl">proc euler {f y0 a b h} {
puts "computing $f over \[$a..$b\], step $h"
set y [expr {double($y0)}]
Line 3,102 ⟶ 4,399:
}
puts "done"
}</langsyntaxhighlight>
Demonstration with the Newton Cooling Law:
<langsyntaxhighlight lang="tcl">proc newtonCoolingLaw {time temp} {
expr {-0.07 * ($temp - 20)}
}
Line 3,110 ⟶ 4,407:
euler newtonCoolingLaw 100 0 100 2
euler newtonCoolingLaw 100 0 100 5
euler newtonCoolingLaw 100 0 100 10</langsyntaxhighlight>
End of output:
<pre>
Line 3,126 ⟶ 4,423:
90.000 20.002
done
</pre>
 
=={{header|Uiua}}==
'''Solution:'''
<syntaxhighlight lang="Uiua">
"Euler Solution"
T ← 100 # initial starting temp
TR ← 20 # room temp
TMINUSTR ← - TR T
h ← 10 # step size
k ← 0.07 # coefficent
TEND ← 100 # end time
n ← ÷ h 100 # steps
# inital starting point
T
.
# .. clone the top of stack and take if for next step
# repeat the steps n times with ⍥
Solution ← [⍥(.. - × h × k - TR)]+ n 1
⇌ ⊂ Solution T
 
# analytical solution
"Analytical Solution"
# apply function to LIST
List ← × k × h ⇡n
# Analytical solution applied
+ TR × TMINUSTR ⁿ ¯List e
 
</syntaxhighlight>
'''Example:'''
<pre>
"Euler Solution"
[100 43.99999999999999 27.199999999999996 22.159999999999997 20.648 20.194399999999998 20.05832 20.017496 20.0052488 20.00157464 20.000472392 20.0001417176 20.0001417176 20.0001417176]
 
"Analytical Solution"
[100 59.72682430331276 39.727757115328515 29.796514260238553 24.864805010017434 22.41579067378548 21.199646145638216 20.59572664567395 20.295829097318634 20.14690438216231]
</pre>
 
=={{header|VBA}}==
{{trans|Phix}}<langsyntaxhighlight lang="vb">Private Sub ivp_euler(f As String, y As Double, step As Integer, end_t As Integer)
Dim t As Integer
Debug.Print " Step "; step; ": ",
Line 3,164 ⟶ 4,497:
ivp_euler r_cooling, 100, 5, 100
ivp_euler r_cooling, 100, 10, 100
End Sub</langsyntaxhighlight>{{out}}
<pre> Time: 0 10 20 30 40 50 60 70 80 90 100
Analytic: 100,000 59,727 39,728 29,797 24,865 22,416 21,200 20,596 20,296 20,147 20,073
Line 3,170 ⟶ 4,503:
Step 5 : 100,000 53,800 34,281 26,034 22,549 21,077 20,455 20,192 20,081 20,034 20,014
Step 10 : 100,000 44,000 27,200 22,160 20,648 20,194 20,058 20,017 20,005 20,002 20,000 </pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="go">import math
// Fdy is a type for fntion f used in Euler's method.
type Fdy = fn(f64, f64) f64
// euler_step computes a single new value using Euler's method.
// Note that step size h is a parameter, so a variable step size
// could be used.
fn euler_step(f Fdy, x f64, y f64, h f64) f64 {
return y + h*f(x, y)
}
// Definition of cooling rate. Note that this has general utility and
// is not specific to use in Euler's method.
// new_cooling_rate returns a fntion that computes cooling rate
// for a given cooling rate constant k.
fn new_cooling_rate(k f64) fn(f64) f64 {
return fn[k](delta_temp f64) f64 {
return -k * delta_temp
}
}
// new_temp_func returns a fntion that computes the analytical solution
// of cooling rate integrated over time.
fn new_temp_func(k f64, ambient_temp f64, initial_temp f64) fn(f64) f64 {
return fn[ambient_temp,initial_temp,k](time f64) f64 {
return ambient_temp + (initial_temp-ambient_temp)*math.exp(-k*time)
}
}
// new_cooling_rate_dy returns a fntion of the kind needed for Euler's method.
// That is, a fntion representing dy(x, y(x)).
//
// Parameters to new_cooling_rate_dy are cooling constant k and ambient
// temperature.
fn new_cooling_rate_dy(k f64, ambient_temp f64) Fdy {
// note that result is dependent only on the object temperature.
// there are no additional dependencies on time, so the x parameter
// provided by euler_step is unused.
return fn[k,ambient_temp](_ f64, object_temp f64) f64 {
return new_cooling_rate(k)(object_temp - ambient_temp)
}
}
fn main() {
k := .07
temp_room := 20.0
temp_object := 100.0
fcr := new_cooling_rate_dy(k, temp_room)
analytic := new_temp_func(k, temp_room, temp_object)
for delta_time in [2.0, 5, 10] {
println("Step size = ${delta_time:.1f}")
println(" Time Euler's Analytic")
mut temp := temp_object
for time := 0.0; time <= 100; time += delta_time {
println("${time:5.1f} ${temp:7.3f} ${analytic(time):7.3f}")
temp = euler_step(fcr, time, temp, delta_time)
}
println('')
}
}</syntaxhighlight>
Output, truncated:
<pre>
...
85.0 20.053 20.208
90.0 20.034 20.147
95.0 20.022 20.104
100.0 20.014 20.073
 
Step size = 10.0
Time Euler's Analytic
0.0 100.000 100.000
10.0 44.000 59.727
20.0 27.200 39.728
30.0 22.160 29.797
40.0 20.648 24.865
50.0 20.194 22.416
60.0 20.058 21.200
70.0 20.017 20.596
80.0 20.005 20.296
90.0 20.002 20.147
100.0 20.000 20.073
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
proc Euler(Step); \Display cooling temperatures using Euler's method
Line 3,196 ⟶ 4,616:
 
Format(3,2); \display cooling temps using differential eqn.
Text(0, "Dif eq "); \ dTemp(time)/dtime = -k*Temp�Temp
Time:= 0.0;
while Time <= 100.1 do
Line 3,208 ⟶ 4,628:
Euler(5);
Euler(10);
]</langsyntaxhighlight>
 
Output:
Line 3,217 ⟶ 4,637:
Step 5 100.00 53.80 34.28 26.03 22.55 21.08 20.46 20.19 20.08 20.03 20.01
Step 10 100.00 44.00 27.20 22.16 20.65 20.19 20.06 20.02 20.01 20.00 20.00
</pre>
 
=={{header|Wren}}==
{{trans|C}}
{{libheader|Wren-fmt}}
{{libheader|Wren-iterate}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
import "./iterate" for Stepped
 
var euler = Fn.new { |f, y, step, end|
Fmt.write(" Step $2d: ", step)
for (t in Stepped.new(0..end, step)) {
if (t%10 == 0) Fmt.write(" $7.3f", y)
y = y + step * f.call(y)
}
System.print()
}
 
var analytic = Fn.new {
System.write(" Time: ")
for (t in Stepped.new(0..100, 10)) Fmt.write(" $7d", t)
System.write("\nAnalytic: ")
for (t in Stepped.new(0..100, 10)) {
Fmt.write(" $7.3f", 20 + 80 * (-0.07*t).exp)
}
System.print()
}
var cooling = Fn.new { |temp| -0.07 * (temp - 20) }
 
analytic.call()
for (i in [2, 5, 10]) euler.call(cooling, 100, i, 100)</syntaxhighlight>
 
{{out}}
<pre>
Time: 0 10 20 30 40 50 60 70 80 90 100
Analytic: 100.000 59.727 39.728 29.797 24.865 22.416 21.200 20.596 20.296 20.147 20.073
Step 2: 100.000 57.634 37.704 28.328 23.918 21.843 20.867 20.408 20.192 20.090 20.042
Step 5: 100.000 53.800 34.280 26.034 22.549 21.077 20.455 20.192 20.081 20.034 20.014
Step 10: 100.000 44.000 27.200 22.160 20.648 20.194 20.058 20.017 20.005 20.002 20.000
</pre>
 
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">const FMT=" %7.3f";
fcn ivp_euler(f,y,step,end_t){
Line 3,245 ⟶ 4,704:
ivp_euler(cooling, 100.0, 2, 100);
ivp_euler(cooling, 100.0, 5, 100);
ivp_euler(cooling, 100.0, 10, 100);</langsyntaxhighlight>
{{out}}
<pre>
Line 3,257 ⟶ 4,716:
=={{header|ZX Spectrum Basic}}==
{{trans|BBC_BASIC}}
<langsyntaxhighlight lang="zxbasic">10 LET d$="-0.07*(y-20)": LET y=100: LET a=0: LET b=100: LET s=10
20 LET t=a
30 IF t<=b THEN PRINT t;TAB 10;y: LET y=y+s*VAL d$: LET t=t+s: GO TO 30</langsyntaxhighlight>
2,130

edits