我想使用build-in-function myfunction
来评估我的目标函数的双重积分(名为integral2
,见下文)。
我试过运行这个脚本;
f = @(r,theta) myfunction(r,theta);
integral2(f,0,2,0,2*pi);
其中myfunction
是以下函数:
function fkt=myfunction(r,theta)
x=r.*cos(theta);
y=r.*sin(theta);
los(:,1)=x;
los(:,2)=y;
norm = (sum( sqrt( los(:,1).^2 + los(:,2).^2)));
fkt=norm*r;
end
我在极坐标中做积分,为什么 fkt = norm * r 。
Matlab给出了以下错误消息:
>> untitled2
Subscripted assignment dimension mismatch.
Error in myfunction (line 8)
los(:,1)=x;
我无法弄清楚,问题是什么。
答案 0 :(得分:0)
x是一个矩阵,您尝试将其分配给列向量。
答案 1 :(得分:0)
有两件事可以改进:
los
未定义los(:,1)
是一列,而x
是一行,因此分配必须失败。您可以通过定义los
并更改作业来更正此问题。例如:
los = NaN(numel(r), 2);
los(:,1) = x';
los(:,2) = y';
但是,为什么需要变量los
?只需删除它,错误就会消失:
function fkt=myfunction(r,theta)
x=r.*cos(theta);
y=r.*sin(theta);
norm = (sum( sqrt(x.^2 + y.^2)));
fkt=norm*r;
end
最佳,