Arrays have incompatible sizes for this operation.

조회 수: 2 (최근 30일)
Jayachander Borker
Jayachander Borker 2023년 2월 8일
답변: Jan 2023년 2월 8일
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
Dyuman Joshi
Dyuman Joshi 2023년 2월 8일
Please copy-paste the full error message.
Jayachander Borker
Jayachander Borker 2023년 2월 8일
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에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by