Is there any way in Matlab, to shorten this function or is there any built-in function I can use instead of this? The following function works for me, but I want to exclude it from my script.
在Matlab中有什么方法可以缩短这个函数或者有什么内置函数可以代替这个?下面的函数适用于我,但是我想把它从我的脚本中排除。
function p = nintegrate(x, fx)
acc = 0;
for i = 1:length(x)-1
p(i) = acc;
delta_x = x(i+1) - x(i);
acc = acc + delta_x * fx(i);
end
end
1 个解决方案
#1
4
Have a look at cumtrapz, which does something similar. It used trapezoidal integration, which is a slightly better way of doing numerical integration. Usage in your case should be (untested):
看看cumtrapz,它也有类似的功能。它使用梯形积分,这是一种更好的数值积分方法。在你的情况下使用应(未经测试):
acc = cumtrapz(x, fx);
#1
4
Have a look at cumtrapz, which does something similar. It used trapezoidal integration, which is a slightly better way of doing numerical integration. Usage in your case should be (untested):
看看cumtrapz,它也有类似的功能。它使用梯形积分,这是一种更好的数值积分方法。在你的情况下使用应(未经测试):
acc = cumtrapz(x, fx);