# MATLAB divide by zeros problem

1. Apr 28, 2010

Hello everybody, im 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 im 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 dont know HOW to evade it or eliminate the indetermination, cuz i had a similar problem with th magnetic fields but i solved it with "eps" but here i simply dont know how to manage the equations in matlab so it wont divide by zero, or log zero. Im really having issues here, can anyone help me??

PS: Im 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.

2. Apr 29, 2010

### MATLABdude

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 (Text):

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 [Broken]

Last edited by a moderator: May 4, 2017
3. Apr 30, 2010

I tried the code that you put, but matlab says something like "??? Undefined function or variable 'SuperElectricSolverScript'." dont know if thats bad or anything. Im triyng to graph 3D electric fields of high voltage transsmission lines, but i dont have a reference on how its supossed to look like, cant anyone tell me how the electric fields on 3D looks like for two lines??

PS: Im kinda of a novice in using matab

4. Apr 30, 2010

### MATLABdude

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: Apr 30, 2010
5. Apr 30, 2010

Thanks i get it now!
About 3D graphs, im 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 im getting some weird peaks, and i was wondering if there is a way to smooth it??

PS: Thanks for helping me here

6. May 1, 2010

### MATLABdude

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)

7. May 1, 2010

Code (Text):

>> %Simulation of two power lines
%Defining variables
I=2000; %Current
h=10; %Height
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 dont know if im making any mistake in it, im 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 dont know if there is anything wrong.

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