带有附加参数的函数的 Fortran 最小化 [英] Fortran minimization of a function with additional arguments

查看:28
本文介绍了带有附加参数的函数的 Fortran 最小化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 fortran 中,我有一个外部优化例程,它将 function f(x) 和起点作为输入并返回局部最小值的值.例如,如果函数被称为最小:

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)

问题是我需要最小化的函数取决于一些不属于最小化例程的附加参数:f(x,data).

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).

我怎样才能克服这个问题.在 matlab 中我会使用匿名函数

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

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

minimum(g, x0, xopt)

然而,正如我所理解的,fortran 90 中没有匿名函数.

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

谢谢.

推荐答案

为此您不需要匿名函数.您的 Matlab 示例最终也不是匿名的,它的名称为 g.

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

Fortran 内部函数在这里很有用(Fortran 2008 特性,但在 gfortran 和 ifort 中支持,在 Solaris Studio 中不支持):

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

这篇关于带有附加参数的函数的 Fortran 最小化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆