I’m writing a rpogram in Fortran for computing Boundary Value Problem ODE.
Here is my code:
module_BVP.f90
module module_BVP
public :: Step, Tridiagonal_Solv, ReadFromFile, WriteToFile, channel
contains
!========== Step =============================================================================================
! - a, - b
! - n.
function Step(n, a, b) result(h)
implicit none
integer, intent(in) :: n
double precision, intent(in) :: b, a
real :: h
h = (b-a) / (n-1)
end function Step
!======================================================================================================================
!============== Subroutine Values =================
subroutine Tridiagonal_Solv(n, a, b, alpha, beta)
implicit none
integer :: i, j, n
double precision :: a, b !
real, dimension(:) :: q, w, t, y
real, dimension(:) :: sigma, teta
double precision :: p, l, z
double precision, intent(in) :: alpha, beta
double precision, dimension(n) :: x, r, vector_B
double precision, dimension(n,n) :: matrix_A
x(0) = a
x(n) = b
p = -1 / (Step(n, a, b)*Step(n, a, b))
z = 2 / (Step(n, a, b)*Step(n, a, b)) + 1
l = 2 / Step(n, a, b)
! x(i) r(x(i))
do i = 2, n-1
x(i) = a + i*Step(n, a, b)
r(i) = 2*sin(x(i))
end do
! nxn .
do i = 1, n
do j = 1, n
matrix_A(i,j) = 0
end do
end do
! ( )
matrix_A(1,1) = -3 / (2*Step(n, a, b))
matrix_A(1,2) = l
matrix_A(1,3) = -1 / (2*Step(n, a, b))
!
do i = 2, n-1
matrix_A(i,i-1) = p
matrix_A(i,i) = z
matrix_A(i,i+1) = p
vector_B(i) = r(i)
end do
! ( )
matrix_A(n,n-2) = 1 / (2*Step(n, a, b))
matrix_A(n, n-1) = l
matrix_A(n,n) = 3 / (2*Step(n, a, b))
vector_B(1) = alpha
vector_B(n) = beta
y(0) = 0
y(n+1) = 0
sigma(0) = 0
teta(0) = 0
w(1) = 0
t(n) = 0
do i = 2, n-1
w(i) = matrix_A(i,i-1)
end do
do i = 1, n
q(i) = matrix_A(i,i)
end do
do i = 1, n-1
t(i) = matrix_A(i,i+1)
end do
! ,
do i = 1, n
sigma(i) = -t(i)/(q(i) + w(i)*sigma(i-1))
teta(i) = (r(i) - w(i)*teta(i-1))/(q(i) + w(i)*sigma(i-1))
end do
y(n) = teta(n)
! ,
do i = n-1, 1, -1
y(i) = sigma(i)*y(i+1) + teta(i)
end do
end subroutine Tridiagonal_Solv
!==============================================
!================= Subroutine _Read ==================
subroutine ReadFromFile(n, a, b, alpha, beta)
implicit none
integer :: n, free, err
double precision :: a, b
double precision :: alpha, beta
call channel(free)
open(unit = free, file = "Values.dat", status = "old", action = "read")
do
read(free, *, iostat = err) n, a, b, alpha, beta
if(err == 0) then
print *, "Reading data..."
print *
print *, " n a b alpha beta"
print "(i7, f8.2, f16.10, f11.3, f10.3)", n, a, b, alpha, beta
print *
print *, "Data reading complete successfully!"
exit
else if(err < 0) then
print *, "Error in data reading!"
exit
end if
end do
close(free)
end subroutine ReadFromFile
!===============================================
!================= Subroutine _Write ========
subroutine WriteToFile(n, x, y)
implicit none
integer :: free, n, i
double precision, dimension(n) :: x, y
call channel(free)
open(unit = free, file = "Results.dat", status = "replace", action = "write")
write(free, *) " i x(i) y(i)"
write(free, *)
do i = 1, n
write(free, fmt = "(i3, f13.8, f13.8)"), i, x(i), y(i)
end do
print *
print *, "The results was writen in file 'Results.dat'"
close(free)
end subroutine WriteToFile
!==============================================
!================== CHANNEL ==========================================================================================
SUBROUTINE channel (NFREE)
LOGICAL FISOPEN
INTEGER NFREE
FISOPEN = .TRUE.
NFREE = 9
DO WHILE (FISOPEN)
NFREE = NFREE + 1
INQUIRE (NFREE, OPENED = FISOPEN)
END DO
RETURN
END SUBROUTINE channel
!====================================================================================================================
end module module_BVP
BVP.f90
program BVP
use module_BVP
implicit none
integer :: n
double precision :: a, b
double precision :: alpha, beta
double precision, dimension(n) :: x, y
call ReadFromFile(n, a, b, alpha, beta)
call Tridiagonal_Solv(n, a, b, alpha, beta)
call WriteToFile(n, x, y)
end program BVP
There is no error when I compile it. But when run it in the Results.dat file there is no results writen. Only zeros in all columns. Why is that happen.