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?

2
  • 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 Commented Nov 17, 2016 at 9: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. Commented Nov 17, 2016 at 9:28

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! The Surface(), surface() syntax makes things much easier.
which backand and color props did you use?
This looks (from the font) like the PyPlot backend.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.