Re: Beginer needing help
- From: "e p chandler" <epc8@xxxxxxxx>
- Date: 30 May 2006 10:24:05 -0700
[discussion on using the CERN routine RMINFC to find the
minimum of sin(x)/x for x>0]
vexx wrote:
here is the code(i decidet to set interval from 1 to 7):
program bla
REAL(KIND=4) :: a = 1.0_4, b = 7.0_4
REAL(KIND=8) :: eps = 1.0D-8
REAL(KIND=8) :: del = 1.0D-7
REAL(KIND=8) :: x,y
The called routine expects KIND=4 for these arguments.
LOGICAL :: llm
EXTERNAL fun
CALL RMINFC(fun,a,b,eps,del,x,y,llm)
IF (llm) THEN
PRINT *,'Minimum is = ',x
PRINT *,'y(x_min) is = ',y
END IF
END PROGRAM
FUNCTION fun (x) RESULT (y)
REAL(KIND=4) :: x,y,pi=3.1415926535897932385_4
y = SIN(pi*x/180)/x
You are converting the argument X from degrees to radians before
passing it
to the SIN function but you are using it as is - *in degrees* in 1/X.
This makes no sense at all.
Insert above this
REAL(KIND=4) :: r
r = pi*x/180
and change the line above to
y = SIN(r)/r
END FUNCTION
Using your arguments of 1.0 and 7.0, the corrected program outputs no
result.
That is because over this interval fun(x) is strictly decreasing.
Replacing these arguments with 0.0 and 360.0 does give meaningful
output (see below).
but the problem is i think now with library. when i type in command
prompt: gfortran new.f90 -o new.exe -lmathlib it says cannot find
-mathlib. so does that mean that my compiler does not have a CERN
library ad if that's the problem how do i get it?
Source code and pre-compiled libraries are available from
http://cernlib.web.cern.ch/cernlib/version.html
However the library for Windows was made for Digital Visual Fortran.
This is incompatible with the libraries used by gfortran.
Exploring the links under source code reveals a file minfc.F and an
include
file minfccod.inc.
Combine these to get a new minfc.f.
C:\temp>type new.f90
program bla
REAL(KIND=4) :: a = 0.0_4, b = 360.0_4
REAL(KIND=4) :: eps = 1.0D-8
REAL(KIND=4) :: del = 1.0D-7
REAL(KIND=4) :: x,y
LOGICAL :: llm
EXTERNAL fun
CALL RMINFC(fun,a,b,eps,del,x,y,llm)
IF (llm) THEN
PRINT *,'Minimum is = ',x
PRINT *,'y(x_min) is = ',y
END IF
END PROGRAM
FUNCTION fun (x) RESULT (y)
REAL(KIND=4) :: x,y,pi=3.1415926535897932385_4
REAL(KIND=4) :: r
r = pi*x/180
y = SIN(r)/r
END FUNCTION
C:\temp>gfortran new.f90 minfc.f
C:\temp>a
Minimum is = 257.4686
y(x_min) is = -0.2172336
C:\temp>
257.4686 degrees = 4.493675 radians
which is close to the answer when you do the entire problem in radians:
Minimum is = 4.493674
y(x_min) is = -0.2172336
Using Newton's method with an initial guess x0 = 4.3, eps = 1e-6
my test program produced:
x = 4.493409
f(x)= -.2172336
e-mail: epc8 at juno dot com
.
- Follow-Ups:
- Re: Beginer needing help
- From: glen herrmannsfeldt
- Re: Beginer needing help
- References:
- Beginer needing help
- From: vexx
- Re: Beginer needing help
- From: e p chandler
- Re: Beginer needing help
- From: vexx
- Beginer needing help
- Prev by Date: Re: Algorithm help for unique string searching/counting within an array.
- Next by Date: Re: Algorithm help for unique string searching/counting within an array.
- Previous by thread: Re: Beginer needing help
- Next by thread: Re: Beginer needing help
- Index(es):