Question

why doesnt this work it isnt checking nans propperly import numpy as np import time x...

why doesnt this work it isnt checking nans propperly

import numpy as np
import time
x = np.linspace(-2,-1,100)
y = np.linspace(-1.5,1.5,100)
z0 = 0
z1s = []
for i in x:
for k in y:
z1 = (z0)**2 + i + k*1j
z0 = z1
if np.real(z0) == np.nan:########## this doesnt work. it never detects nans
print('nan') ### this NEVER prints. it should print after about 10 seconds
pass
if np.real(z0) != np.nan:
print('not nan')
z1s.append(z1)
print(z1)
time.sleep(1)
print(z1s)
################################## how do i get it to check for nans propperly. also how do i stop the runtime error

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answer below::

Here np.isnan should be used to check if number is NaN.

Code is as below:

import numpy as np
import time
x = np.linspace(-2,-1,10)
y = np.linspace(-1.5,1.5,10)
z0 = 0
z1s = []
for i in x:
for k in y:
z1 = (z0)**2 + i + k*1j
z0 = z1
if np.isnan(np.real(z0)):########## this doesnt work. it never detects nans
print('nan') ### this NEVER prints. it should print after about 10 seconds
else:
print('not nan')
z1s.append(z1)
print(z1)
time.sleep(1)
print(z1s)

output:

Also run time error is coming due to overflow value assigned to data type. This can be resolved only by changing logic to code or by using appropriate data type.

Add a comment
Know the answer?
Add Answer to:
why doesnt this work it isnt checking nans propperly import numpy as np import time x...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 abscissa = np.arange(20) 5 plt....

    python 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 abscissa = np.arange(20) 5 plt.gca().set_prop_cycle( ’ color ’ , [ ’ red ’ , ’ green ’ , ’ blue ’ , ’ black ’ ]) 6 7 class MyLine: 8 9 def __init__(self, * args, ** options): 10 #TO DO: IMPLEMENT FUNCTION 11 pass 12 13 def draw(self): 14 plt.plot(abscissa,self.line(abscissa)) 15 16 def get_line(self): 17 return "y = {0:.2f}x + {1:.2f}".format(self.slope, self.intercept) 18 19 def __str__(self):...

  • PYTHON import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import...

    PYTHON import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split Our goal is to create a linear regression model to estimate values of ln_price using ln_carat as the only feature. We will now prepare the feature and label arrays. "carat"   "cut" "color"   "clarity"   "depth"   "table"   "price"   "x"   "y"   "z" "1" 0.23   "Ideal" "E" "SI2" 61.5 55 326   3.95   3.98   2.43 "2" 0.21   "Premium" "E" "SI1"...

  • What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that...

    What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that share a set of tasks, even though the tasks are executed in different ways. Polymorphism allows a programmer to use a subclass object in place of a superclass object. Polymorphism allows a subclass to override a superclass method by providing a completely new implementation. Polymorphism allows a subclass to extend a superclass method by performing the superclass task plus some additional work. Assume that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT