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

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

问题描述

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

  minimum(f,x0,xopt)

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



我该如何解决这个问题。
在matlab中我会使用匿名函数
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b最低(g,x0,xopt)

然而,正如我在fortran 90没有匿名函数。



谢谢。

解决方案

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



Fortran内部函数是在这里非常有用(Fortran 2008功能,但在gfortran和ifort中支持,在Solaris Studio中不支持):

  call minimum(g ,x0,xopt)

包含

实函数g(x)
实数,意图(in):: x
g = 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)

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.

解决方案

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

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

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