matplotlib - Python: TypeError: only length-1 arrays can be converted to Python scalars -


i have func

f(x) = sin(x/5.0)*exp(x/10.0) + 5*exp(-x/2.0) 

i need solve system of linear equations

w0 + w1x1 + w2(x1)**2 + ... + wn(x1)**n = f(x1) 

i solve have problem plot it

from math import sin, exp scipy import linalg import numpy np  b = [] def f(x):     return sin(x/5.0)*exp(x/10.0) + 5*exp(-x/2.0)  in [1, 15]:     b.append(f(i))  = []  in [1, 15]:     ij = []     x0 = ** 0     x1 = ** 1     ij.append(x0)     ij.append(x1)     a.append(ij)  matrix = np.array(a) b = np.array(b).t  x = linalg.solve(matrix, b) matplotlib import pyplot plt plt.plot(x, f(x)) 

but returns

typeerror: length-1 arrays can converted python scalars 

how can solve problem?

math.sin , math.exp expect scalar inputs. if pass array, typeerror

in [34]: x out[34]: array([ 3.43914511, -0.18692825])  in [35]: math.sin(x) typeerror: length-1 arrays can converted python scalars 

from math import sin, exp loads sin , exp math module , defines them functions in global namespace. f(x) calling math's version of sin function on x numpy array:

def f(x):     return sin(x/5.0)*exp(x/10.0) + 5*exp(-x/2.0) 

to fix error, use numpy's sin , exp functions instead.

import numpy np def f(x):     return np.sin(x/5.0)*np.exp(x/10.0) + 5*np.exp(-x/2.0) 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -