# Rosenbrock function

The Rosenbrock function is defined as: $f(x) = \sum_{i=1}^{d-1}[ (x_{i+1}-x_i)^2 + (x_i - 1)^2]$

I will treat the 2D version, which is commonly defined as: $f(x,y) = (1-x)^2 + 100(y-x^2)^2$ Let's import Surrogates and Plots:

using Surrogates
using Plots
default()

Define the objective function:

function f(x)
x1 = x[1]
x2 = x[2]
return (1-x1)^2 + 100*(x2-x1^2)^2
end
f (generic function with 1 method)

Let's plot it:

n = 100
lb = [0.0,0.0]
ub = [8.0,8.0]
xys = sample(n,lb,ub,SobolSample());
zs = f.(xys);
x, y = 0:8, 0:8
p1 = surface(x, y, (x1,x2) -> f((x1,x2)))
xs = [xy[1] for xy in xys]
ys = [xy[2] for xy in xys]
p2 = contour(x, y, (x1,x2) -> f((x1,x2)))
scatter!(xs, ys)
plot(p1, p2, title="True function")

Fitting different Surrogates:

mypoly = PolynomialChaosSurrogate(xys, zs,  lb, ub)
loba = LobachevskySurrogate(xys, zs, lb, ub)
inver = InverseDistanceSurrogate(xys, zs,  lb, ub)

Plotting:

p1 = surface(x, y, (x, y) -> mypoly([x y]))
scatter!(xs, ys, zs, marker_z=zs)
p2 = contour(x, y, (x, y) -> mypoly([x y]))
scatter!(xs, ys, marker_z=zs)
plot(p1, p2, title="Polynomial expansion")
p1 = surface(x, y, (x, y) -> loba([x y]))
scatter!(xs, ys, zs, marker_z=zs)
p2 = contour(x, y, (x, y) -> loba([x y]))
scatter!(xs, ys, marker_z=zs)
plot(p1, p2, title="Lobachevsky")
p1 = surface(x, y, (x, y) -> inver([x y]))
scatter!(xs, ys, zs, marker_z=zs)
p2 = contour(x, y, (x, y) -> inver([x y]))
scatter!(xs, ys, marker_z=zs)
plot(p1, p2, title="Inverse distance surrogate")