我正在尝试在MATLAB上数字集成一个函数(我在MATLAB 7.7上)。这是我的代码。
fun = @(t)(cos(t)./sqrt(3)-sin(t)).^39*(cos(t)./sqrt(3)+sin(t)).^63*sqrt(2*cos(t).^2 + 2/3*sin(t).^2);
quad(fun,-pi/6,pi/6)
不幸的是我没有关注错误消息。有人可以帮帮我吗?
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==>
@(t)(cos(t)./sqrt(3)-sin(t)).^39*cos(t)./sqrt(3)+sin(t)).^63*sqrt(2*cos(t).^2+2/3*sin(t).^2)
Error in ==> quad at 77
y = f(x, varargin{:});
我试图查看函数定义是否正确。它似乎对我有用:
fun(1)
ans =
-1.4078e-007
这是我在1处评估时所期望的正确值。我已经用各种输入做了几次试验,函数fun()似乎计算好了!
p.s:我之前使用过quad()。它以前对我有用。
答案 0 :(得分:0)
您的函数适用于标量输入,但不适用于矢量:
>> fun(1:5)
Error using *
Inner matrix dimensions must agree.
您需要更改函数以进行元素乘法运算:
fun = @(t)(cos(t)./sqrt(3)-sin(t)).^39.*(cos(t)./sqrt(3)+sin(t)).^63.*sqrt(2*cos(t).^2 + 2/3.*sin(t).^2);