Euler method: Difference between revisions

Added Xbasic
(Add MATLAB implementation)
(Added Xbasic)
 
(5 intermediate revisions by 3 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}}==
2,130

edits