Ask Your Question
0

Please help me fix an error

asked 2014-02-10 19:33:23 -0600

anonymous user

Anonymous

updated 2014-02-12 11:14:58 -0600

chip gravatar image

I'm getting an error in python:

"File "Untitled", line 17
    s.velocity = s.velocity +((1.6e-22)*cos(w*t)*dt)
ValueError: operands could not be broadcast together with shapes (3) (0) "

The code is posted below. does anybody know how i can fix this?:

B = vector(0,0,0.5e-4) # Tesla, the earth's magnetic field
q = -1.6e-19 # Coulomb, the charge on an electron
m = .00507 # kg, the mass of an electron
s = sphere(radius=2.0e-6)
s.velocity = vector(100,0,0) # m/s
w=(2*pi,0,0)
trail=curve(color=color.blue,pos=s.pos)
s.pos=(0,0,0)
t=0
dt=1e-10 # s
while t < 3e-6:
    if s.pos.x < 1e-6:
        s.velocity = s.velocity +((1.6e-22)*cos(w*t)*dt)
        trail.append(s.pos)
        t = t+dt
        rate(4e-7/dt)
    else:
        s.acceleration = q*cross(s.velocity,B)/m
        s.velocity = s.velocity + s.acceleration*dt
        s.pos = s.pos + s.velocity*dt
        trail.append(s.pos)
        t = t+dt
        rate(4e-7/dt)
edit retag flag offensive close merge delete

Comments

As chip said one of the arrays might be empty. Why don't you try 'w=vector(2*pi,0,0)', since you used vector for 's.velocity'

SC gravatar imageSC ( 2014-02-12 15:16:56 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2014-02-12 11:24:35 -0600

chip gravatar image

Check out the variables in the line of code which is giving you problems:

s.velocity = s.velocity +((1.6e-22)*cos(w*t)*dt)

It looks like one of the arrays is empty.

You can see this problem in a distilled case like:

import numpy as np
np.array([1,1,1]) * np.array([])

Which gives the error:

ValueError: operands could not be broadcast together with shapes (3) (0)
> <ipython-input-165-1fc37b5acfae>(1)<module>()
----> 1 np.array([1,1,1]) * np.array([])

A couple more examples:

In [169]: np.array([1,1,1]) * np.array([2.5, 1.0]) 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-169-37890460fbb0> in <module>()
----> 1 np.array([1,1,1]) * np.array([2.5, 1.0])

ValueError: operands could not be broadcast together with shapes (3) (2)

In [170]: np.array([1,1,1]) * np.array([2.5]) 
Out[170]: array([ 2.5,  2.5,  2.5])
edit flag offensive delete link more
0

answered 2014-02-12 10:39:48 -0600

SC gravatar image

Ask a stupid question, what module did you import for 'vector' and 'space'? 'visual'? I just installed VPython which claimed have these two, and

import visual

import vis

Failed masseges show as:

    import visual
  File "C:\Python27\Lib\site-packages\visual\__init__.py", line 34
    from visual_common.create_display import *
  File "C:\Python27\Lib\site-packages\visual_common\create_display.py", line 10
    import wx as _wx
  File "C:\Python27\lib\site-packages\wx-2.9.4-msw\wx\__init__.py", line 45
    from wx._core import *
  File "C:\Python27\lib\site-packages\wx-2.9.4-msw\wx\_core.py", line 4
    import _core_
ImportError: DLL load failed: %1 is not a valid Win32 application.

Any idea?

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

1 follower

Stats

Asked: 2014-02-10 19:33:23 -0600

Seen: 1,159 times

Last updated: Feb 12 '14