首页 > 解决方案 > “输入参数过多”尝试在 MATLAB 中运行脚本时出错

问题描述

我正在尝试运行讲师给我的脚本,但由于某个功能而出现错误。该函数位于一个单独的文件中,但位于同一目录中。该函数是一个方程,将在主脚本中双重集成。

这是主脚本(此文件名为“ele_assignment_ii.m”:

clear
format short

%The following part is for part (a)
dx = input('Enter the integration increment dx... \n >');
dy = input('Enter the integration increment dy... \n >');
total_charge=0;
for x=0:dx:1, 
for y=0:dy:1,
total_charge=rho_s_fun(x,y)*dx*dy+total_charge;
end
end
disp(sprintf('The total charge computed by manual double'));
disp(sprintf(' integration is %d nC',total_charge));
total_charge=dblquad(@rho_s_fun, 0, 1, 0, 1);
disp(sprintf('The total charge computed by the functional'))
disp(sprintf(' double integration is %d n',total_charge))

%The following for part (b)
Etotal=0;
for x=0:dx:1,
for y=0:dx:1,
rminusrprime=[-x -y 5];
numerator=rho_s_fun(x,y)*1e-9*dx*dy*rminusrprime;
denominator=4*pi*1e-9/(36*pi)*norm(rminusrprime)^3;
Etotal=Etotral+numerator/denominator;
end
end
disp(sprintf('The electric field computed by manual double'));
disp(sprintf(' integration is (%d, %d, %d) V/m',... 
    Etotal(1), Etotal(2), Etotal(1)))

下面是函数代码(这个文件名为 rho_s_fun.m):

function rho_s_fun = rho_s_fun()
    rho_s_fun = @(x,y) (x.*y).*(x.^2 + y.^2 + 25).^(3/2);
end

最后,这是我运行主脚本时的错误:

>> ele_assignment_ii
Enter the integration increment dx... 
 >2
Enter the integration increment dy... 
 >2
Error using rho_s_fun
Too many input arguments.

Error in ele_assignment_ii (line 10)
total_charge=rho_s_fun(x,y)*dx*dy+total_charge;

据我了解,这个错误显然导致了第 10 行的后续错误。

如何更改此代码以使其正常工作?

标签: matlab

解决方案


推荐阅读