NaN at (5,0) in graph. How to change it? Electric field

As you can see, at the coordinates (5,0) it has NaN NaN
Does anyone know what can i do?
x = -24:18;
y = (-20:2:18).';
E0 = 8.85e-12;
k = 1/(4*pi*E0);
q = 2;
aq = abs(q);
a = 10;
Er_x = 0;
Er_y = 0;
id = -4;
jd = -12;
while id < 4
xp = ((x + a/2).^2 + (y-id).^2).^1.5;
E1_x = k * aq * (x+a/2) ./ xp;
E1_y = k * aq * y ./ xp;
Er_x = Er_x + (E1_x);
Er_y = Er_y + (E1_y);
id=id+1;
end
while jd < 12
xm = ((x - a/2).^2 + (y-jd).^2).^1.5;
E2_x = -k * aq * (x-a/2) ./ xm;
E2_y = -k * aq * y ./ xm;
Er_x = Er_x + ( E2_x);
Er_y = Er_y + ( E2_y);
jd=jd+1;
end
Er_x = Er_x + (E1_x + E2_x);
Er_y = Er_y + (E1_y + E2_y);
Etot = sqrt(Er_x.^2 + Er_y.^2);
u = Er_x ./ Etot;
v = Er_y ./ Etot;
quiver(x,y,u,v)

Réponses (2)

KSSV
KSSV le 5 Mai 2022
Modifié(e) : KSSV le 5 Mai 2022
You can fill the NaN's using fillmissing Read about this function.
x = -24:18;
y = (-20:2:18).';
E0 = 8.85e-12;
k = 1/(4*pi*E0);
q = 2;
aq = abs(q);
a = 10;
Er_x = 0;
Er_y = 0;
id = -4;
jd = -12;
while id < 4
xp = ((x + a/2).^2 + (y-id).^2).^1.5;
E1_x = k * aq * (x+a/2) ./ xp;
E1_y = k * aq * y ./ xp;
Er_x = Er_x + (E1_x);
Er_y = Er_y + (E1_y);
id=id+1;
end
while jd < 12
xm = ((x - a/2).^2 + (y-jd).^2).^1.5;
E2_x = -k * aq * (x-a/2) ./ xm;
E2_y = -k * aq * y ./ xm;
Er_x = Er_x + ( E2_x);
Er_y = Er_y + ( E2_y);
jd=jd+1;
end
Er_x = Er_x + (E1_x + E2_x);
Er_y = Er_y + (E1_y + E2_y);
Etot = sqrt(Er_x.^2 + Er_y.^2);
u = Er_x ./ Etot;
v = Er_y ./ Etot;
% Fill the NaN's
u = fillmissing(u,'linear') ;
v = fillmissing(v,'linear') ;
quiver(x,y,u,v)

2 commentaires

Cristobal Meza
Cristobal Meza le 5 Mai 2022
Modifié(e) : Cristobal Meza le 5 Mai 2022
oh I see, but the thing is that:
I need not to have a value, let me explain, it has to remain like the other ones above or below.
lets say that there are two graphs, the one in the left is perfect.
The one in the right has exactly filled (with NaN) at coordinates (5,0) but it shouldn`t
The only thing that i need to change is exactly at (5,0)
However, it's very interesting to know that there is such function, thank you for that.
The calculation you posted leads to 0 divided by 0 at that location. What should the result be there?

Connectez-vous pour commenter.

Etot = sqrt(Er_x.^2 + Er_y.^2);
u = Er_x ./ Etot;
What happens if Etot is 0 because Er_x and Er_y are both 0? 0/0 is... what?
Hint:
You can change Etot to the maximum of the existing calculations together with a very very small value. Then you would have 0/tiny which would be 0.

Produits

Version

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by