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

  • MATLAB
  • Thread starter Radinax
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses simulating electric and magnetic fields of high voltage transmission lines using MATLAB. The equations for the electric field involve division by zero and log of zeros, which can be dealt with using commands like isnan and isinf. The code provided by the expert suggests using these commands to eliminate or fix these values in the vectors. The conversation also includes a discussion about graphing 3D electric fields and the possibility of using the surfc command to smooth out the peaks in the graph. The original code for calculating the electric and magnetic fields is also shared.
  • #1
Radinax
12
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
  • #2
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:
  • #3
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
 
  • #4
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:
  • #5
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
 
  • #6
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
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.
 

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

1. How do I handle the "MATLAB divide by zeros" error?

There are a few ways to handle this error. One option is to use the "try-catch" statement to catch the error and handle it in your code. Another option is to use the "eps" function to replace the zeros with a very small number before performing the division.

2. Why does MATLAB give a "divide by zeros" error?

This error occurs when you attempt to divide a number by zero, which is mathematically undefined. MATLAB recognizes this as an error and stops the execution of your code to prevent incorrect calculations.

3. Can I ignore the "divide by zeros" error in MATLAB?

While it is possible to ignore this error, it is not recommended as it can lead to incorrect calculations and results. It is best to handle the error in your code to ensure accurate results.

4. How can I debug the "divide by zeros" error in MATLAB?

You can use the MATLAB debugger to step through your code and identify where the error is occurring. You can also use the "dbstop if error" command to automatically pause the execution of your code when an error, such as the "divide by zeros" error, occurs.

5. Is there a way to prevent the "divide by zeros" error in MATLAB?

There are a few ways to prevent this error. One option is to check your code for any potential divisions by zero before executing it. Another option is to use the "isinf" function to check for infinite values before performing the division.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
314
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
Back
Top