Dustin O'Neal
Dustin O'Neal

Reputation: 23

X and Y are not of the same dimesnion. Python Issue

I am writing a code that uses the forward Euler method to numerically solve some velocity equations. When I run the code, I am getting an error:

`   235         y = np.atleast_1d(y)
    236         if x.shape[0] != y.shape[0]:
--> 237             raise ValueError("x and y must have same first dimension")
    238         if x.ndim > 2 or y.ndim > 2:
    239             raise ValueError("x and y can be no greater than 2-D") 

ValueError: x and y must have same first dimension`

I have been staring at this for hours, and I can not see how they are not the same dimension. Any help would be much appreciated.

Here is the full code:

from matplotlib.pylab import *
import numpy as n
import pylab as p
steps = 100
m = .15 #Kilograms
D= .07 #Meters
Gamma = .25 #Units of Ns^2/m^4
c = Gamma*D**2
g = 9.8 #Meters per second
v_0 = 25.0 #initial speed
theta_0 = 45.0 #initial angle
dt = .25
t = linspace(0, 100, 10)

Vx = zeros(steps)
Vy = zeros(steps)
x = zeros(steps)
y = zeros(steps)

for i in range(steps - 1):
    Vx[i] = Vx[i-1] - dt*((c/m)*sqrt(Vx[i-1]**2+Vy[i-1]**2)*Vx[i-1])
    Vy[i] = Vy[i-1] - dt*((c/m)*sqrt(Vx[i-1]**2+Vy[i-1]**2)*Vy[i-1] + g)
    x[i] = x[i-1] + dt*(Vx[i-1])
    y[i] = y[i-1] + dt*(Vy[i-1])
    if y[i] < 0.0:
        print (x[i]*y[i-1] - x[i-1]*y[i])/(y[i-1] - y[i])
        break

plot(y, t)
show()

Upvotes: 0

Views: 3648

Answers (3)

wallE
wallE

Reputation: 61

matplotlib gives error if the dimensions of your arrays are not matched. You can simply check it and squeeze it to desired dimensions using numpy

>>> x = np.array([[[0], [1], [2]]])

x.shape

(1, 3, 1)

np.squeeze(x, axis=(2,)).shape

(1, 3)

Upvotes: 1

sebix
sebix

Reputation: 3230

Both arrays do not have the same dimension, that's correct by matplotlib. You can check the dimensions and sizes (the «shape») by doing the following:

>>> y.shape
(100,)
>>> t.shape
(10,)

So just do

t = linspace(0, 100, steps)

Upvotes: 0

YS-L
YS-L

Reputation: 14738

The error is explaining itself - y and t should have the same shape but they don't. I suspect this is what you want:

t = linspace(0, 100, steps)

Upvotes: 0

Related Questions