How to Make a Variable Global in mnbrak.f90?

  • Thread starter Thread starter winiepu
  • Start date Start date
  • Tags Tags
    Numerical
Click For Summary
SUMMARY

This discussion addresses how to declare a global variable in Fortran, specifically within the context of using the mnbrak.f90 subroutine. The user initially attempted to use an internal procedure for the function f=(x-i)^2 but encountered limitations due to the internal procedure's incompatibility with mnbrak. The solution provided involves creating a module named 'globals' to declare the variable i, allowing it to be accessed by both the main program and the internal function. This approach effectively resolves the issue of variable scope.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with modules in Fortran
  • Knowledge of subroutines and functions in Fortran
  • Basic understanding of variable scope in programming
NEXT STEPS
  • Learn about Fortran module creation and usage
  • Explore variable scope and lifetime in Fortran
  • Study the mnbrak.f90 subroutine and its applications
  • Investigate best practices for using global variables in Fortran
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working with numerical methods and optimization routines, as well as programmers looking to understand variable scope and module usage in Fortran.

winiepu
Messages
3
Reaction score
0
The following code is just an example. I try to apply mnbrak.f90 to my function f=(x-i)^2. I want to make i globle variable. If I use internal procedure, I can not use mnbrak.f90, since it can not accept the internal procedure. How do I deal with it? Thanks!

PROGRAM xbrent
USE nr
USE nrtype
IMPLICIT NONE

INTEGER(i4b) :: i
REAL(sp) :: ax, bx, cx, fa, fb, fc
!real, external:: f
! common i

DO i=1,10
ax=i-0.5_sp
bx=i+0.5_sp
CALL mnbrak(ax, bx, cx, fa, fb, fc, f)
!$ WRITE(*,'(1x,t13,a,t25,a,t37,a)') 'A','B','C'
!$ WRITE(*,'(1x,a3,t5,3f12.6)') 'X',ax,bx,cx
!$ WRITE(*,'(1x,a3,t5,3f12.6)') 'F',fa,fb,fc
write(*,*) f(ax),f(bx)
END DO

contains

FUNCTION f(x)
USE nr
USE nrtype
IMPLICIT NONE
REAL(sp) ::f,x
!common i
f=(x-i)**2.0_sp
END FUNCTION f

END PROGRAM xbrent
 
Technology news on Phys.org


Thank you for sharing your code and question. It seems like you are trying to use the mnbrak.f90 subroutine with your own function f=(x-i)^2, where i is a global variable. However, you are having trouble using an internal procedure with the mnbrak subroutine.

One solution to this issue could be to declare the global variable i as a module variable. This way, it can be accessed by both the main program and the internal procedure. You can do this by creating a module that contains the variable i and then importing it into both the main program and the internal procedure. Here is an example:

MODULE globals
INTEGER(i4b) :: i ! declare i as a module variable
END MODULE globals

PROGRAM xbrent
USE nr
USE nrtype
USE globals ! import the module containing i
IMPLICIT NONE

REAL(sp) :: ax, bx, cx, fa, fb, fc

DO i=1,10
ax=i-0.5_sp
bx=i+0.5_sp
CALL mnbrak(ax, bx, cx, fa, fb, fc, f)
WRITE(*,'(1x,a3,t5,3f12.6)') 'X',ax,bx,cx
WRITE(*,'(1x,a3,t5,3f12.6)') 'F',fa,fb,fc
write(*,*) f(ax),f(bx)
END DO

contains

FUNCTION f(x)
USE nr
USE nrtype
USE globals ! import the module containing i
IMPLICIT NONE
REAL(sp) ::f,x
f=(x-i)**2.0_sp
END FUNCTION f

END PROGRAM xbrent

By declaring i as a module variable, it can now be used in both the main program and the internal procedure. I hope this helps with your code! Let me know if you have any further questions.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
35K
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
12K
  • · Replies 3 ·
Replies
3
Views
3K