I need to find a way to add new terms to the function:

f(x) = 4sin(x)/pi

such that the new function looks like this:

f(x) = 4sin(x)/pi + 4sin(3x)/3pi + 4sin(5x)/5pi + 4sin(7x)/7pi … up to 201.

I was thinking I could create the function with a `for`

loop inside that would iterate using a list `n`

but there must be a way to do it. I am using the `math`

and `numpy`

libraries. This is what I have so far:

# original function f(x) def f(x): return (4/math.pi)*math.sin(x) # create list (n) composed of odd numbers up to 201 n = list(range(1, 202, 2)) # new function with added terms def newf(x): for i in n: sum (4/n[i]*math.pi)*math.sin(n[i]*x)

I am still relatively new to Python and cannot find a similar example to troubleshoot with. Am I thinking through this correctly? I’m unsure why this code would not work.

I do not know how to do math notation in Stack Overflow, so I apologize for that!

You are close to the solution! The reason is not working is because inside your loop nothing is retaining the computation of the previous iterations.
And you can simply use `i`

without accessing `n[i]`

, which eventually will throw you an `IndexError: list index out of range`

The code should provide you what you need:

# original function f(x) import math def f(x): return (4/math.pi)*math.sin(x) # create list (n) composed of odd numbers up to 201 n = list(range(1, 202, 2)) def new_f(x): return sum([(4/i*math.pi)*math.sin(i*x) for i in n])

## Recent Comments