Building a 2d function as a surface in 3d space using `Plots.jl`

I have the following problem when building with Plots.jl . I like to build the rosenbrock function

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

as a surface that is awaiting input of a 2d Tuple{Float64,Float64} .

What I can think of is this:

 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 gives this graph: irregular rosenberk surface

I think I messed up some measurements, but I donโ€™t see that I was wrong.

Do I need to insert mapping calculations for y and x to get the result?

+5
source share
1 answer

After a quick study of the Rosenbrock function, I found and will fix it if Im wrong, but you need to specify y -vector, which is supposed to enclose it in z or something like that. Someone else tried the same as shown here but using graphics

The solution is as follows: 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) 

The result is

enter image description here

side note

I'm glad I was looking for this because I was looking for a 3D plotter for Julia except PyPlot (since it can be a bit of a hassle to configure users of my program), and it even looks better and the images can be rotated.

+7
source

Source: https://habr.com/ru/post/1259891/


All Articles