0% found this document useful (0 votes)
10 views

Exp 9 Num Using Py Code

Uploaded by

K. Vishal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Exp 9 Num Using Py Code

Uploaded by

K. Vishal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

import numpy as np print("Optimization was successful.

")

from scipy.optimize import minimize print("Optimal solution:", result.x)

print("Objective function value at optimal


solution:", result.fun)
# Objective function to minimize: f(x) = x1^2 +
x2^2 else:

def objective(x): print("Optimization failed.")

return x[0]**2 + x[1]**2 print(result.message)

# Equality constraint: g1(x) = x1 + x2 - 1 = 0

def equality_constraint(x):

return x[0] + x[1] - 1

# Inequality constraint: g2(x) = x1 - x2 >= 0 (or


x1 - x2 >= 0 can be expressed as -g2(x) <= 0)

def inequality_constraint(x):

return x[0] - x[1]

# Initial guess

x0 = np.array([0.5, 0.5])

# Define the constraints in a dictionary

constraints = [

{'type': 'eq', 'fun': equality_constraint},

{'type': 'ineq', 'fun': inequality_constraint}

# Perform the optimization

result = minimize(objective, x0,


constraints=constraints)

# Print the results

if result.success:

You might also like