MATLAB How to Avoid Division by Zero in MATLAB for Electric Field Simulations?

  • Thread starter Thread starter Radinax
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion focuses on avoiding division by zero and logarithm of zero errors in MATLAB while simulating electric fields for high voltage transmission lines. The user is encountering NaN and -inf outputs in their electric field equations and seeks solutions to handle these issues effectively. Suggestions include using MATLAB commands like isnan, isinf, and isfinite to identify and manage invalid values, as well as modifying the original equations to prevent these errors. The user also expresses difficulties with 3D graphing and seeks advice on smoothing out peaks in the visual output. Overall, the conversation emphasizes troubleshooting MATLAB code for accurate electric field simulations.
Radinax
Messages
11
Reaction score
0
Hello everybody, I am trying to simulate electric and magnetic fields of high voltage transmission lines (im using DC for NOW!), when i put the equations for two cables, one at a distance "a" and another at "-a" at a height of "h", the equations are as follow:

Exx=Ke*((((X+a)./((X+a).^2+(Y-h).^2))-((X+a)./((X+a).^2+(Y+h).^2))) + (((X-a)./((X-a).^2+(Y-h).^2))-((X-a)./((X-a).^2+(Y+h).^2))));

Eyy=Ke*((((Y-h)./((X+a).^2+(Y-h).^2))-((Y+h)./((X+a).^2+(Y+h).^2))) + (((Y-h)./((X+a).^2+(Y-h).^2))-((Y+h)./((X+a).^2+(Y+h).^2))));

im having issues at the electric field (im good with the magnetic field one), of course I am not considering "Q" right now. These are the components of the elctric field for two lines. Well the problem is that i ordered MATLAB to tell me when there are division by zero or log of zeros, well it appears on the two componentes, and i don't know HOW to evade it or eliminate the indetermination, because i had a similar problem with th magnetic fields but i solved it with "eps" but here i simply don't know how to manage the equations in MATLAB so it won't divide by zero, or log zero. I am really having issues here, can anyone help me??

PS: I am learning english, so i may have put some words wrong.
On the equations there are four tearms, two belong to one cable, and the other two to the other one, both positives.
 
Physics news on Phys.org
For something like this, you can sometimes just 'fix' the output, rather than trying to avoid the division by / log of 0 problem.

The output of divide by zero is a NaN (Not a Number), while I believe the output of log(0) is -inf (negative infinity). The commands isnan and isinf return index numbers of elements that are NaNs or Infs (positive or negative). There's also the isfinite command, which you can complement to eliminate both NaNs or Infs.
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/isnan.html
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/isinf.html
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/isfinite.html

So, you could do something like the following:

Code:
warning off all		% turns off the warning messages
[Exx, Eyy] = SuperElectricSolverScript
warning on all		% turns warning messages back on

nanElem = isnan(Exx);	% finds the elements of Exx which are NaNs
Exx(nanElem) = 0;	% sets NaNs to 0, or whatever is appropriate
infElem = isinf(Exx);
Exx(infElem) = 0;

naninfElem = find(~isvalid(Eyy));
Eyy(naninfElem) = 0;

Alternately, you can eliminate these values (as in delete them from the vector--though you should also go through and delete the corresponding values in y so the vectors still line up, and match up properly):
http://www.mathworks.com/access/helpdesk/help/techdoc/data_analysis/f0-10104.html#f0-8511
 
Last edited by a moderator:
Thnx answering!
I tried the code that you put, but MATLAB says something like "? Undefined function or variable 'SuperElectricSolverScript'." don't know if that's bad or anything. I am triyng to graph 3D electric fields of high voltage transsmission lines, but i don't have a reference on how its supossed to look like, can't anyone tell me how the electric fields on 3D looks like for two lines??

PS: I am kinda of a novice in using matab
 
Radinax said:
Thnx answering!
I tried the code that you put, but MATLAB says something like "? Undefined function or variable 'SuperElectricSolverScript'." don't know if that's bad or anything. I am triyng to graph 3D electric fields of high voltage transsmission lines, but i don't have a reference on how its supossed to look like, can't anyone tell me how the electric fields on 3D looks like for two lines??

PS: I am kinda of a novice in using matab

I thought it was clear, but SuperElectricSolverScript is your function for calculating Exx and Eyy values.

EDIT: Instead of doing SuperElectricSovlerScript, you can just do what you have for Exx and Eyy, now that I look back at your original post.
 
Last edited:
Thanks i get it now!
About 3D graphs, I am getting some ugly peaks when i use the surfc command, i establish something like follow:

x=(-40:1:40);
y=x;
[X,Y]=meshgrid(x,y);

and i use the surfc command to graph it and then the contour, but on the 3D I am getting some weird peaks, and i was wondering if there is a way to smooth it??

PS: Thanks for helping me here
 
I'm sorry, but instead of posting snippets, might I suggest you post your actual code instead? TIP: put the [CODE ][\CODE ] tags around your code (without the spaces at the end, in order to have better formatted code)
 
Code:
>> %Simulation of two power lines
%Defining variables
I=2000; %Current
a=3; %Radio
h=10; %Height
u=1.2566e-006; %permeabilidad magnetica
w=6.832; %2*pi
>> Ke=9*10^9;
>> x=(-40:1:40);
>> y=x;
>> [X,Y]=meshgrid(x,y);

>> %ELECTRIC FIELD CASE
>> Exx=Ke*((((X+a)./((X+a).^2+(Y-h).^2))-((X+a)./((X+a).^2+(Y+h).^2)))-(((X-a)./((X-a).^2+(Y-h).^2))-((X-a)./((X-a).^2+(Y+h).^2))));
>> Eyy=Ke*((((Y-h)./((X+a).^2+(Y-h).^2))-((Y+h)./((X+a).^2+(Y+h).^2)))-(((Y-h)./((X+a).^2+(Y-h).^2))-((Y+h)./((X+a).^2+(Y+h).^2))));
>>  E3D=sqrt(Exx.^2+Eyy.^2); %TOTAL ELECTRIC FIELD
>> surfc(X,Y,E3D)
>> contour(X,Y,E3D)

>> %MAGNETIC FIELD CASE
>> Bxx=-(u*I/w)*(((Y-h)./((Y-h).^2+(X-a).^2+eps))+((Y-h)./((Y-h).^2+(X+a).^2+eps)));
>> Byy=(u*I/w)*(((X+a)./((Y-h).^2+(X+a).^2+eps))+((X-a)./((Y-h).^2+(X-a).^2+eps)));
>> B3D=sqrt(Bxx.^2+Byy.^2); %Campo magnetico en 3D
>> surfc(X,Y,B3D)
>> contour(X,Y,B3D)

This is the code that i made, i don't know if I am making any mistake in it, I am very new at matlab, i started using it about a month ago, i used the equations that i made, and i simply give it values and then graph it, but i don't know if there is anything wrong.

PS: This is the original one without the code you made to fix the inf problem.
 

Similar threads

Back
Top