Fourier series coefficients

I am a beginner, had to obtain fourier series coefficients and I wrote a code, But I get errors and dont know why. can anyone correct my code?

def an(t,n):
    an= []
    f=4+3*np.sin(t)-4*np.cos(t)
    for i in range (1,n+1):
        an=f*np.cos(i*t)
    return an
def bn(t,n):
    bn= []
    f=4+3*np.sin(t)-4*np.cos(t)
    for i in range (1,n+1):
        bn=f*np.sin(i*t)
    return bn
n=10
for i in range (n):
    an[i]=np.round(quad(an[i],0,2*np.pi,args=(n))[0]/np.pi)
    bn[i]=np.round(quad(bn[i],0,2*np.pi,args=(n))[0]/np.pi)
print("as= ",an, "& bs= ",bn)

Hello and welcome to the forum @znes

That was something to start with.
There is a lot of errors in this code.
I strongly suggest you to go through the book first.

If you want to try anyway perhaps I can give you this for a start:


# First define a function taking two parameters
def fourier_an(a, b):
    # here you can start to code your formula
    
    # not a fourier series but shows how the parameters are processed:
    result = a + b
    # "result" is a variable
    #  You can give the variables any name you want.
    # but it is confusing if is called the same as a function.
    return result

# and here I call this function with two arguments I want to calculate
fourier_an(8, 2)

# the output will be: 10

Probably there is a module to install / import to help calculate these advanced formulas.
You will learn about import a module later in the book (Learn python the hard way).
Start with the book today. It will feel wonderful when you can write your own code that calculate Fourier series coeffients.

Alright, so first thing is imagine I’m baking a pie, and I call you up and I say this:

“Hey @znes, I’m baking an apple pie and it tastes weird.”

Could you help me fix my pie with just that? Just “it tastes weird” isn’t enough information. I’d have to say something like this:

“Hey @znes, I’m baking an apple pie and it tastes very salty and the apples aren’t cooked right.”

You could then tell me that I probably put too much salt in and didn’t cook it long enough, but without what is wrong you couldn’t help me.

Now, what you should do is tell me what’s happening with your code, rather than just say “it has an error”. You should do that by running it and then pasting the output into a reply, but do it like this:

[code]
# your error output here
[/code]

Then everyone can actually help you. The other thing to do is to delete this and rewrite it but in small chunks. You most likely just typed this all in then ran it once. Try typing in a little bit at a time instead.

Are you using imports like math ?