Plotting equation for solenoid magnetic field in python, receiving error

Viewed 122

I am very new to this, so I have no idea what is going wrong. I'm trying to plot the equation for the magnetic field through the axis of a solenoid in jupyter notebooks. It keeps returning the error 'TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int''. Here is the code for the equation:

def model(x,a,b,c,l,r):
     y=a*b*c*.5*((x/(math.sqrt(x**2+r**2)))-((x-l)/(math.sqrt((x-l)**2+r**2)))
return y

Anything helps.

1 Answers

If you want to make calculations for a whole collection of numbers at once you should use numpy.ndarrays and not lists. so converting x=np.array(x) to numpy and swaping math.sqrt to np.sqrt might already solve your problem.

When trying to run your code I noticed a missmatched parenthesis; but you should doublecheck.

import numpy as np

def model(x, a, b, c, l, r):
    y = 0.5*a*b*c * (x / np.sqrt(x**2 + r**2) - (x-l) / np.sqrt((x - l)**2 + r**2))
    return y

a, b, c, l, r = 1, 2, 3, 4, 5  # constants 
x = np.linspace(0, 1, 100)     # dummy data 

y = model(x, a, b, c, l, r)
Related