numpy - Python not computing equation correctly -


i'm trying write simple program plot x vs y. however, not seem computing correct value ofy.

for instance in first case when x = -1 value of e^(x^2/2 - x) -1 should 3.48, instead returning 1.

the other error have doesn't seem plotting x vs y, instead separate line each value of x.

import numpy np import math import matplotlib.pyplot plt  x = np.arange(-2, 2) y = np.arange(-2, 2)  in range(-3, 3):     y[i] = math.exp(( x[i]^2/2 ) - x[i])-1  print x, y plt.plot([x, y]) plt.show() 

the built in numpy array operations perfect this.

this line want:

 y = np.exp(np.power(x, 2)/2 - x) - 1 

full code becomes

import numpy np import math import matplotlib.pyplot plt  x = np.arange(-2, 2)  y = np.exp(np.power(x, 2)/2 - x) - 1   print(x, y) plt.plot(x, y) plt.show() 

(note, plot statement changed here)

details

if start write loop when working numpy, way. vector operations can faster (sometimes several orders of magnitude) corresponding python code, that's why people love it. many of basic operations (+, -, *, /, **) overloaded. check last item in references section more info.

references


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -