Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

How can I fix the code?

2 visualizaciones (últimos 30 días)
Anh Thu
Anh Thu el 30 de Jul. de 2018
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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 comentarios
Walter Roberson
Walter Roberson el 30 de Jul. de 2018
What happens if you try to display s? Is there an error message?
Anh Thu
Anh Thu el 31 de Jul. de 2018
it display "NaN+NaNi"

Respuestas (1)

OCDER
OCDER el 30 de Jul. de 2018
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

La pregunta está cerrada.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by