Arrays have incompatible sizes for this operation.
조회 수: 2 (최근 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
답변 (1개)
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.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!