0

Im new to programming and have been set a task to create a function that will sum up an even Fourier series (so no bn's) for 50 different x's between pi and -pi if the user inputs the respective coefficients. As far as I can tell, I need to set up a double for loop, one to set the values of X, and one to do the sum with each of these values and their respective coefficients.

I dont get an error message, and my array of values for X is correct. However, when I input a few coefficients to test it, the output of the sum (cosSumValues) is just an array of 0's.

n is an array of numbers 1-len(coefficients), and coefficients is what the user inputs

Here's my double for-loop:

for i in range(nx):
    X = AllValues[i]
    for j in range(1, len(coefficients)):
        cosSumValues[j] = cosSumValues[j] + coefficients[j]*numpy.cos(n[j]*X)
        print xValues, cosSumValues
        return xValues, cosSumValues

Thanks for your help.

alko
  • 46,136
  • 12
  • 94
  • 102
user3088440
  • 144
  • 7
  • 6
    It seems strange that you have the "return" statement right after the "print" statement. Given that this code is probably in a function. The return statement will exit the loop. – WLin Dec 10 '13 at 20:23
  • Are you using floating point numbers? Because if you just use integers python will round them. For example, 2/3=0 but 2.0/3.0=0.66666666666666663. – AronVietti Dec 10 '13 at 20:24
  • 1
    Some other small comments: vectors/lists use 0-based indexing in Python, so maybe you need `for j in range(len(coeffs))`. Also, you can use [+=](http://stackoverflow.com/q/2347265/2647279) to add something to `cosSumValues`. – Bas Swinckels Dec 10 '13 at 20:24
  • @AronVietti no type coersion will happen in the code provided. – alko Dec 10 '13 at 20:28
  • What are the values in `n`? Maybe you could make this a http://sscce.org/. – Benjamin Bannier Dec 10 '13 at 20:43

0 Answers0