File:Rosenbrock3.gif

Summary

Description
English: Animation of Rosenbrock's function of three variables.
Date
Source Own work
Author nicoguaro
GIF development
InfoField
Source code
InfoField

Python code

"""
Animation of the Rosenbrock function of three variables
"""
import numpy as np
from scipy.optimize import rosen
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation

Axes3D
plt.rcParams["font.size"] = 10
plt.rcParams["mathtext.fontset"] = "cm"

x1, x2 = np.mgrid[-2.5:2.5:51j, -2.5:2.5:51j]
x3  = np.linspace(-2.5, 2.5, 21)
x3  = np.append(x3, x3[-2::-1])
fun = rosen([x1, x2, x3[0]])

fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111, projection='3d')
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 
ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 
ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))

def data_gen(num):
    fun = rosen([x1, x2, x3[num]])
    ax.cla()
    surf = ax.plot_wireframe(x1, x2, fun/1000, linewidth=1, color="#3c3c3c",
                         rstride=51, cstride=51)
    contour = ax.contour(x1, x2, fun/1000, 20)
    ax.set_xlabel(r"$x_1$", fontsize=12)
    ax.set_ylabel(r"$x_2$", fontsize=12)
    ax.set_zlabel(r"$f(x_1, x_2, x_3)/10^3$", fontsize=12)
    ax.set_title(r"$x_3 = {}$".format(x3[num]), fontsize=12, y=1.08)
    ax.set_xlim(-2.5, 2.5)
    ax.set_ylim(-2.5, 2.5)
    ax.set_zlim(0, 16)
    ax.view_init(elev=45, azim=-45)
    return surf, contour

ani = animation.FuncAnimation(fig, data_gen, range(41))
ani.save("rosenbrock3.gif", writer='imagemagick')
plt.show()

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
Category:CC-BY-SA-4.0#Rosenbrock3.gifCategory:Self-published work
Category:Rosenbrock function Category:Animated GIF files
Category:Animated GIF files Category:CC-BY-SA-4.0 Category:PNG created with Matplotlib code Category:Rosenbrock function Category:Self-published work