I need help minimizing this summation function dependent on what the values of ai are. Can someone help me write a code (in Python 2.7.12) that chooses ai values such that Fmin is minimized? I have a script now, but the problem is that it asks me to input values of ai and then it spits out a value for Fmin. I want a script that selects ai such that Fmin is smallest. Anyways, here’s my (wrong) script:

import math

m = 11

first = 0

second = 0

pi = math.pi

# delare an empty list to hold values of a

a = []

# getting input from user

for i in range(0,m):

a.append( float(input(‘Enter a value of a[‘ + str(i) + ‘]: ‘)) )

# calculation for summation

# in function range(1,m), 1 is included and m is excluded. So effectively we go from 1 to m-1.

first = 0.0

second = 0.0

for j in range(1, m):

temporary_sum_first = 0.0

temporary_sum_second = 0.0

for i in range(0, m):

# this is the summation inside the square brackets

temporary_sum_first += (a[i] * math.cos(2*pi*i*j/m))

temporary_sum_second += ((-a[i])*i* math.sin(2*pi*i*j/m))

# now square it and sum it

first = first + math.pow(temporary_sum_first, 2)

second = second + math.pow(temporary_sum_second, 2)

# at this point, we have finished summing i=0 to i=m-1 for one value of p, we continue this for m-1 values of j.

Fmin=first+second

print(“Summation value:”)

print(Fmin)

And then at some point I’m going to want to plot this function out as well. I appreciate the help!

## Expert Answer

import math

m = 11

first = 0

second = 0

pi = math.pi

# delare an empty list to hold values of a

a = []

# getting input from user

for i in range(0,m):

a.append( float(input(‘Enter a value of a[‘ + str(i) + ‘]: ‘)) )

# calculation for summation

# in function range(1,m), 1 is included and m is excluded. So effectively we go from 1 to m-1.

first = 0.0

second = 0.0

for j in range(1, m):

temporary_sum_first = 0.0

temporary_sum_second = 0.0

for i in range(0, m):

# this is the summation inside the square brackets

temporary_sum_first += (a[i] * math.cos(2*pi*i*j/m))

temporary_sum_second += ((-a[i])*i* math.sin(2*pi*i*j/m))

# now square it and sum it

first = first + math.pow(temporary_sum_first, 2)

second = second + math.pow(temporary_sum_second, 2)

# at this point, we have finished summing i=0 to i=m-1 for one value of p, we continue this for m-1 values of j.

Fmin=first+second

print(“Summation value:”)

print(Fmin)