Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168733
Image: ubuntu2004
Numerical Optimization is the task of finding the minimum or maximum of a given objective function ff subject to possible constraints. Sage interfaces with various solvers to find optimal values for different types of problems.
#unconstrained optimization var('x y') f = log(1 + (2+x)^2 + (x*y+1)^2) res = minimize(f, [1,1]); res
Optimization terminated successfully. Current function value: 0.000000 Iterations: 9 Function evaluations: 14 Gradient evaluations: 14 (-2.00000022076, 0.49999997085)
var('tx, ty') a = plot3d(f(x=tx, y=ty), (tx,-3,1), (ty, -2,4)) b = point3d([res[0], res[1], f(x=res[0], y=res[1])], color='red', size=20) show(a+b)
#constrained optimization
#c1, c2 >= 0 c1 = lambda p: -p[0]*p[1] + p[0] c2 = lambda p: 3*p[0] + 2 res2 = minimize_constrained(f, [c1, c2], [-2,1]); res2
(-0.666666666667, 1.4999)
b_c = point3d([res2[0], res2[1], f(x=res2[0], y=res2[1])], color='yellow', size=20) p_c1 = parametric_plot3d([x,y,c1([x,y])], (x,-3,1), (y,-2,4), color='orange') p_c2 = parametric_plot3d([x,y,c2([x,y])], (x,-3,1), (y,-2,4), color='purple') show(a + b + b_c + p_c1 + p_c2)