About bracketing roots
This is an important tool to search for possible roots in the real or complex plane. This is, if you know where the root is located you may use this information to narrow the search and/or guide the program by supplying a better guess. It is common practice to do this by plotting the function or equation in question to investigate how many roots and where are these located. In fact, if you have a good software with plotting capabilities it would be a good tool for most cases.
Take for instance the case of the following equation:
$4x\sin(2x)=0$
can be plotted to look for possible roots between $x=1$ and $x=10$, for example. If we plot it in Maple the following is obtained:
You can see in the plot above the several crossings in the horizontal axis or roots. At a first inspection you would not probably realize the existence of such number of roots. Fortunately, the guys of Numerical Recipes faced this problem decades ago and created a routine capable of bracketing the roots.The zbrac routine in F90
You may find this routine in Chapter B9 in the NR book. However, you could need a main program to make use of the zbrac routine. Two options appear at this point: you may write your own program or make use of the Numerical Recipes Example Book (NREB). I took the shortest way and take advantage of the main example program for this routine.
Another obstacle that you would need to overcome is that some modules could be needed in order to see what this routine can do. You will need:
- the nrtype module. You can find it in the appendix C1 of the NR book,
- the nrutil module. You can find it in the appendix C2 of the NR book,
- an extra module, that I created to store zbrac, so that the routine could be called into the main program
- an extra module, that I cretaed to store a fortran function of the function/equation to be studied, so that it could be used in the main program.
Modules, in points 3 and 4, listed above are not mentioned either in NR nor in NREB. Here is a short explanation of this:
A module for zbrac
As you can see in the driver program proposed in the NREB for zbrac the subroutine is called and the compiler should find either in the same file for the main program or in a separate file. I preferred the second option to get a more easy-to-review for errors program. th code for this module is as followsroot_finding_module.f90
module root_finding_module
!
public:: zbrac
!
contains
!
subroutine zbrac(func,x1,x2,succes)
...
end subroutine zbrac
!
end module root_finding_module
In this way, all you have to do is to add a line code to connect this module with the main program as use root_finding_module.
A module for the function/equation to be studied
As you may have noticed in the codes supplied by NR and NREB no function was defined at all. However, for the test program proposed in NREB the Bessel functions of the first kind and order zero are considered since it is known that this functions has several zeros. You can see this graphically as follows:
where several crossings of the horizontal axis can be appreciated.
We know all this because in the NREB a BessJ0 function is used. Then, it should be supplied some how and again I used a module to do this. This is the one i used:
bessel_module.f90module bessel_module
!
public:: bessj0
!
contains
!
function bessj0(x) result(bessj0_result)
intrinsic:: bessel_j0
real, intent(in):: x
real:: bessj0_result
!
bessj0_result=bessel_j0(x)
end function bessj0
!
end module bessel_module
I changed the name of the main program to look more familiar.
No comments:
Post a Comment