Prepare three Fortran functions to calculate the following mathematical function
ID: 672505 • Letter: P
Question
Prepare three Fortran functions to calculate the following mathematical functions
sinh(x)=(e^x-e^-x)/2 , cosh(x)=(e^x+e^-x)/2 , tanh(x)=(e^x-e^-x)/(e^x+e^-x)
Then, write a main unit program that uses these functions to calculate the hyperbolic sines, cosines and tangents of the following values:-2, -1.5, -1.0, -0.5, -0.25, 0, 0.25, 0.5, 1.0, 1.5 and 2. Write out the results in the following format.
x sinh(x) cosh(x) tanh(x)
-2.0 -3.63 3.762 -0.9640
-1.5 -2.13 2.352 -0.9051
-1.0 -1.18 1.543 -0.7616
-0.5 -0.52 1.128 -0.4621
-0.2 -0.25 1.031 -0.2449
0.0 0.00 1.000 0.0000
0.2 0.25 1.031 0.2449
0.5 0.52 1.128 0.4621
1.0 1.18 1.543 0.7616
1.5 2.13 2.352 0.9051
2.0 3.63 3.762 0.9640
Explanation / Answer
program math_func
!implicit none
integer :: i
real, dimension(11) :: numbers
numbers = (/-2.0, -1.5, -1.0, -0.5, -0.25, 0., 0.25, 0.5, 1.0, 1.5, 2.0/)
Print *, " x sinh(x) cosh(x) tanh(x)"
do i=1, 11
write(*,10)numbers(i),sinhyper(numbers(i)),coshyper(numbers(i)),tanhyper(numbers(i))
10 format(f8.1,4x,f10.2,4x,f10.3,4x,f10.4)
end do
end program math_func
function sinhyper(x)
implicit none
real :: sinhyper
real :: x
sinhyper = (exp(x)-exp(-x))/2
end function sinhyper
function coshyper(x)
implicit none
real :: coshyper
real :: x
coshyper = (exp(x)+exp(-x))/2
end function coshyper
function tanhyper(x)
implicit none
real :: tanhyper
real :: x
tanhyper = (exp(x)-exp(-x))/(exp(x)+exp(-x))
end function tanhyper