Koding Optimasi

From ccitonlinewiki
Jump to: navigation, search

import numpy as np from scipy.optimize import minimize


def calc_drag(x):#drag

   x1 = x[0]
   drag =  21.185*x1**2-125.62*x1 + 143.69
   return drag

def calc_lift(x): #lift

   x1 = x[0]
   lift = -3.5089*x1**2 + 34.626*x1 - 43.619
   return lift

def objective(x): #sudut yang diminimalkan

   return calc_drag(x)

def constraint1(x): #variable SUDUT yang meminimalkan persamaan garis drag

   return 90 - calc_drag(x)

def constraint2(x): #variable SUDUT yang meminimalkan persamaan garis lift

   return 90 - calc_lift(x)

con1=({'type':'ineq','fun':constraint1}) con2=({'type':'ineq','fun':constraint2}) cons = (con1,con2)

x1_guess = 50

x0 = np.array([x1_guess])

sol = minimize(objective,x0, method='SLSQP',constraints=cons, options={'disp':True})

xopt = sol.x forceopt = -sol.fun

dragopt = calc_drag(xopt) # drag optimal liftopt = calc_lift(xopt) # lift optimal

print ('sudut optimal = '+str(-xopt[0])) print ('total force optimal = '+str(forceopt)) print ('drag force optimal = '+str(-dragopt)) print ('lift force optimal = '+str(liftopt))