Euler method: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 161: | Line 161: | ||
100: 2.00005E+01 |
100: 2.00005E+01 |
||
</pre> |
</pre> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{trans|D}} Note: This specimen retains the original [[#D|D]] coding style. |
{{trans|D}} Note: This specimen retains the original [[#D|D]] coding style. |
||
Line 407: | Line 408: | ||
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 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</lang> |
Step 10: 100.000 44.000 27.200 22.160 20.648 20.194 20.058 20.017 20.005 20.002 20.000</lang> |
||
=={{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|C++}}== |
=={{header|C++}}== |
||
Line 457: | Line 498: | ||
done |
done |
||
</pre> |
</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}}== |
=={{header|Clay}}== |
||
Line 531: | Line 533: | ||
80 20.0052488 |
80 20.0052488 |
||
90 20.00157464 |
90 20.00157464 |
||
</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> |
</pre> |
||
Line 597: | Line 639: | ||
x = 80.0000, y = 20.0052 |
x = 80.0000, y = 20.0052 |
||
x = 90.0000, y = 20.0016 |
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> |
</pre> |
||
Line 1,517: | Line 1,519: | ||
eulersMethod(cooling, 0, 100, 100, 10); |
eulersMethod(cooling, 0, 100, 100, 10); |
||
</lang> |
</lang> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq|1.4}} |
{{works with|jq|1.4}} |
||
Line 2,050: | Line 2,053: | ||
90 20.090 -0.281 20.034 -0.559 20.002 -0.721 20.147 |
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> |
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}}== |
=={{header|Phix}}== |
||
Line 2,260: | Line 2,211: | ||
100 20.0729503632 20.0004730225 |
100 20.0729503632 20.0004730225 |
||
</pre> |
</pre> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Line 2,552: | Line 2,502: | ||
See also [[Runge-Kutta method#Racket]] |
See also [[Runge-Kutta method#Racket]] |
||
=={{header|Raku}}== |
|||
(formerly 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|REXX}}== |
=={{header|REXX}}== |
||
Line 3,026: | Line 3,029: | ||
DONE |
DONE |
||
</pre> |
</pre> |
||
=={{header|SequenceL}}== |
=={{header|SequenceL}}== |
||
Line 3,231: | Line 3,233: | ||
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 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> |
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|XPL0}}== |
=={{header|XPL0}}== |
||
<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations |
<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations |
||
Line 3,257: | Line 3,260: | ||
Format(3,2); \display cooling temps using differential eqn. |
Format(3,2); \display cooling temps using differential eqn. |
||
Text(0, "Dif eq "); \ dTemp(time)/dtime = -k* |
Text(0, "Dif eq "); \ dTemp(time)/dtime = -k*�Temp |
||
Time:= 0.0; |
Time:= 0.0; |
||
while Time <= 100.1 do |
while Time <= 100.1 do |