我想用Matlab的dblquad
來計算積分。要做到這一點,我首先寫了一個腳本函數。我的代碼是MatLab中的數值積分
function z = IntRect(x, y)
%The variables
z0=20;
x0=15;
y0=20;
rl=sqrt(x.^2+y.^2+(z0/2)^2);
theta=acos(z0./(2*rl));
phi=atan(y./x);
%The function
z=(x0-z0*tan(theta).*cos(phi))*(y0-z0*tan(theta).*sin(phi))*(z0/2)^4;
z=z/rl.^3;
要計算數值積分我在命令窗口
z0=20;x0=15;y0=20;
Q = dblquad(@IntRect,0,x0/2,0,y0/2,1.e-6);
型我得到一個錯誤,說
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> IntRect at 8
z=(x0-z0*tan(theta).*cos(phi))*(y0-z0*tan(theta).*sin(phi))*(z0/2)^4;
Error in ==> quad at 77
y = f(x, varargin{:});
Error in ==> dblquad>innerintegral at 84
Q(i) = quadf(intfcn, xmin, xmax, tol, trace, y(i), varargin{:});
Error in ==> quad at 77
y = f(x, varargin{:});
Error in ==> dblquad at 60
Q = quadf(@innerintegral, ymin, ymax, tol, trace, intfcn, ...
我在做什麼不好呢?
編輯
更換
z=(x0-z0*tan(theta).*cos(phi))*(y0-z0*tan(theta).*sin(phi))*(z0/2)^4;
與
z=(x0-z0*tan(theta).*cos(phi)).*(y0-z0*tan(theta).*sin(phi))*(z0/2)^4;
我得到一個新的錯誤
??? Index exceeds matrix dimensions.
Error in ==> quad at 85
if ~isfinite(y(7))
Error in ==> dblquad>innerintegral at 84
Q(i) = quadf(intfcn, xmin, xmax, tol, trace, y(i), varargin{:});
Error in ==> quad at 77
y = f(x, varargin{:});
Error in ==> dblquad at 60
Q = quadf(@innerintegral, ymin, ymax, tol, trace, intfcn, ...
開始調試。第#8行中發生第一個錯誤的變量的維數是多少? – Schorsch
@Schorsch:非常感謝您的評論。這是我不確定的事情......我知道這個問題在'theta'和'phi'變量的某個地方,但我無法確定。我所知道的是,它具有相同的尺寸。所以我在#8行使用了'phi'而不是'phi',但是我得到了完全相同的錯誤... – Thanos