Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How can I fix the code?

조회 수: 2 (최근 30일)
Anh Thu
Anh Thu 2018년 7월 30일
마감: MATLAB Answer Bot 2021년 8월 20일
I'm trying to calculate the sum of f(z1^i,z2^j) for i,i=1:2m and x1,x2 are chosen before (are nodes). Here is my code in Matlab
% code
k=5;r0=0.5; M=1000; m=100; h=r0/m;
ui=@(x,y) exp(5i*y);
a=@(x,y)( 0.01*exp(1-(0.25/(0.25-x.^2-y.^2))).*(x.^2+y.^2 < 0.25)+0.*( x.^2+y.^2 >=0.25));
% a=0.01*exp(1-(0.25/(0.25-x^2-y^2) if x^2+y^2 < 0.25 and a=0 otherwise.
f=@(x1,x2,y1,y2) -1i.*k.^2./4.*besselh(0,sqrt((x1-y1).^2+(x2-y2).^2)).*a(y1,y2).*ui(y1,y2);
s=0;
for p=1:2*m
x1=-r0+p*h;
for q=1:2*m
x2=-r0+q*h;
for i=1:2*m
z1=-r0+(i/2)*h;
for j=1:2*m
z2=-r0+(j/2)*h;
s=s+f(x1,x2,z1,z2);
disp(s);
disp(f(x1,x2,z1,z2));
end
end
end
Matlab can't show the results of 's' but can show the results of 'f(x1,x2,z1,z2)'. How can I fix the code?
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 7월 30일
What happens if you try to display s? Is there an error message?
Anh Thu
Anh Thu 2018년 7월 31일
it display "NaN+NaNi"

답변 (1개)

OCDER
OCDER 2018년 7월 30일
For some reason, you're getting a NaN + NaNi, a complex imaginary NaN. Try to skip these, otherwise s will just become NaN + NaN*i.
% code
k=5;r0=0.5; M=1000; m=100; h=r0/m;
ui=@(x,y) exp(5i*y);
a=@(x,y)( 0.01*exp(1-(0.25/(0.25-x.^2-y.^2))).*(x.^2+y.^2 < 0.25)+0.*( x.^2+y.^2 >=0.25));
% a=0.01*exp(1-(0.25/(0.25-x^2-y^2) if x^2+y^2 < 0.25 and a=0 otherwise.
f=@(x1,x2,y1,y2) -1i.*k.^2./4.*besselh(0,sqrt((x1-y1).^2+(x2-y2).^2)).*a(y1,y2).*ui(y1,y2);
s=0;
for p=1:2*m
x1=-r0+p*h;
for q=1:2*m
x2=-r0+q*h;
for i=1:2*m
z1=-r0+(i/2)*h;
for j=1:2*m
z2=-r0+(j/2)*h;
Out = f(x1,x2,z1,z2);
if ~isreal(Out); continue; end
s=s+Out;
disp(s);
disp(Out);
end
end
end
end

이 질문은 마감되었습니다.

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by