Arrays have incompatible sizes for this operation.
2 views (last 30 days)
Show older comments
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
Answers (1)
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!