#149 Optimization in python

import os
import numpy as np
from scipy.optimize import minimize

os.chdir(r'D:\projects\wordpress\ex149')
os.getcwd()

def obj(x):
    return (1-x[0])**2 + 100*(x[1]-x[0]**2)**2

def con(x):
    g=np.zeros(2)
    g[0]=1-x[0]**2-x[1]**2
    g[1]=5-x[0]-3*x[1]
    return g

x0=[5.0,5.0]
constraints = {'type': 'ineq','fun': con}
options={'disp':True}
res = minimize(obj,x0,constraints=constraints,options=options)
print('x =',res.x)
print('f =',res.fun)
print(res.success)

Output:

Optimization terminated successfully    (Exit mode 0)
            Current function value: 0.04567481620594842
            Iterations: 21
            Function evaluations: 73
            Gradient evaluations: 21
x = [0.78641211 0.61770218]
f = 0.04567481620594842
True

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading