1

I want to visualize the magnetic field of a wire with the quiver function.

#Calculation of a magnetic field of a wire

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from pylab import *

I = 100000000000
constant = 1e-7/(4*np.pi)

# wire elements; always lenght one
coord = [(10,10,0), (11,10,0), (12,10,0), (13,10,0), (14,10,0), (15,10,0), (16,10,0), (17,10,0), (18,10,0),
         (19,10,0), (19,11,0), (19,12,0), (19,13,0)]

xwidth = 3
ywidth = 3
z = 1
b = np.zeros((xwidth,ywidth))

# calculate the b-field
def bfield(x,y,z,c):
    for x in range(xwidth):
        for y in range(ywidth):
            # number of wire elements
            for i in range(1,12):
                rx = x-(coord[i][0]+coord[i+1][0])/2.
                ry = y-(coord[i][1]+coord[i+1][1])/2.
                rz = z * 1.0 # = z-0
                r = (rx**2+ry**2+rz**2)**0.5 # distance r between field and middle of the wire
                dl = np.array([(coord[i+1][0]-coord[i][0]), (coord[i+1][1]-coord[i][1]), 0])
                bb = np.cross(dl, np.array([rx,ry,rz]))
                e = constant*I*bb/r**3
                print e
                #print e[0], e[1]
                b[x,y] += e[c]  # EDIT
    return b

X,Y = meshgrid(arange(0,xwidth,1),arange(0,ywidth,1))
U = bfield(X,Y,z,0)
V = bfield(X,Y,z,1)
quiver(X,Y,U,V)
xlim(0,xwidth)
ylim(0,ywidth)
show()

EDIT 2: How can I plot lines of the coords in the plot? EDIT 3: I want to use quiver, but it doesn't work.

kame
  • 20,848
  • 33
  • 104
  • 159

2 Answers2

1

it looks like quiver only supports 2d plots right now, but you could make it 3d by plotting multiple 2d layers into a 3d plot. You can follow my example to see how to do these layers.

Community
  • 1
  • 1
Paul
  • 42,322
  • 15
  • 106
  • 123
  • No, i want to use 2d. U contains the x-compontent of the magnetic-field-vector. V contains the y-compontent of the magnetic-field-vector. – kame Feb 19 '11 at 12:52
  • the shape of U and V should be (5,5) in your example (to match up with the dims of X and Y. – Paul Feb 19 '11 at 13:13
  • also, `bfield` should also return a copy of b if you don't want U and V to be identical – Paul Feb 19 '11 at 13:18
  • Paul: I did it with xwidth now. - U and V are different now. – kame Feb 19 '11 at 13:32
0
ValueError: too many values to unpack

That means you have more values on the RHS than variables on the LHS

Asterisk
  • 3,534
  • 2
  • 34
  • 53
  • Can you explain a little bit more? In which line? Here? --> U = bfield(X,Y,z,0)??? – kame Feb 19 '11 at 12:17
  • 1
    File "D:\Python26\Lib\site-packages\matplotlib\quiver.py", line 343, in _parse_args nr, nc = U.shape – Asterisk Feb 19 '11 at 12:23
  • 1
    @kame - If there's still a problem, please make clear what it is. – tom10 Feb 19 '11 at 16:45
  • @kame: what doesn't work about it? That is, quiver works, but I assume you mean that you're not getting out of it what you want. To figure this out, we'd need to know what you want and compare this to what you're getting. It's difficult just based on code to figure out what you want it to do vs what it actually does, so if you say in words exactly what you want, we might be able to help. – tom10 Mar 06 '11 at 18:22