|
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
shahrooz
Joined: 23 Oct 2013 Posts: 10 Location: Iran
|
Posted: Thu Nov 07, 2013 6:12 pm Post subject: ##[SOLVED]##Access Violation while Calling Subroutine |
|
|
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?
Code: | 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 |
Last edited by shahrooz on Thu Nov 07, 2013 6:56 pm; edited 2 times in total |
|
Back to top |
|
|
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Thu Nov 07, 2013 6:29 pm Post subject: |
|
|
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:
Code: |
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:
Code: |
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. _________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Last edited by davidb on Thu Nov 07, 2013 6:33 pm; edited 2 times in total |
|
Back to top |
|
|
shahrooz
Joined: 23 Oct 2013 Posts: 10 Location: Iran
|
Posted: Thu Nov 07, 2013 6:31 pm Post subject: |
|
|
Thank you, I thought fortran transpose function is for square matrices only |
|
Back to top |
|
|
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Thu Nov 07, 2013 6:34 pm Post subject: |
|
|
No it woks for any array of rank 2. e.g. Rectangular matrices. _________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|