1

I'm new, and I have a question about my python code. It doesn't execute like I think it should. The part that's messing up is in the nested while:

import math
mu=4*math.pi*10**(-7)  ##mu naught
I=
float(input('Enter a value for the current measured in Amperes: '))
R=abs(float(input('Enter a value for the radius 
of the circle measured in meters: '))) ##radius
step=R/200  ##Step size for integration
B=[] ##array for B(r)
J=[]
Plot=[B,J]

k = 1 ##step counter for Circumfrence
j = 1 ##step counter for Radius
b_temp = 0 ##place holder to sum b(step*k)

D_Vector = [R*math.cos(k*math.pi/100),R*math.sin(k*math.pi/100)]
R_Vector = [R-j*step,0]

while(j*step<=R):
    while(k*step<=2*math.pi*R):
        r=(math.sqrt((R_Vector[0]-D_Vector[0])**2 + 
        (R_Vector[1]-D_Vector[1])**2))
        b_temp = b_temp + (mu/(4*math.pi))*I*step*step/(r**2)
        k=k+1
        D_Vector = [R*math.cos(k*math.pi/100),R*math.sin(k*math.pi/100)]
        R_Vector = [R-j*step,0]
        print(round(r,3), j)
    B.append(round(b_temp,8))
    print('New j value!')
    b_temp=0
    J.append(round(step*j,3))

    j=j+1

It's supposed to step down the radius (the first while loop) and then loop around the circle, summing the magnetic field contribution for each chunk of wire. For some reason, it's not looping through the inner loop each iteration of the outer loop like it should be, and I'm honestly not sure why. The new j value line is to let me see if it's looping properly, and this is my output:

...
13.657 1
13.884 1
14.107 1
14.327 1
14.543 1
14.756 1
14.965 1
15.17 1
15.372 1
New j value!
New j value!
New j value!
New j value!
New j value!
New j value!
New j value!
New j value!
...

The 1 at the end of each float (radius value) is the j value, which stays at 1 for each circle around the loop...

2
  • I suppose I should have mentioned that some of the lines are broken up because the lines were too long to fit in the editing box. so the I and R inputs are all actually one line a piece and I think that's it. Commented Apr 20, 2015 at 19:56
  • What is the desired output? Commented Apr 20, 2015 at 19:56

1 Answer 1

1

You are increasing k but never resetting it to 1. so k * step becomes bigger and bigger until the condition of the inner loop becomes false. At which point is clear that it's not getting executed anymore.

Note that you should avoid while loops when the you are simply iterating over an integer range. Just use for j in range(a, b). This avoid exactly the kind of bug you have in your code.

If I'm not mistaken you can replace the loops with:

area = 2*math.pi*R

for j in range(1, R//step + 1):
    # j*step <= R holds
    for k in range(1, area // step + 1):
        # k * step <= area holds here

where a // b is quotient of a and b.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a million, that one line fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.