Build a contour plot using outer product function and plotly in R

Happy new year!

Today I would like to share with you how I build a contour plot using outer product function and plotly in R. Consider the Rosenbrock banana function:

The following R code uses outer function in R to build the contour of Rosenbrock function.

library(plotly)

x1 <- seq(-2, 2, length.out = 100)
x2 <- seq(-2, 2, length.out = 100)
result <- outer(x1, x2, function(x, y) (1-x)^2+5*(y-x^2)^2 )
p <- plot_ly(x=x1, y=x2, z=result, type = ‘contour’)

And here is the graph:

Leave a Reply

Create your own website, free on WordPress.com.Create your own website.
Start free