Rosenbrock function
f(x,y) = 100(y-x^2)^2 + (1-x)^2
f (generic function with 1 method)
using PyPlot
x = -2:0.1:2
y = -2:0.1:2
X = repmat(x',length(y),1)
Y = repmat(y,1,length(x))
Z = map(f,X,Y)
fig = figure(figsize=(8,8))
ax = fig[:add_subplot](1,1,1, projection = "3d")
ax[:plot_surface](X, Y, Z, rstride=2,edgecolors="k", cstride=1, cmap=ColorMap("jet"), alpha=0.8, linewidth=0.25)
xlabel("x")
ylabel("y")
PyObject <matplotlib.text.Text object at 0x31b1f9890>
x = -2:0.01:2
y = -2:0.01:2
X = repmat(x',length(y),1)
Y = repmat(y,1,length(x))
Z = map(f,X,Y)
levels=float(0.0:0.25:10.0)
fig = figure(figsize=(10,6))
ax = fig[:add_subplot](1,2,1)
cp = ax[:contour](X, Y, Z, linewidth=2.0, levels=levels)
ax[:clabel](cp, inline=1, fontsize=10)
xlabel("x")
ylabel("y")
PyObject <matplotlib.text.Text object at 0x31ca48890>
x = 0:0.01:2
y = 0:0.01:2
X = repmat(x',length(y),1)
Y = repmat(y,1,length(x))
Z = map(f,X,Y)
levels=float(0.0:0.25:2.0)
fig = figure(figsize=(10,6))
ax = fig[:add_subplot](1,2,1)
cp = ax[:contour](X, Y, Z, linewidth=2.0, levels=levels)
ax[:clabel](cp, inline=1, fontsize=10)
xlabel("x")
ylabel("y")
x = .95:0.001:1.05
y = .95:0.001:1.05
X = repmat(x',length(y),1)
Y = repmat(y,1,length(x))
Z = map(f,X,Y)
levels=float(0.0:0.01:.1)
ax = fig[:add_subplot](1,2,2)
cp = ax[:contour](X, Y, Z, linewidth=2.0, levels=levels)
ax[:clabel](cp, inline=1, fontsize=10)
xlabel("x")
ylabel("y")
tight_layout()
using SymPy
@syms x y
f(x,y) = 100(y-x^2)^2 + (1-x)^2
[diff(f(x,y), x); diff(f(x,y), y)]
WARNING: Method definition f(Any, Any) in module Main at In[1]:1 overwritten at In[5]:4.
[diff(f(x,y), x, x) diff(f(x,y), x, y); diff(f(x,y), y, x) diff(f(x,y), y, y)]
From $\nabla_y f(x,y) = 0$, we have $y =x^2$. Plugging-in this to $\nabla_x f(x,y) = 0$, we get $$ -400x( -x^2 + x^2 ) + 2x -2 = 0 \; \Rightarrow \; x=1 $$ That is, $(x,y) = (1,1)$ is the only stationary point of the rosenbrock function
A = [0 0 .5 1; .5 0 0 0; .5 1 0 0; 0 0 .5 0]
using JuMP
using Ipopt
m = Model(solver = IpoptSolver())
@variable(m, r[1:4] >= 0.)
@objective(m, Min, dot(r-A*r, r-A*r))
status = JuMP.solve(m)
****************************************************************************** This program contains Ipopt, a library for large-scale nonlinear optimization. Ipopt is released as open source code under the Eclipse Public License (EPL). For more information visit http://projects.coin-or.org/Ipopt ****************************************************************************** This is Ipopt version 3.12.4, running with linear solver mumps. NOTE: Other linear solvers might be more efficient (see Ipopt documentation). Number of nonzeros in equality constraint Jacobian...: 0 Number of nonzeros in inequality constraint Jacobian.: 0 Number of nonzeros in Lagrangian Hessian.............: 100 Total number of variables............................: 4 variables with only lower bounds: 4 variables with lower and upper bounds: 0 variables with only upper bounds: 0 Total number of equality constraints.................: 0 Total number of inequality constraints...............: 0 inequality constraints with only lower bounds: 0 inequality constraints with lower and upper bounds: 0 inequality constraints with only upper bounds: 0 iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls 0 9.9999800e-05 0.00e+00 1.01e+00 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0 1 1.0975053e-02 0.00e+00 1.15e-01 -1.0 1.01e-01 - 8.96e-01 1.00e+00f 1 2 1.3644633e-04 0.00e+00 2.42e-02 -2.5 1.53e-01 - 9.23e-01 1.00e+00f 1 3 2.8575494e-07 0.00e+00 5.36e-04 -3.8 2.52e-02 - 9.56e-01 1.00e+00f 1 4 4.0062266e-11 0.00e+00 1.27e-05 -5.7 7.50e-03 - 9.88e-01 1.00e+00f 1 5 7.2858386e-17 0.00e+00 8.44e-09 -8.6 7.99e-04 - 9.99e-01 1.00e+00f 1 Number of Iterations....: 5 (scaled) (unscaled) Objective...............: 7.2858385991025898e-17 7.2858385991025898e-17 Dual infeasibility......: 8.4372834151355382e-09 8.4372834151355382e-09 Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00 Complementarity.........: 2.5089262935572296e-09 2.5089262935572296e-09 Overall NLP error.......: 8.4372834151355382e-09 8.4372834151355382e-09 Number of objective function evaluations = 6 Number of objective gradient evaluations = 6 Number of equality constraint evaluations = 0 Number of inequality constraint evaluations = 0 Number of equality constraint Jacobian evaluations = 0 Number of inequality constraint Jacobian evaluations = 0 Number of Lagrangian Hessian evaluations = 5 Total CPU secs in IPOPT (w/o function evaluations) = 0.156 Total CPU secs in NLP function evaluations = 0.056 EXIT: Optimal Solution Found.
:Optimal
@show rval = getvalue(r)
norm(rval-A*rval, Inf)
rval = getvalue(r) = [0.297715,0.148858,0.297715,0.148858]
4.218633831154506e-9