Home > Enterprise >  How can I fix the Zero Division Error for a limit of a function with a singular point?
How can I fix the Zero Division Error for a limit of a function with a singular point?

Time:12-17

I am trying to avoid zero division error for the limit

f(u) = 1/(1-u) as u tends to 1^{-}, then f(u) tends to oo (inf)

My goal is for the function u to approach but not reach zero.  And I will add the 1/(1-u) function that satisfies this limit condition above to my equation that is described in another Python function. I've coded this limit in two different ways, but it gives the following error.

How can I avoid this error and still add this function to my equation?

My first way to define limit:

class F():
   def source(self):
      self.f = 1 / (1 - self.u)
      self.limit_f = limit(self.f, self.u, 1, '-')

ERROR:

<ipython-input-13-c16795f88d58>:18: RuntimeWarning: divide by zero encountered in true_divide
  self.f = 1 / (1 - self.u)

The second way:

class F():
   def source(self):
      self.f = 1 / (1 - self.u)
      if (self.u < 1).all():
         self.limit_f == limit(self.f, self.u, 1, '-')
      if (self.u == 1).all():
         self.f == oo

ERROR:

<ipython-input-13-c16795f88d58>:18: RuntimeWarning: divide by zero encountered in true_divide
  self.f = 1 / (1 - self.u)
<ipython-input-13-c16795f88d58>:29: RuntimeWarning: divide by zero encountered in true_divide
  self.f = 1 / (1 - self.u)

THANK YOU IN ADVANCE!

CodePudding user response:

The first line executes the error, before you try to catch it in the second line. One variant to solve this would be to use a mask

f = np.ones_like(u)*np.inf          # default to infinite value
singular = u==1                     # find out where infinity occurs
f[~singular] = 1/(1-u[~singular])   # compute value where not infinity

CodePudding user response:

As mentioned in the comments, this does not appear to be a problem at all as u-> infinity.

take this simple function (no need for a class).

def f(u):
    f = 1 / (1-u)
    return f


u = 10000000000000000

y = f(u)
print(y)
  1. this is completely well behaved for large values of u.
  2. the only issue would arise as u -> 1, in which case the denominator would -> 0 and f would become infinite.

This would be the only case needed to be handled (u=1).

CodePudding user response:

This syntax makes no sense (unless you are using some libraries like numpy or something else you didn't mention):

  if (self.u < 1).all():

Calling all() on boolean condition will result in AttributeError: 'bool' object has no attribute 'all'

If you are working with floating points the comparison to 0 does not make too much sense neither (due to floating point representation error). You should rather define an epsilon for which you would define conditions on "how close" to the 0 you allow it to be.

Regarding the "limit" I'd suggest to go with this approach (which is an overkill, see comment in the code...but since you didn't explain very clearly what you are aiming at):

  • keep passing increasing values for u
  • except on division error and then assign the "limit"
  • do further processing
  • since an error is raise your f will keep the previous (valid) value
try:
    self.f = 1 / (1 - self.u)
except ZeroDivisionError:
    self.limit = self.u # This is the "first" value of `u` which causes the error, but it boils down to u being 1 or 1.0, otherwise it won't happen

Take a look at this example (in python 3.11):

x = 1 / (1-10e1000000) # 10e1000000 is quite a big number
print(x) # -0.0 <class 'float'> 
  • Related