# Demo entry 4798388

Calculate the magnetic flux density

Submitted by anonymous on May 18, 2016 at 13:04
Language: Python. Code size: 3.9 kB.

```#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
import math

def bField(point, res):
#Dimensions of coil
R = 0.155 #0.155 m
N = 150   #150 turns
I = 0.999 #0.999 A

Ntheta = res
dtheta = 2*np.pi / Ntheta
theta = dtheta * np.arange(1, Ntheta + 1)

B = np.zeros(3)
for n in range(0,Ntheta):
r = np.array([point[0]-R*math.cos(theta[n]),
point[1]-R*math.sin(theta[n]),
point[2]])
ds = dtheta * np.array([-R*math.sin(theta[n]),
R*math.cos(theta[n]), 0])
dB = I * np.cross(ds,r)/(np.linalg.norm(r)**3)

B = B + dB

#mu_0 and number of turns
B = B * N * 10**(-7)

#return z-component of B-field
return B[2]

def main():
#Resolution of the integration
res = 100

#Points of measurements
xPoints = np.arange(-0.12,0.13,0.02)
zPoints = np.arange(-0.12,0.13,0.02)

#Values obtained through measurements
xValues = np.array([10.8, 8.5, 7.16, 6.44, 6, 5.6, 5.8, 5.9, 6.14, 6.62, 7.46,
8.96, 12.2]) * 10**(-4)
zValues = np.array([2.98, 3.56, 4.22, 4.78, 5.32, 5.68, 5.8, 5.52, 5.14, 4.6,
4, 3.4, 2.84]) * 10**(-4)

#Create axis for theoretical function
axis = np.linspace(-0.25,0.25,100)

Bx = np.zeros(len(axis))
Bz = np.zeros(len(axis))

#Calculate B-field for entire axis (x-axis)
for i in range(0,len(axis)):
Bx[i] = bField([axis[i],0,0], res)

#(z-axis)
for j in range(0,len(axis)):
Bz[j] = bField([0,0,axis[j]], res)

#Plotting magnetic flux density on x-axis of coil
plt.figure(1)
plt.plot(xPoints, xValues, 'o', label=u"Flödestäthet (x-axel)")
plt.plot(axis, Bx, 'r', label=u"Teoretisk kurva")
plt.axis([-0.13,0.13,0.0004,0.0013])

#Plot labels
plt.xlabel(u"Avstånd [cm]")
plt.ylabel(u"Magnetisk flödestäthet [T]")

#Scientific notation on y-axis
plt.ticklabel_format(style="sci", axis="y", scilimits=(0,0))
plt.legend(loc=3, prop={'size':12})

#Plotting magnetic flux density on z-axis of coil
plt.figure(2)
plt.plot(zPoints, zValues, 'o', label=u"Flödestäthet (z-axel)")
plt.plot(axis, Bz, 'r', label=u"Teoretisk kurva")
plt.axis([-0.13,0.13,0,0.0007])

#Plot labels
plt.xlabel(u"Avstånd [cm]")
plt.ylabel(u"Magnetisk flödestäthet [T]")

#Scientific notation on y-axis
plt.ticklabel_format(style="sci", axis="y", scilimits=(0,0))
plt.legend(loc=2, prop={'size':12})

#Plotting comparison
#Storing values in vectors
comparisonAxis = np.append(np.arange(-0.20,0.05,0.05), np.arange(0,0.25,0.05))
vassilios = np.array([6.7,11,18.1,27.8,33.8,33.6,26.6,16.7,10.2,6.4])/(5*10**(4))
gabriella = np.array([5.7,9.8,16.6,25.3,29.1,29.1,24.5,15.4,9.3,5.7])/(5*10**(4))
andreas = np.array([3.1,1.3,8.2,16.4,20,41.1,36.4,28.2,21.3,17.1])/(5*10**(4))
robert = np.array([5.4,8.7,14.5,22.6,30,30.1,29.4,21.9,14.1,8.6])/(5*10**(4))

#Plot
plt.figure(3)
plt.plot(axis, Bz, 'r',label=u"Teoretisk kurva")
plt.plot(zPoints, zValues, 'b--o', label=u"Våra mätvärden")
plt.plot(comparisonAxis, vassilios, 'g-.o', label=u"Vassilios")
plt.plot(comparisonAxis, gabriella, 'c-.o', label=u"Gabriella")
plt.plot(comparisonAxis, andreas, 'm-.o', label=u"Andreas")
plt.plot(comparisonAxis, robert, 'y-.o', label=u"Robert")
plt.axis([-0.21,0.21,0,0.0009])

#Plot labels
plt.xlabel(u"Avstånd [cm]")
plt.ylabel(u"Magnetisk flödestäthet [T]")

#Scientific notaion on y-axis
plt.ticklabel_format(style="sci", axis="y", scilimits=(0,0))
plt.legend(loc=2, prop={'size':12})

#Show plots
plt.show()

if __name__ == "__main__":
main()
```

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.