Reputation: 4406
I have an array that is (219812,2)
but I need to split to 2 (219812)
.
I keep getting the error ValueError: operands could not be broadcast together with shapes (219812,2) (219812)
How can I accomplish?
As you can see, I need to take the two separate solutions from u = odeint and multiple them.
def deriv(u, t):
return array([ u[1], u[0] - np.sqrt(u[0]) ])
time = np.arange(0.01, 7 * np.pi, 0.0001)
uinit = array([ 1.49907, 0])
u = odeint(deriv, uinit, time)
x = 1 / u * np.cos(time)
y = 1 / u * np.sin(time)
plot(x, y)
plt.show()
Upvotes: 1
Views: 534
Reputation: 5522
To extract the ith column of a 2D array, use arr[:, i]
.
You could also unpack the array (it works row wise, so you need to transpose u
so that it has shape (2, n)), using u1, u2 = u.T
.
By the way, star imports aren't great (except maybe in the terminal for interactive use), so I added a couple of np.
and plt.
to your code, which becomes:
def deriv(u, t):
return np.array([ u[1], u[0] - np.sqrt(u[0]) ])
time = np.arange(0.01, 7 * np.pi, 0.0001)
uinit = np.array([ 1.49907, 0])
u = odeint(deriv, uinit, time)
x = 1 / u[:, 0] * np.cos(time)
y = 1 / u[:, 1] * np.sin(time)
plt.plot(x, y)
plt.show()
It also seems like a logarithmic plot looks nicer.
Upvotes: 3
Reputation: 526543
It sounds like you want to index into the tuple:
foo = (123, 456)
bar = foo[0] # sets bar to 123
baz = foo[1] # sets baz to 456
So in your case, it sounds like what you want to do might be...
u = odeint(deriv, uinit, time)
x = 1 / u[0] * np.cos(time)
y = 1 / u[1] * np.sin(time)
Upvotes: 1