For Programmers: Free Programming Magazines  


Home > Archive > Fortran > September 2006 > how can I parameterize a 1d function by extracting it from a 2d function?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how can I parameterize a 1d function by extracting it from a 2d function?
Nye

2006-09-28, 7:00 pm


In case I have a subroutine A that needs a 1d function f(x) as its
parameter, and f(x) could be derived from a 2d function g(x,y) by
keeping the other variable y as a constant.

The situation is, I have g(x,y) already well defined in the program, and
I need to call A from time to time when the y changes thus need f(x) to
be a dynamically changing function. I wonder is there any simple way to
achieve this?


do i=1,100
y=y*1.2
call A(f)
continue
end

function g(x,y)

so how to define f(x)?
--

Don't forget your dreamS
Nye

2006-09-28, 7:00 pm

Craig Powers <enigma@hal-pc.org> writes:

> Nye wrote:
>
> Define f as a module function, with the value of y kept as a module variable, e.g.
>
> MODULE mod_f
> ! etc.
> REAL y
>
> CONTAINS
> REAL FUNCTION f(x)
> f = g(x,y)
> END FUNCTION f
> END MODULE mod_f
>
> ! etc.

Thanks, but my compiler is f77, I'm afraid there is no MODULE support within......
Is there any other way of doing this?
--

Don't forget your dreamS
Richard Maine

2006-09-28, 7:00 pm

Nye <zuyingl@gmail.com> wrote:

> Craig Powers <enigma@hal-pc.org> writes:
>
variable,[color=darkred]
> Thanks, but my compiler is f77, I'm afraid there is no MODULE support
> within...... Is there any other way of doing this?


Well, the partly, but not entirely facietious answer is that one way is
to get an f90/f95 compiler, insomuch as they are widely available, even
in free versions. There are many benefits to becoming no more than 15
years out of date.

The other answer is that you can do largely the same thing with common
in f77.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Herman D. Knoble

2006-09-29, 8:01 am

First, Richard's suggestion of using Fortran 90/95 instead of Fortran
77 compiler is the best advice. More advice, use Double Precision for
real variables.

Second, while not advised, you can use Fortran 77 Single Precision as
in the following example. (Apologies if I misunderstud your question).

Skip Knoble

C Main Program
real y, x
external f
common /fshare/ y
x=2.0
y=1.0
do i=1,100
y=y*1.20D0
print *, A(f,x)
end do
stop
end

real function g(x,y)
real x,y
g=x*y
C For example
return
end

real function A(func,x)
real func, x
A=func(x)
return
end

real function f(x)
real x, y
common /fshare/ y
f=g(x,y)
return
end

John Keenan

2006-09-29, 7:01 pm

> Thanks, but my compiler is f77, I'm afraid there is no MODULE support
within......
> Is there any other way of doing this?


This is from 20 year old memories. It is a standard pattern I used to use.
It avoids the need for a common via the use of a private saved variable.

program test
implicit none
real x, y, y_old

real f_y, f
external f_y, f

integer i, j

do i = 0, 100, 10
y_old = f_y( real( i ) )
do j = -5, 5
write( *, * ) 'i = ', i, ' j = ', j, ' f(j) = ', f( real( j ) )
end do
end do

end

real function existing_fxy( x_, y_ )
implicit none
real x_, y_
existing_fxy = x_ * y_
return
end

real function f_support()
implicit none
! Saved variables
real y
save y
! Other entries
real f_y, f
! Dummy arguments
real x_, y_
! External functions
real existing_fxy
external existing_fxy

stop 'f_support() should not be referenced.'
f_support = 0.0
return

entry f_y( y_ )
f_y = y ! Return original value
y = y_
return

entry f( x_ )
f = existing_fxy( x_, y )
return

end

John


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com