Ask Your Question

Revision history [back]

Imprecise nonlinear optimization

I want do do optimization to find the maximum of length of a vector under constraint of a polynomial in sagemath, but it output something strange. Is sagemath actually able to do such work?

#!/usr/bin/env sage
import sys
from sage.all import *
from scipy.optimize import NonlinearConstraint
# Assuming your vector is represented as a list or a vector in SageMath
x, y, z = var('x y z')
vector = [x, y, z]

# Define the optimization problem
initial_guess = [3, 3, 3]

def fun1(vector):
    return -sum([v**2 for v in vector])

def fun2(x,y,z):
    return x**2 + y**2 + z**2 -27

problemm = minimize(fun1, initial_guess, NonlinearConstraint(fun2,0,0))
print(problemm)

It gives output as below:

(600.1187424066786, 600.1187424066786, 600.1187424066786)