Jump to content

Euler method: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 161:
100: 2.00005E+01
</pre>
 
=={{header|ALGOL 68}}==
{{trans|D}} Note: This specimen retains the original [[#D|D]] coding style.
Line 407 ⟶ 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 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++}}==
Line 457 ⟶ 498:
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}}==
Line 531 ⟶ 533:
80 20.0052488
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>
 
Line 597 ⟶ 639:
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>
 
Line 1,517 ⟶ 1,519:
eulersMethod(cooling, 0, 100, 100, 10);
</lang>
 
=={{header|jq}}==
{{works with|jq|1.4}}
Line 2,050 ⟶ 2,053:
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}}==
Line 2,260 ⟶ 2,211:
100 20.0729503632 20.0004730225
</pre>
 
 
=={{header|PowerShell}}==
Line 2,552 ⟶ 2,502:
 
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}}==
Line 3,026 ⟶ 3,029:
DONE
</pre>
 
 
=={{header|SequenceL}}==
Line 3,231 ⟶ 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 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}}==
<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
Line 3,257 ⟶ 3,260:
 
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
10,339

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.