Skip to content

Instantly share code, notes, and snippets.

@matiskay
Created September 1, 2012 03:53
Show Gist options
  • Save matiskay/3563514 to your computer and use it in GitHub Desktop.
Save matiskay/3563514 to your computer and use it in GitHub Desktop.
Fortran Coding
! Write a program that evals the following operations and show then in
! the screen
a = 8 / 2
b = 6 / 10
c = (6 + 8) / 5
d = 4 * 5 / 3 * 2
e = (4 * 5) / (3 / 2)
f = (4 * (5 / 3) / 2)
write (*,*) '8 / 2 =>' , a
write (*, *) '6 / 10 =>', b
write (*, *) '(6 + 8) / 5 =>', c
write (*, *) '4 * 5 / 3 * 2 =>', d
write (*, *) '(4 * 5) / (3 / 2) =>', e
write (*, *) '(4 * (5 / 3) / 2) =>', f
end
! gfortran temperature_module.f90 10.f90
! ./a.out
program main
use temperature_module, ONLY : celsius2farenheit, celsius2kelvin
implicit none
real :: c
write (*, *) 'Insert Temperature in Celsius: '
read (*, *) c
print *, celsius2farenheit(c)
print *, celsius2kelvin(c)
end program main
! gfortran 7.f90
program main
implicit none
real :: period
real :: l
print *, 'Insert the lenght of the rope: '
read (*, *) l
write (*, *) period(l)
end program main
! vf = v0 + a * t
real function period(l)
real :: l
real :: PI = 3.14
real :: G = 9.8
period = 2 * PI * (l / G) ** (1/2)
return
end function period
! gfortran 12.f90 swap_module.f90
program main
use swap_module, ONLY : swap_using_variable
implicit none
real :: a
real :: b
a = 3.0
b = 4.0
print *, a
print *, b
call swap_using_variable(a, b)
print *, a
print *, b
end program main
! gfortran 12.f90 swap_module.f90
program main
use swap_module, ONLY : swap_without_variable
implicit none
real :: a
real :: b
a = 3.0
b = 4.0
print *, a
print *, b
call swap_without_variable(a, b)
print *, a
print *, b
end program main
! gfortran 2.f90
! ./a.out
! Write a program that evals the following operations and show then in
! the screen
a = 2
b = 4
c = 2.4
D = 8.3
E = 1.43
P = (a ** 2 + b ** 2) / c
Q = (a ** 2) * b - ((b ** 2) * c / d)
R = 3 * ((a ** 2) * b - b ** 2)
S = (a / b) + ((b - c) / d) * ((e ** 3) / a)
write (*,*) '2 =>', a
write(*,*) '4 =>', b
write(*,*) '2.4 =>', c
write(*,*) '8.3 =>', D
write(*,*) '1.43 =>', E
write(*,*) '(a ** 2 + b ** 2) / c =>', P
write(*,*) '(a ** 2) * b - ((b ** 2) * c / d) =>', Q
write(*,*) '3 * ((a ** 2) * b - b **2) =>', R
write(*,*) '(a / b) + ((b - c) / d) * ((e **3) / a) =>', S
end
! gfortran 3.f90
! ./a.out
program main
a = 2.3
b = 5
write (*,*) Perimeter(a, b), Area(a, b)
end program main
real function Perimeter(a, b)
Perimeter = 2 * (a + b)
end function Perimeter
real function Area(a, b)
Area = a * b
end function Area
! gfortran 4.f90
! ./a.out
program main
implicit none
real :: r
real :: Longitude
real :: Area
real :: Volumen
r = 6.0
write (*, *) Longitude(r), Area(r), Volumen(r)
end program main
real function Longitude(r)
implicit none
real :: PI
real :: r
PI = 3.14
Longitude = 2.0 * PI * r
return
end function Longitude
real function Area(r)
implicit none
real :: PI
real :: r
PI = 3.14
Area = PI * r ** 2.0
return
end function Area
real function Volumen(r)
implicit none
real :: PI
real :: r
PI = 3.14
Volumen = 4 * (PI * r ** 3) / 3
return
end function Volumen
! gfortran 5.f90
! ./a.out
program main
implicit none
real :: Area
real :: Volume
real :: a, b, c
a = 18.3
b = 12.7
c = 8.2
write (*,*) Area(a, b, c), Volume(a, b, c)
end program main
real function Area(a, b, c)
Area = 2.0 * (a * b + b * c + c * a)
end function Area
real function Volume(a, b, c)
Volume = a * b * c
end function Volume
! gfortran 6.f90
program main
implicit none
real :: w
real :: angular_velocity_earth
real :: centripetal_acceleration
real :: linear_velocity
print *, 'Angular velocity of the Earth: ', angular_velocity_earth()
read (*, *) w
print *, "Linear Velocity: ", linear_velocity(w)
print *, 'Centrepal Acceleration: ', centripetal_acceleration(w)
end program main
! \omega = \frac{ 2 \pi }{T}
real function angular_velocity_earth()
implicit none
real, parameter :: PERIOD = 24.0
real, parameter :: R = 6370.0
real, parameter :: PI = 3.14
angular_velocity_earth = (2.0 * PI) / (PERIOD * 60 * 60)
return
end function angular_velocity_earth
! v = \frac{ 2 \pi R }{T}
real function linear_velocity(longitude)
implicit none
real, parameter :: PERIOD = 24.0
real, parameter :: R = 6370.0
real, parameter :: PI = 3.14
real :: longitude
real :: sex2rad
linear_velocity = (2 * PI / (PERIOD * 60 * 60)) * (R * cos(sex2rad(longitude)))
return
end function linear_velocity
! a_{c} = \frac{v^{2}}{r} = r w^{2}
! longitude sexagesimal
! Note: In Fortran, all angles are in radians. There are 2*pi radians in 360°,
! therefore, there are
! x = sin(r) r is in radians
! R=Hex*pi/180
real function centripetal_acceleration(longitude)
implicit none
real, parameter :: R = 6370.0
real :: longitude
real :: linear_velocity
real :: sex2rad
centripetal_acceleration = (R * cos(sex2rad(longitude))) * (linear_velocity(longitude) ** 2)
return
end function centripetal_acceleration
real function sex2rad(sex)
implicit none
real, parameter :: PI = 3.14
real :: sex
sex2rad = sex * (PI / 180.0)
return
end function sex2rad
! gfortran 7.f90
program main
implicit none
real :: vf
real :: a
real :: t
real :: initial_speed
real :: distance
vf = 33.0
t = 2.5
a = 100.0
print *, 'initial_speed: '
write (*, *) initial_speed(vf, a, t)
print *, 'distance: '
write (*, *) distance(initial_speed(vf, a, t), a, t)
end program main
! vf = v0 + a * t
real function initial_speed(vf, a, t)
real :: vf
real :: a
real :: t
initial_speed = vf - (a * t)
return
end function initial_speed
real function distance(v0, a, t)
real :: v0
real :: a
real :: t
distance = v0 * t + (1 / 2) * a * (t ** 2)
return
end function distance
! gfortran 8.f90
program main
implicit none
real :: a, b, c, d, e, f
real :: avg
a = 2.5
b = 6.3
c = 3.7
d = 4.1
e = 3.3
f = 5.6
print *, 'Average: '
write (*, *) avg(a, b, c, d, e, f)
end program main
! vf = v0 + a * t
real function avg(a, b, c, d, e, f)
implicit none
real :: a, b, c, d, e, f
avg = (a + b + c + d + e + f) / 6
return
end function avg
! gfortran 8.f90
program main
implicit none
integer :: i, j, l, k, m
real :: x, y, z, a
i = 4
j = 7
l = 9
k = 12
m = 19
x = 2.345
y = 2.153
z = 54.234
a = 32.768
print *, real(i)
print *, real(j)
print *, real(l)
print *, real(k)
print *, real(m)
print *, int(x)
print *, int(y)
print *, int(z)
print *, int(a)
end program main

Fortran 90

TODO

  • Add documentation
  • Add modules
  • Create a basic template for modules and functions
module swap_module
implicit none
contains
!real function swap_using_variable(a, b)
!
! real function swap_using_variable(a, b)
! real :: a
! real :: b
! a = a + b
! b = a - b
! a = a - b
! return
!end function swap_using_variable
subroutine swap_using_variable(a, b)
real :: a
real :: b
real :: temp
temp = a
b = temp
a = b
return
end subroutine swap_using_variable
subroutine swap_without_variable(a, b)
real :: a
real :: b
a = a + b
b = a - b
a = a - b
return
end subroutine swap_without_variable
! function swap_without_variable(a, b)
! real :: a
! real :: b
! end function swap_without_variable
end module swap_module
module temperature_module
implicit none
contains
real function celsius2kelvin(c)
real :: c
celsius2kelvin = c + 273.15
return
end function celsius2kelvin
real function celsius2farenheit(c)
real :: c
! F = 1.8 * c + 32
celsius2farenheit = 1.8 * c + 32
return
end function celsius2farenheit
end module temperature_module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment