How can I fix the code?
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
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
2018년 7월 30일
What happens if you try to display s? Is there an error message?
Anh Thu
2018년 7월 31일
답변 (1개)
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
댓글 수: 0
이 질문은 마감되었습니다.
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!