Skip to content

Commit e347d67

Browse files
depateleios
authored andcommitted
Euler fortran (#411)
* Delete monte_carlo_pi.f90 * init * further implementation * next * forward euler implementation * revoked merge changed from bubble.f90 * Gitbook documentation * fix fortran highlight
1 parent 0fb0325 commit e347d67

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
PROGRAM euler
2+
3+
IMPLICIT NONE
4+
LOGICAL :: is_approx
5+
REAL(8), DIMENSION(:), ALLOCATABLE :: vec
6+
REAL(8) :: time_step, threshold
7+
INTEGER :: n
8+
9+
time_step = 0.01d0
10+
n = 100
11+
threshold = 0.01d0
12+
13+
ALLOCATE(vec(n))
14+
CALL forward_euler(time_step, n, vec)
15+
is_approx = check_result(vec, threshold, time_step)
16+
17+
WRITE(*,*) is_approx
18+
19+
DEALLOCATE(vec)
20+
21+
CONTAINS
22+
23+
SUBROUTINE forward_euler(time_step, n, vec)
24+
25+
IMPLICIT NONE
26+
REAL(8), DIMENSION(:), INTENT(OUT) :: vec
27+
REAL(8), INTENT(IN) :: time_step
28+
INTEGER, INTENT(IN) :: n
29+
INTEGER :: i
30+
31+
vec(1) = 1d0
32+
33+
DO i=1, n-1
34+
35+
vec(i+1) = vec(i) - 3d0 * vec(i) * time_step
36+
37+
END DO
38+
END SUBROUTINE
39+
40+
LOGICAL FUNCTION check_result(euler_result, threshold, time_step)
41+
42+
IMPLICIT NONE
43+
REAL(8), DIMENSION(:), INTENT(IN) :: euler_result
44+
REAL(8), INTENT(IN) :: threshold, time_step
45+
REAL(8) :: time, solution
46+
INTEGER :: i
47+
48+
check_result = .TRUE.
49+
50+
DO i = 1, SIZE(euler_result)
51+
52+
time = (i - 1) * time_step
53+
solution = EXP(-3d0 * time)
54+
55+
IF (ABS(euler_result(i) - solution) > threshold) THEN
56+
57+
WRITE(*,*) euler_result(i), solution
58+
check_result = .FALSE.
59+
60+
END IF
61+
END DO
62+
END FUNCTION
63+
END PROGRAM euler
64+

contents/forward_euler_method/forward_euler_method.md

+2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ Full code for the visualization follows:
121121
[import, lang:"matlab"](code/matlab/euler.m)
122122
{% sample lang="swift" %}
123123
[import, lang:"swift"](code/swift/euler.swift)
124+
{% sample lang="f90" %}
125+
[import, lang:"fortran"](code/fortran/euler.f90)
124126
{% endmethod %}
125127

126128
<script>

0 commit comments

Comments
 (0)