4

I have the following problem while plotting with Plots.jl. I like to plot the rosenbrock function

rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2

as surface, which expects a 2d Tuple{Float64,Float64} as input.

What I could come up with, is the following:

using Plots
gr()

rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2

ts = linspace(-1.0, 1.0, 100)
x = ts
y = map(rosenbrock, [(x, z) for (x,z) in zip(ts,ts)])
z = map(rosenbrock, [(x, y) for (x,y) in zip(ts,ts)])
# plot(x, x, z)
plot(x, y, z, st = [:surface, :contourf])

which yields this plot: wrong rosenbrock surface

I think I messed up some dimensions, but I don't see what I got wrong.

Do I have to nest the calculation of the mappings for y and x to get the result?

swiesend
  • 1,051
  • 1
  • 13
  • 23
  • I suggest that you try to frame your question a bit clearer, I have no idea what you are trying to do and I have no idea what the question is – isebarn Nov 17 '16 at 09:03
  • @isebarn I've updated the question. I like to plot the function as a surface, but I am not sure how to do it. – swiesend Nov 17 '16 at 09:28

1 Answers1

7

After a quick investigation of the Rosenbrock function I found, and correct me if Im wrong, but you need to specify the y-vector you arent supposed to nest it within z or anything like that Someone else tried this same thing as shown here but using Plots

the solution is as follows as done by Patrick Kofod Mogensen

using Plots

function rosenbrock(x::Vector)
  return (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
end

default(size=(600,600), fc=:heat)
x, y = -1.5:0.1:1.5, -1.5:0.1:1.5
z = Surface((x,y)->rosenbrock([x,y]), x, y)
surface(x,y,z, linealpha = 0.3)

This results in

enter image description here

side note

Im glad I searched for this as I've been searching for a 3D plotter for Julia other than PyPlot (as it can be a bit of a hassle to set up for the users of my program) and this even looks better and images can be rotated.

isebarn
  • 3,812
  • 5
  • 22
  • 38