Arrays have incompatible sizes for this operation.

5 次查看(过去 30 天)
I am new to matlab coding. I wrote a code and I am getting this error "Arrays have incompatible sizes for this operation".
% here I define epx, esx and define polx as given below, which all Nr x ntheta x nphi dimension
epx = zeros(nr,ntheta,nphi);
esx = zeros(nr,ntheta,nphi);
polx = 3.0*(epx.^2.*conj(esx));
for ii = 1:nR
for jj = 1:nTheta
for kk = 1:nPhi
% the function handle funy is used to integrate f(r, theta, phi) over r, theta and phi using simp3v function below
funy = @(r,theta,phi) -(ka^2/R(ii))*exp(1i*ka*R(ii)).*...
exp(-1i*ka*R(ii).*r.*(sin(Phi(kk)).*sin(phi).*cos(Theta(jj)+theta)+...
cos(Phi(kk)).*cos(phi))/R(ii)).*r.^2.*sin(theta).*cos(Theta(jj))*cos(Phi(kk)).*polx;
eay(ii,jj,kk) = simp3v(funy,rmin,rmax,thetamin,thetamax,phimin,...
phimax,nr);
end
end
so the error comes due to "polx". if i remove this in the function handle, then the code runs correctly, but with polx in the function handle, the code runs into error.
Any suggestions would be helpful as I am new to matlab.
  3 个评论
Jayachander Borker
Arrays have incompatible sizes for this operation.
Error in CARS_2>@(r,theta,phi)-(ka^2/R(ii))*exp(1i*ka*R(ii)).*exp(-1i*ka*R(ii).*r.*(sin(Phi(kk)).*sin(phi).*cos(Theta(jj)+theta)+cos(Phi(kk)).*cos(phi))/R(ii)).*r.^2.*sin(theta).*cos(Theta(jj))*cos(Phi(kk)).*polx (line 188)
cos(Phi(kk)).*polx;
Error in simp3v (line 19)
S = feval(func,xx,yy,zz);
Error in CARS_2 (line 199)
eay(ii,jj,kk) = simp3v(funy,rmin,rmax,thetamin,thetamax,phimin,...
Related documentation

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2023-2-8
If you use a normal function instead of an anonymous function, you can use the debugger easily.
function y = funy(r,theta,phi)
y = -(ka^2/R(ii))*exp(1i*ka*R(ii)).*...
exp(-1i*ka*R(ii).*r.*(sin(Phi(kk)).*sin(phi).*cos(Theta(jj)+theta)+...
cos(Phi(kk)).*cos(phi))/R(ii)).*r.^2.*sin(theta).*cos(Theta(jj))*cos(Phi(kk)).*polx;
end
Set a breakpoint in this function or let Matlab stop automatically by setting:
dbstop if error
Now run the code again until it stops. Check the dimensions of the used functions. Splitting the huge formula into parts would be useful also - and easier to read.

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by