gpt4 book ai didi

Matlab对匿名函数的操作

转载 作者:太空宇宙 更新时间:2023-11-03 20:28:04 24 4
gpt4 key购买 nike

我有两个匿名函数 f 和 g,我需要使用 quad 函数计算 f*g 的积分。

我尝试了以下方法,但没有用:

h=@(x)(f(x)*g(x))
quad(h,-1,1);

有什么方法可以让它工作吗?

最佳答案

简答

这取决于 fg 的定义方式,但您可能只需要添加一个点:

h = @(x) f(x).*g(x);

长答案

根据 quad's documentation

Q = quad(FUN,A,B)

[...] The function Y=FUN(X) should accept a vector argument X and return a vector result Y, the integrand evaluated at each element of X.

假设 fg 已经满足这个要求,h 应该定义为 .* ( element-wise multiplication )这样它也能实现它。

例子:

>> f = @(x) x;
>> g = @(x) x.^2;
>> h = @(x) f(x).*g(x);
>> quad(h, 0, 1)
ans =
0.250000000000000

关于Matlab对匿名函数的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37910525/

24 4 0
文章推荐: matlab - Matlab中n个元素的分段平均
文章推荐: c# - 如何在 WPF 中进行 CPU 使用控制
文章推荐: javascript - 使
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com