Euler method: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 1,731:
 
=={{header|Maple}}==
Build-in function with Euler:
<lang Maple> with(Student[NumericalAnalysis]);
with(Student[NumericalAnalysis]);
k := 0.07:
TR := 20:
Line 1,741 ⟶ 1,740:
{{out}}
<pre>
20.04
 
20.01
20.04
20.00
20.01
20.00
</pre>
Hard-coded procedure:
<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);</lang>
{{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}}==
Anonymous user