Q:

What is causing this simple function to plot poorly?

This function is giving me some grief, I think something is possible rounding or not behaving like I expect it to. I've tried changing phi in small increments and the value of AFn jumps around in a way that it definitely shouldn't. It should be a round radiation pattern like this: 
but it's coming out spikey and discrete-looking, like this:
It has the right sort of trends, but there's clearly something wrong.
Here's the code: 
import numpy as np
import matplotlib.pyplot as plt
N = 7 #number of arrays 
phi = np.arange(0, np.pi, np.pi/100)`

def ArrayFact(phi):
    i, k, d = 0.0, 4.0, 1.0
    while i<N:
        d = d+2    
        AF = AF + np.cos( k*d * np.cos(phi) ) #array factor formula for nonuniform baselines
        i=i+1
    AFn = (1 / float(N)) * AF
    return abs(AFn)

plt.figure(figsize=(12, 8))
plt.polar(phi, ArrayFactor(phi, N, k) * np.sin(phi))
plt.legend(loc='lower right', bbox_to_anchor = (1, 0))
plt.show()

Any help would be greatly appreciated.
Edit: Thanks guys, this post was a mess from start to finish but it's all worked out for the best for all of us. I think we've all learnt something here, maybe not about Python but definitely about people.

A:

After cleaning up the code, I was able to get this to plot rounded curves:
import numpy as np
import matplotlib.pyplot as plt
N = 7 #number of arrays 
phi = np.arange(0, np.pi, np.pi/500) # Increased it from np.pi/100 to get the roundedness

def ArrayFact(phi):
    i, k, d = 0, 4.0, 1
    AF = 0
    while i<N:
        d = d+2    
        AF = AF + np.cos( k*d * np.cos(phi) ) #array factor formula for nonuniform baselines
        i=i+1
    AFn = (1 / float(N)) * AF
    return abs(AFn)

plt.figure(figsize=(12, 8))
plt.polar(phi, ArrayFact(phi) * np.sin(phi))
plt.legend(loc='lower right', bbox_to_anchor = (1, 0))
plt.show()

