0% found this document useful (0 votes)
27 views1 page

Exp 8 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)
27 views1 page

Exp 8 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 sympy as sp y_sol = solutions[y]

λ_sol = solutions[λ]

# Define the variables

x, y, λ = sp.symbols('x y λ') # Display the solution

print(f"x = {x_sol}, y = {y_sol}, λ = {λ_sol}")

# Define the objective function f(x, y)

f = x**2 + y**2 # Verify the constraint

constraint_value = g.subs({x: x_sol, y:


y_sol})
# Define the constraint g(x, y) = 0
print(f"Constraint value:
g=x+y-1
{constraint_value}")

# Define the Lagrangian L(x, y, λ)

L=f+λ*g

# Compute the partial derivatives of L with


respect to x, y, and λ

L_x = sp.diff(L, x)

L_y = sp.diff(L, y)

L_λ = sp.diff(L, λ)

# Solve the system of equations L_x = 0, L_y =


0, L_λ = 0

solutions = sp.solve([L_x, L_y, L_λ], (x, y, λ))

# Print the solution

print("Solutions for x, y, and λ:")

print(solutions)

# Verify the solution by substituting the values


of x and y into the constraint

for sol in solutions:

x_sol = solutions[x]

You might also like