Silverfrost Forums

Welcome to our forums

##[SOLVED]##Access Violation while Calling Subroutine

7 Nov 2013 5:12 (Edited: 7 Nov 2013 5:56) #13287

Hi I'm trying to write a code for Curve Fitting, before I go further I should declare that I'm not an experienced programer I'm still learning Fortran and programing. Here is what happens I need to transpose F matrix and store it in Fbar I do it by calling my transposer subroutine here is my code: (it is fragmented cause it is not complete yet).

there are two problems with it currently I decided to make pi a Parameter first it said you can't use atan intrinsic function in initializing and what you see now is my second try which was in vain again. the second and primary problem with the code is when I run my program (without pi being a parameter) I get Access Violation error which points to this line: Fbar(j,i) = F(i,j) from my subroutine. I tried to understand what was the problem but I couldn't since the error doesn't give me enough information 😦

EDIT: I found my error for access violation I did not allocate Fbar but the first question is still there how can I have pi as constant?

Program Curve_Fitting1
    implicit none
    integer :: i, j, n, m
    real, dimension(:,:), allocatable :: F
    real, dimension(:,:), allocatable :: Fbar
    real, dimension(2,5) :: P                  !Data points
    real :: c
    c = 4.0*atan(1.0)
    real, parameter :: e = 0.00001, pi = c
    
    
    
    
    
    
    OPEN (10, file='data.txt', status='old', action='read') 
    READ (10,*) P
    allocate (F(5,3))
    do i = 1, 5
        F(i,1) = 1.0/2 
        F(i,2) = cos(pi*P(1,i)/2000.)
        F(i,3) = cos(2.*pi*P(1,i)/2000.)
    end do
    n = 5
    m = 3
    call transposer (n, m, F, Fbar)
    write (*,*) Fbar(1,:)
    read (*,*) m








end program Curve_Fitting1












!##############################################################
!funcstions and subroutines####################################
!##############################################################



subroutine  transposer (n, m, F, Fbar)
    implicit none
    integer, intent(in) :: n
    integer, intent(in) :: m
    real, dimension(n,m), intent(in) :: F
    real, dimension(m,n), intent(out) :: Fbar
    integer :: i, j
    do i = 1, n
        do j = 1, m
            Fbar(j,i) = F(i,j)
        end do
    end do
end subroutine transposer
7 Nov 2013 5:29 (Edited: 7 Nov 2013 5:33) #13288

The best way to get PI as a constant is just to use a literal constant with enough decimal places for the precision you are using.

So:

integer, parameter :: SP = 4 ! Depending on
inetger, parameter :: DP = 8 ! compiler
real(DP), parameter :: dpi = 3.141592653589793238_DP
real(SP), parameter :: spi = 3.14159265_SP

The decorations _DP and _SP are essential!

You can use atan in an initialize expression in Fortran 2008 but compilers haven't implemented this yet (some folks like to use pi=acos(-1.0) instead of 4.0*atan(1.0))

You have not allocated Fbar array, which is probably the cause of the access violation.

You don't need to write your own transpose. You can just do:

Fper = transpose(F)

Transpose is a built in Fortran intrinsic function. In Fortran 2003 this would probably also allocate Fper automatically for you. but it doesn't work in Fortran 95 so you have to do this yourself.

7 Nov 2013 5:31 #13289

Thank you, I thought fortran transpose function is for square matrices only

7 Nov 2013 5:34 #13290

No it woks for any array of rank 2. e.g. Rectangular matrices.

Please login to reply.