Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...