HW 1 Solutions.pdf - e62 Introduction to Optimization...

  • Stanford University
  • MS&E 111
  • Homework Help
  • danielfelix
  • 7
  • 100% (1) 1 out of 1 people found this document helpful

This preview shows page 1 - 5 out of 7 pages.

e62 Introduction to Optimization Spring 2018 Professor Benjamin Van Roy 20161007 Homework 1 Solutions A. Python Practice Problem 1 a) The script below will generate the required result. import opt111 as op Force, row_labels, column_labels = op.load_matrix("Force.csv") Force = Force.tolist() grav_forces = [] Mass_first = 2 G = 6.67 * 10**-11 for item in Force: Mass_second = item[0] Distance = item[1] grav_forces.append(G*Mass_first*Mass_second/(Distance)**2) print ("The First 10 Gravitational Forces:") print(grav_forces[:10]) print ("The Average of All Gravitational Forces:") print(sum(grav_forces)/len(grav_forces)) Below is the result of running the code above. The First 5 Gravitational Forces: [1.4822222222222224e-11, 2.084375e-12, 5.336e-10, 1.6675e-11, 5.92888888888889e-11] The Average of All Gravitational Forces: 5.64318085053e-11 b) The script below will generate the required result.
import random import numpy Force2 = [[random.randrange(1, 5), random.randrange(1, 10)] for i in range(0, 100)] Force = Force + Force2 tst = numpy.array(Force) op.save_matrix(’test.csv’,tst) Problem 2 The script below will generate the required result. import opt111 c = np.matrix([2,1]) A = np.matrix([[2,3],[5,7]]) b = np.matrix([[60],[70]]) opt_sol, opt_val = opt111.linprog(A,b,c) print ("optimal solution:") print (opt_sol) print ("optimal objective value:") print (opt_val) This is the result of running the code above: Optimization terminated successfully. optimal solution: [[ 14.] [ 0.]] optimal objective value: 28.0
B. Linear Algebra Review Part I Problem 1 Expanding out the first four expressions, we have: a T x = 0 x 2 = - 1 2 x 1 (1) a T x = 1 x 2 = - 1 2 x 1 + 1 2 (2) b T x = 0 x 2 = - 2 x 1 (3) b T x = 1 x 2 = - 2 x 1 + 1 (4) Each of these corresponds to a line in the plane. Expanding out expressions 5 and 6, we have: [ a | b ] x = [0 , 1] T " 1 2 2 1 # " x 1 x 2 # = " 0 1 # (5) [ a | b ] x = [0 , 2] T " 1 2 2 1 # " x 1 x 2 # = " 0 2 # (6) Each of these is a system with a unique solution. Their respective solutions are x = " 2 3 - 1 3 # and x = " 4 3 - 2 3 # .
We thus obtain the following graph: x 1 x 2 1: a T x =0 2: a T x =1 3: b T x =0 4: b T x =1 5 6

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture