Arrays have incompatible sizes for this operation.

2 views (last 30 days)
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 Comments
Jayachander Borker
Jayachander Borker on 8 Feb 2023
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

Sign in to comment.

Answers (1)

Jan
Jan on 8 Feb 2023
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.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!