【问题标题】:Declaring a Numeric Function in an Octave/Matlab Function在 Octave/Matlab 函数中声明数值函数
【发布时间】:2014-02-18 17:50:56
【问题描述】:

问题如下:

在 Octave 中实施 regula falsi 方法的伊利诺伊版本。 函数的签名是

illinois(
          f,    % a real numeric function
          a,    % left bound of search interval
          b,    % right bound of search interval
          yAcc, % accuracy in the y-dimsension
          nIter % maximum number of iterations
          )

提示:http://en.wikipedia.org/wiki/False_position_method#Illinois_algorithm

我想问:

我如何在 Matlab 函数中为这个问题输入一个实数函数?

例如说我在 x^2-4 中搜索 1 和 3 之间的根,所以它在 GUI 上的功能应该是

illinois(x^2-4, 1, 3, 0.1, 1000)

我如何告诉 matlab/octave 这个“数字函数”x^2-4 或者我错误地解释了这个问题?

【问题讨论】:

    标签: matlab math octave


    【解决方案1】:

    看看函数句柄:

    http://www.gnu.org/software/octave/doc/interpreter/Function-Handles.html

    您可以使用f=@(x)(x.^2-2) 定义函数,使用g(f,1,2,3,4) 将其传递给另一个函数,并像其他所有函数f(1) 一样评估它。

    【讨论】:

    • 谢谢!奇迹般有效!这是唯一可能的解决方案吗?
    • 这是我可以推荐的唯一解决方案。另一种方法是将函数作为字符串传递并使用 'eval',但仅总结一些缺点:奇怪的错误消息,难以调试,代码难以阅读,不支持私有函数,相同的变量名冲突和语法有限,因为您必须在一行中创建完整的函数。
    猜你喜欢
    • 2017-02-16
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 2014-04-30
    • 2012-11-19
    • 1970-01-01
    相关资源
    最近更新 更多