Plots of nonlinear functions explored in Lec3 (Jan 20) and Lec4 (Jan 22)¶

InĀ [1]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import plotly.graph_objects as go
InĀ [2]:
# Define the function f(x) = x^2 * sin(x) + x
def f(x):
    return x**2 * np.sin(x) + x
InĀ [3]:
x = np.linspace(-10, 10, 1000)
y = f(x)

# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(x, y, label=r'$f(x) = x^2 \sin(x) + x$')

# Add axis lines, labels, title, and grid
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.xlabel('$x$')
plt.ylabel('$f(x)$')
# plt.title('$f(x) = x^2 sin(x) + x$')
plt.legend()
plt.grid(True, linestyle='--', alpha=0.5)
No description has been provided for this image
InĀ [4]:
# separable bivariate function 
def G(x, y):
    return f(x) + f(y)
InĀ [19]:
# Generate a grid of x and y values
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
X, Y = np.meshgrid(x, y)
Z = G(X, Y)

# Create the 3D plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot the surface with a color map
surf = ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')

# Add labels and title using LaTeX formatting
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_zlabel('$G(x, y)$')
ax.set_title('$G(x, y) = f(x) + f(y)$ where $f(x) = x^2 sin(x) + x$')

# Add a color bar for reference
fig.colorbar(surf, ax=ax, shrink=0.5, aspect=5)
Out[19]:
<matplotlib.colorbar.Colorbar at 0x27912d95f70>
No description has been provided for this image

Interactive 3D plot, which may not show up when the notebook is exported as a html file¶

InĀ [6]:
# Create the interactive 3D surface
fig = go.Figure(data=[go.Surface(z=Z, x=X, y=Y, colorscale='Viridis')])

# Add labels and title
fig.update_layout(
    title='$G(x, y) = f(x) + f(y)$',
    scene=dict(
        xaxis_title='X',
        yaxis_title='Y',
        zaxis_title='G(x, y)'
    ),
    width=800,
    height=800
)

# Show the plot 
fig.show()
InĀ [7]:
# nonseparable bivariate function 
def d(x, y):
    return x**2 * np.sin(y)
InĀ [15]:
# Draw 3D surface of nonseparable function

# Generate a grid of x and y values
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
X, Y = np.meshgrid(x, y)
Z = d(X, Y)
InĀ [16]:
# Create the 3D plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot the surface with a color map
surf = ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')

# Add labels and title using LaTeX formatting
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_zlabel('$G(x, y)$')
ax.set_title('$d(x, y) = x^2 sin(y)$')

# Add a color bar for reference
fig.colorbar(surf, ax=ax, shrink=0.5, aspect=5)
Out[16]:
<matplotlib.colorbar.Colorbar at 0x2790fd11a30>
No description has been provided for this image
InĀ [9]:
fig = go.Figure(data=[go.Surface(z=Z, x=X, y=Y, colorscale='Viridis')])

# Add labels and title
fig.update_layout(
    title='$d(x, y) = x^2 sin(y)$',
    scene=dict(
        xaxis_title='X',
        yaxis_title='Y',
        zaxis_title='d(x, y)'
    ),
    width=800,
    height=800
)

# Show the plot 
fig.show()

$D(x,y) = x^2 + y^4, E(x,y) = x^2 - y^4$, and $H(x,y) = x^2 + y^3$¶

All three have the same PSD Hessian $H = \begin{bmatrix} 2 & 0 \\ 0 & 0 \end{bmatrix}$ at $(0,0)$

InĀ [10]:
def D(x,y):
    return x**2 + y**4

def E(x,y):
    return x**2 - y**4

def H(x,y):
    return x**2 + y**3
InĀ [21]:
# Generate a grid of x and y values
x = np.linspace(-1, 1, 100)
y = np.linspace(-1, 1, 100)
X, Y = np.meshgrid(x, y)
InĀ [26]:
# Create the 3D plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot the surface with a color map
surf = ax.plot_surface(X, Y, D(X,Y), cmap='viridis', edgecolor='none')

# Add labels and title using LaTeX formatting
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_zlabel('$D(x, y)$')
ax.set_title('$D(x, y) = x^2 + y^4$')

# Add a color bar for reference
fig.colorbar(surf, ax=ax, shrink=0.5, aspect=5)
Out[26]:
<matplotlib.colorbar.Colorbar at 0x27914d25340>
No description has been provided for this image
InĀ [27]:
fig = go.Figure(data=[go.Surface(z=D(X,Y), x=X, y=Y, colorscale='Viridis')])

# Add labels and title
fig.update_layout(
    title='$D(x, y) = x^2 + y^4$',
    scene=dict(
        xaxis_title='X',
        yaxis_title='Y',
        zaxis_title='D(x, y)'
    ),
    width=800,
    height=800
)

# Show the plot 
fig.show()
InĀ [28]:
# Create the 3D plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot the surface with a color map
surf = ax.plot_surface(X, Y, E(X,Y), cmap='viridis', edgecolor='none')

# Add labels and title using LaTeX formatting
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_zlabel('$E(x, y)$')
ax.set_title('$E(x, y) = x^2 - y^4$')

# Add a color bar for reference
fig.colorbar(surf, ax=ax, shrink=0.5, aspect=5)
Out[28]:
<matplotlib.colorbar.Colorbar at 0x2791568d790>
No description has been provided for this image
InĀ [29]:
# Generate a grid of x and y values
x = np.linspace(-1, 1, 100)
y = np.linspace(-1, 1, 100)
X, Y = np.meshgrid(x, y)

fig = go.Figure(data=[go.Surface(z=E(X,Y), x=X, y=Y, colorscale='Viridis')])

# Add labels and title
fig.update_layout(
    title='$E(x, y) = x^2 - y^4$',
    scene=dict(
        xaxis_title='X',
        yaxis_title='Y',
        zaxis_title='E(x, y)'
    ),
    width=800,
    height=800
)

# Show the plot 
fig.show()
InĀ [30]:
# Create the 3D plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot the surface with a color map
surf = ax.plot_surface(X, Y, H(X,Y), cmap='viridis', edgecolor='none')

# Add labels and title using LaTeX formatting
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_zlabel('$H(x, y)$')
ax.set_title('$H(x, y) = x^2 + y^3$')

# Add a color bar for reference
fig.colorbar(surf, ax=ax, shrink=0.5, aspect=5)
Out[30]:
<matplotlib.colorbar.Colorbar at 0x27915776b10>
No description has been provided for this image
InĀ [31]:
# Generate a grid of x and y values
x = np.linspace(-1, 1, 100)
y = np.linspace(-1, 1, 100)
X, Y = np.meshgrid(x, y)

fig = go.Figure(data=[go.Surface(z=H(X,Y), x=X, y=Y, colorscale='Viridis')])

# Add labels and title
fig.update_layout(
    title='$H(x, y) = x^2 + y^3$',
    scene=dict(
        xaxis_title='X',
        yaxis_title='Y',
        zaxis_title='H(x, y)'
    ),
    width=800,
    height=800
)

# Show the plot 
fig.show()