Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 2.7.12 :I\'m trying to write a minimzation routine which takes my initial

ID: 3848541 • Letter: P

Question

Python 2.7.12 :I'm trying to write a minimzation routine which takes my initial guesses and tries to minimize a function.. The first script is one that allows me to input values for a_i, and outputs the value of the function. If you use m = 3, a_0 = .333, a_1 = .444, and a_2 = .222, you should get something on the order of 10^-31. In my second script, if I use the same values as my guess, I don't get the same initial objective, which I should. Instead I get .54... Can someone tell me how what I've coded in the second script doesn't match what I did in the first? I use x_i in the second script because It automatically attributes the variable to x and not a.

First script:

import math
m = 3

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):

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)

Second Script

### Minimizing the Minimization Function###
import numpy as np
import math
from scipy.optimize import minimize
m = 3
pi = math.pi

def objective(x):
# 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):

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


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

Fmin=first+second
return Fmin

  
  

x0 = [.333,.444,.222] #initial guesses

print('Initial Objective: ' + str(objective(x0))) #should be the same as first script

#Bounds placed on xi
b = (-1.0,1.0)
bnds = (b,b,b)
solution = minimize(objective,x0,method = 'SLSQP',bounds = bnds)

x = solution.x

print('Final Objective: ' + str(objective(x)))

#Displaying solution set
print('solution')
print('x0 = ' + str(x[0]))
print('x1 = ' + str(x[1]))
print('x2 = ' + str(x[2]))

Explanation / Answer

Out dent lines 22, 23 in the second file

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

these two lines that's it