Fitz2715
Fitz2715

Reputation: 1

Plotting a defined function

I am extremely new to Python, and in general incompetent when it comes to computers, much less computer lingo so hopefully the title makes sense.

I have made (what I think) is the sine expansion. However, I am trying to plot it, and I either get the graph to show up with nothing on it, or in a couple attempts (although I can now not recreate it) an error saying it can't plot sine(x) because it is undefined, even though as you can see below, it is clearly 'def'-ed.

my code is as follows:

from math import pi
from math import factorial
import matplotlib.pyplot as plt

def sine(x): 
    sum = 0
    n = 0
    q = 1
    while (q > 0.000001):
        q = (x**(2*n +1))/(factorial(2*n + 1))
        if n % 2 ==0:
            sum += q
        else:
            sum -= q
        n += 1
    return sum

for i in range (10):
    z = float(i*pi)
    print sine(z)

plt.plot(i,color='red', alpha=1)
plt.show()

I just threw the red and alpha in there in case the dot was so tiny I was just unable to see it, but alas, adding those two did not show me anything new.

Upvotes: 0

Views: 157

Answers (1)

ssm
ssm

Reputation: 5383

plot takes either 1 or 2 arrays. You can try the following on the terminal:

plot([1,2,3]),plot([1,2,3,4], [4,3,2,1])

Now try:

plot(1) What do you get? Nothing. Because you are tryign to plot a number. i = 9 from your last assignment in the for loop.

# The rest of your stuff ...

x = range(10)
plt.plot(x, map(sine, x) )
plt.show()

Cheers!

Upvotes: 1

Related Questions