How to Make a Variable Global in mnbrak.f90?

  • Thread starter winiepu
  • Start date
  • Tags
    Numerical
In summary, to use an internal procedure with the mnbrak.f90 subroutine, you can declare global variables as module variables and import them into both the main program and internal procedure.
  • #1
winiepu
3
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
  • #2


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.
 
  • #3



It seems like you are trying to make the variable "i" a global variable in your code. However, using an internal procedure in mnbrak.f90 prevents you from doing so. One solution to this problem could be to pass the value of "i" as a parameter to the function f instead of trying to make it a global variable. This way, you can still use the mnbrak.f90 subroutine without any issues. Another option could be to declare "i" as a module variable, which can be accessed by both the main program and the internal procedure. This would also allow you to use mnbrak.f90 without any problems.
 

Related to How to Make a Variable Global in mnbrak.f90?

1. What is a "Numerical recipe problem"?

A "Numerical recipe problem" refers to a mathematical or computational problem that can be solved using a specific set of numerical algorithms or methods. These methods are often referred to as "numerical recipes" and are commonly used in various fields such as physics, engineering, and computer science.

2. How do I know which numerical recipe to use for a specific problem?

Choosing the right numerical recipe for a problem depends on various factors such as the type of problem, the available data, and the desired level of accuracy. It is important to have a good understanding of the mathematical concepts behind the problem and to consult with experts or references to determine the most appropriate numerical recipe.

3. What are the advantages of using numerical recipes?

Numerical recipes offer several advantages over traditional analytical methods, such as the ability to handle complex problems with large amounts of data, the ability to handle nonlinear relationships, and the ability to find solutions to problems that do not have a closed-form analytical solution. They also often provide faster and more efficient solutions compared to analytical methods.

4. Are there any limitations to using numerical recipes?

While numerical recipes have many advantages, they also have some limitations. These methods may not always provide accurate solutions, especially when dealing with highly nonlinear or chaotic systems. They also require a good understanding of the underlying mathematical concepts and may require significant computational resources.

5. Can numerical recipes be used for real-world problems?

Yes, numerical recipes are commonly used in various fields to solve real-world problems. They are particularly useful in fields such as physics, engineering, and finance, where analytical solutions may not be feasible or accurate. However, it is important to carefully select and validate the numerical recipe used to ensure accurate and reliable results.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
Replies
13
Views
1K
  • Introductory Physics Homework Help
Replies
11
Views
782
  • Programming and Computer Science
Replies
3
Views
3K
  • General Math
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
6
Views
29K
  • Differential Equations
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
1
Views
2K
  • Introductory Physics Homework Help
Replies
4
Views
11K
Back
Top