Skip to content

Euler fortran #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contents/euclidean_algorithm/euclidean_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}


Expand Down
64 changes: 64 additions & 0 deletions contents/forward_euler_method/code/fortran/euler.f90
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions contents/forward_euler_method/forward_euler_method.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

<script>
Expand Down