Euler method: Difference between revisions

Added Xbasic
m (→‎{{header|Wren}}: Minor tidy)
(Added Xbasic)
 
(7 intermediate revisions by 4 users not shown)
Line 383:
 
=={{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}}===
<syntaxhighlight lang="bbcbasic"> PROCeuler("-0.07*(y-20)", 100, 0, 100, 2)
Line 420 ⟶ 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}}===
Line 556 ⟶ 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}}===
Line 592 ⟶ 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}}==
Line 968 ⟶ 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}}==
Line 2,116 ⟶ 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}}==
Line 3,913 ⟶ 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>
 
2,130

edits