【问题标题】:Is there a way to integrate the log of a function, f(x) in Matlab without defining eg l = log(f(x)?有没有一种方法可以在 Matlab 中集成函数 f(x) 的对数而不定义例如 l = log(f(x)?
【发布时间】:2019-04-16 12:29:10
【问题描述】:

我有以下代码:

x = 0:0.001:2.5;
gamma_l = @(x) 2*x;

我想整合以下内容:

integral( log(gamma_l), 0 , 0.6 )

但它给了我错误:

类型输入参数的未定义函数'log' 'function_handle'。

我知道我可以定义:

gamma_l_l = @(x) log(2*x);
integral( gamma_l_l, 0 , 0.6 )

因为它以这种方式工作。但是,我想知道为什么第一种情况不起作用。以及是否有办法在不定义新函数的情况下集成函数。

【问题讨论】:

  • 第一种情况不起作用,因为log() 需要数字作为输入,而您没有给它一个数字,而是给它一个函数句柄。函数句柄的对数没有定义。
  • 谢谢@Ander!那么你认为最有效(唯一)的方法是定义另一个函数吗?没有办法在积分内部定义复合函数吗?
  • Gnovice 的回答是你应该如何处理这个问题;)

标签: matlab integration


【解决方案1】:

您的变量gamma_l 是一个anonymous function,而log 函数并非旨在接受function handles 作为输入。相反,您需要定义第二个匿名函数评估 gamma_l 的给定值,然后将数值结果传递给log,如下所示:

result = integral(@(x) log(gamma_l(x)), 0, 0.6);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多