Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

matlab - Fortran minimization of a function with additional arguments

In fortran I have an external optimization routine that takes as an input the function f(x) and the starting point and returns the value for local minimum. For example if the function is called minimum:

minimum(f,x0,xopt)

The problem is that the function I need to minimize depends on some additional arguments that are not part of the minimization routine: f(x,data).

How can I overcome this issue. In matlab I would use anonymous function

g=@(x) f(x,data)

minimum(g, x0, xopt)

However as I understant in fortran 90 there are no anonymous function.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need anonymous functions for this. Your Matlab example is also not anonymous in the end, it has a name g.

Fortran internal functions are of great use here (Fortran 2008 feature, but supported in gfortran and ifort, not supported in Solaris Studio):

call minimum(g, x0, xopt)

contains

  real function g(x)
    real, intent(in) :: x
    g = f(x,data)
  end function

end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...