diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 81860ae5f..6cac82567 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -139,7 +139,7 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout {% sample lang="nim" %} [import, lang="nim" %](code/nim/euclid_algorithm.nim) {% sample lang="f90" %} -[import, lang="Fortran"](code/fortran/euclidean.f90) +[import, lang="fortran"](code/fortran/euclidean.f90) {% endmethod %} diff --git a/contents/forward_euler_method/code/fortran/euler.f90 b/contents/forward_euler_method/code/fortran/euler.f90 new file mode 100644 index 000000000..5a7c8332e --- /dev/null +++ b/contents/forward_euler_method/code/fortran/euler.f90 @@ -0,0 +1,64 @@ +PROGRAM euler + + IMPLICIT NONE + LOGICAL :: is_approx + REAL(8), DIMENSION(:), ALLOCATABLE :: vec + REAL(8) :: time_step, threshold + INTEGER :: n + + time_step = 0.01d0 + n = 100 + threshold = 0.01d0 + + ALLOCATE(vec(n)) + CALL forward_euler(time_step, n, vec) + is_approx = check_result(vec, threshold, time_step) + + WRITE(*,*) is_approx + + DEALLOCATE(vec) + +CONTAINS + + SUBROUTINE forward_euler(time_step, n, vec) + + IMPLICIT NONE + REAL(8), DIMENSION(:), INTENT(OUT) :: vec + REAL(8), INTENT(IN) :: time_step + INTEGER, INTENT(IN) :: n + INTEGER :: i + + vec(1) = 1d0 + + DO i=1, n-1 + + vec(i+1) = vec(i) - 3d0 * vec(i) * time_step + + END DO + END SUBROUTINE + + LOGICAL FUNCTION check_result(euler_result, threshold, time_step) + + IMPLICIT NONE + REAL(8), DIMENSION(:), INTENT(IN) :: euler_result + REAL(8), INTENT(IN) :: threshold, time_step + REAL(8) :: time, solution + INTEGER :: i + + check_result = .TRUE. + + DO i = 1, SIZE(euler_result) + + time = (i - 1) * time_step + solution = EXP(-3d0 * time) + + IF (ABS(euler_result(i) - solution) > threshold) THEN + + WRITE(*,*) euler_result(i), solution + check_result = .FALSE. + + END IF + END DO + END FUNCTION +END PROGRAM euler + diff --git a/contents/forward_euler_method/forward_euler_method.md b/contents/forward_euler_method/forward_euler_method.md index 25210e1d1..a0a08782e 100644 --- a/contents/forward_euler_method/forward_euler_method.md +++ b/contents/forward_euler_method/forward_euler_method.md @@ -121,6 +121,8 @@ Full code for the visualization follows: [import, lang:"matlab"](code/matlab/euler.m) {% sample lang="swift" %} [import, lang:"swift"](code/swift/euler.swift) +{% sample lang="f90" %} +[import, lang:"fortran"](code/fortran/euler.f90) {% endmethod %}