Issue in plotting array in python - python

TypeError: only size-1 arrays can be converted to Python scalars
import math
import numpy as np
import matplotlib.pyplot as plt
def myfunction(x):
return np.int(x)
myfunction2 = np.vectorize(myfunction)
x = np.array([0, math.pi/100, 2*math.pi])
y = math.sin(x)
plt.plot(myfunction2(x),y)
plt.show()

There are a few problems I see. First, you need to use np.sin in order to apply it to the entire vector. Second, your definition of x is only 3 elements long. I believe you are trying to use np.arange. Try x = np.arange(0,2*math.pi,math.pi/100,dtype='float32') instead.

Related

Error for a given function: only size-1 arrays can be converted to Python scalars

i am a newbie and currently trying to plot this function so I can choose a range of x values for which I should perform my experiments in the ChemistryLab.
I found different articles on plotting functions and it all worked with generic functions like sin(x).
But once I input my function it does not work. The problem already occurs after the first two lines:
import numpy as np
import math
X = np.linspace(0, 512, 256)
f = ((x+22)- math.sqrt((x+22)**2-4*2*x))
--> TypeError: only size-1 arrays can be converted to Python scalars
I found threads saying X should be vectorize, however I could not come up with a solution.
Thanks for help in advance!
Create a vectorized function. For example:
equation_func = np.vectorize(math.sqrt)
new_f = equation_func(X)
Swap math.sqrt with a function containing your more sophisticated equation and you're home.
A good way to implement a mathematical function in Python is, perhaps unsurprisingly, as a Python function. Then you can, for example, use that function in a list comprehension to get an array of values:
import numpy as np
import math
import matplotlib.pyplot as plt
X = np.linspace(0, 512, 256)
def f(x):
return x + 22 - math.sqrt((x + 22)**2 - 8 * x)
Y = np.array([f(x) for x in X])
plt.plot(X, Y);

How to find roots for a numpy array

I am wondering how to find foots for an array. What I have now is:
import numpy as np
from scipy.optimize import brentq as find_root
t = np.linspace(0, 100)
def f(x):
return x ** 2 - t
a = find_root(f, -400, 400)
print(a)
It gives me a type array saying that:
TypeError: only size-1 arrays can be converted to Python scalars.
I know the reason is that find_root can only take a scalar in its argument. What I want is to make “a” a bumpy array that finds root for the function given each possible value of t. Does that mean I need to write a loop for find_root? Or do I need to write a loop before I define the function? What’s the easiest way to do it?
Thank you very much for helping.
Yes, in this case it might be easiest to just loop over the arguments.
import numpy as np
from scipy.optimize import brentq as find_root
def f(x, t):
return x ** 2 - t
a = [find_root(f, 0, 400,args=(i,)) for i in np.linspace(1,10,10)]
print(a)
Note that I introduced an argument t to your function f to which you can pass the value using the args parameter of find_root.

matplotlib error: x and y must have same first dimension and lambda function

I'm quite new to python and especially new to the lambda functions. I tried plotting the function that I define below, but I get
ValueError: x and y must have same first dimension, but have shapes (100,) and (1,)
From what I've seen of function plotting, I think the error is in the definition of my functions, i.e. W is badly defined.
Here is the code:
import numpy as np
import matplotlib.pyplot as plt
def W(t):
s=lambda t: t
for k in range(5):
s=lambda t, y=s: y(t)+k
return s
t=np.linspace(0,1,100)
plt.plot(t,W(t))
If I change my code to this, it works:
import numpy as np
import matplotlib.pyplot as plt
def W(t):
s=lambda x: x
for k in range(5):
s=lambda x, y=s: y(x)+k
return s(t)
t=np.linspace(0,1,100)
plt.plot(t,W(t))

Newton method in python for multivariables (system of equations)

My code is running fine for first iteration but after that it outputs the following error:
ValueError: matrix must be 2-dimensional
To the best of my knowledge (which is not much in python), my code is correct. but I don't know, why it is not running correctly for all given iterations. Could anyone help me in this problem.
from __future__ import division
import numpy as np
import math
import matplotlib.pylab as plt
import sympy as sp
from numpy.linalg import inv
#initial guesses
x = -2
y = -2.5
i1 = 0
while i1<5:
F= np.matrix([[(x**2)+(x*y**3)-9],[(3*y*x**2)-(y**3)-4]])
theta = np.sum(F)
J = np.matrix([[(2*x)+y**3, 3*x*y**2],[6*x*y, (3*x**2)-(3*y**2)]])
Jinv = inv(J)
xn = np.array([[x],[y]])
xn_1 = xn - (Jinv*F)
x = xn_1[0]
y = xn_1[1]
#~ print theta
print xn
i1 = i1+1
I believe xn_1 is a 2D matrix. Try printing it you and you will see [[something], [something]]
Therefore to get the x and y, you need to use multidimensional indexing. Here is what I did
x = xn_1[0,0]
y = xn_1[1,0]
This works because within the 2D matrix xn_1 are two single element arrays. Therefore we need to further index 0 to get that single element.
Edit: To clarify, xn_1[1,0] means to index 1 and then take that subarray and index 0 on that. And although according to Scipy it may seem that it should be functionally equivalent to xn_1[1][0], that only applies to the general np.array type and not the np.matrix type. Here is an excellent thread on SO that explains this.
So you should use the xn_1[1,0] way to get the element you want.
xn_1 is a numpy matrix, so it's elements are accessed with the item() method, not like an array. (with []s)
So just change
x = xn_1[0]
y = xn_1[1]
to
x = xn_1.item(0)
y = xn_1.item(1)

piecewise function with 3d plot

I am having trouble getting np.piecewise to work for multiple dimensional plotting due to broadcast errors.
Does anyone have any manner to get around this?
Here is what I have in a simplified executable script:
import numpy as np
from pylab import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
num_steps = 100
x_arr = np.linspace(0,100, num_steps)
y_arr = np.linspace(0,20, num_steps)
def zfunc(x, y):
return np.piecewise(x, [x>=500, x<500], [x, -x])
x,y = np.meshgrid(x_arr, y_arr)
z =zfunc(x,y)
fig=plt.figure()
ax=fig.subplot(1,1,1,projection='3d')
p = x.plot_surface(x,y,z,rstride=1,cstride=1,cmap=cm.coolwarm,linewidth=0,antialiased=False)
plt.show()
Which gives the error:
return np.piecewise(x, [x>=500, x<500], [x, -x])
File "C:\Python27\lib\site-packages\numpy\lib\function_base.py", line 716, in piecewise
y[condlist[k]] = item
ValueError: array is not broadcastable to correct shape
Taking a look at the docstring of the function you're using is usually a good idea. I found this solution there.
np.piecewise(x, [x>=500, x<500], [lambda x: x, lambda x: -x])
funclist : list of callables, f(x,args,*kw), or scalars
Each function is evaluated over x wherever its corresponding
condition is True. It should take an array as input and give an array
or a scalar value as output. If, instead of a callable,
a scalar is provided then a constant function (lambda x: scalar) is
assumed.

Categories