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