import numpy as np
import matplotlib.pyplot as plt
# Given data
= 2
n = np.array([3, 2])
y = np.array([0.5, 1])
sigma = np.array([[1, 0], [-1, 1]])
A
# Compute A^(1/2). Given A's structure, we'll use a direct approach for this specific case.
# For a general case, we would use a method like spectral decomposition.
# A_half = np.linalg.cholesky(A)
# Define Sigma matrix
= np.diag(sigma)
Sigma
# Generate a grid of points for plotting
= np.linspace(-1, 5, 2000)
x_range = np.linspace(-1, 5, 2000)
y_range = np.meshgrid(x_range, y_range)
X, Y = np.array([X.flatten(), Y.flatten()]).T
Z
# Function to check condition for set U using A directly
def in_set_U(z, A, y):
return np.linalg.norm(np.dot(A, z - y)) <= 1
# Adjusting conditions based on the direct use of A and Sigma
= np.array([in_set_U(z, A, y) for z in Z])
U_condition = np.array([np.max(np.abs(Sigma.dot(z))) <= 1 for z in Z])
V_condition
# Plotting the adjusted sets
=(5, 5))
plt.figure(figsize=":")
plt.grid(linestyle
# Plot points in V
0], Z[V_condition, 1], color='red', alpha=0.5, label='Set V', s=1e-1)
plt.scatter(Z[V_condition,
# Plot points in U
0], Z[U_condition, 1], color='blue', alpha=0.5, label='Set U', s=1e-1)
plt.scatter(Z[U_condition,
r'$x_1$')
plt.xlabel(r'$x_2$')
plt.ylabel(2.75, 1.75, s=r"$U$", fontsize=36)
plt.text(0.25, -0.25, s=r"$V$", fontsize=36)
plt.text(# plt.title('Sets U and V')
"convex_intersection.png", dpi=1000)
plt.savefig( plt.show()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Ellipse
# Given data
= np.array([3, 2])
y = np.array([0.5, 1])
sigma = np.array([[1, 0], [-1, 1]])
A = np.diag(sigma)
Sigma
# Generate the grid of points again
= np.linspace(-1, 5, 400)
x_range = np.linspace(-1, 5, 400)
y_range = np.meshgrid(x_range, y_range)
X, Y = np.array([X.flatten(), Y.flatten()]).T
Z
# Recheck conditions for U and V sets
= np.array([np.linalg.norm(np.dot(A, z - y)) <= 1 for z in Z])
U_condition = np.array([np.max(np.abs(Sigma.dot(z))) <= 1 for z in Z])
V_condition
# Start the plot
=(6, 6))
plt.figure(figsize= plt.gca()
ax =":")
plt.grid(linestyle
# Add an ellipse for set V, centered at y with width and height determined by 2*sigma values
= Ellipse(xy=y, width=2*sigma[0], height=2*sigma[1], edgecolor='red', fc='None', lw=2)
ellipse
ax.add_patch(ellipse)
# Calculate the bounds for the rectangle representing set U
= Z[V_condition]
V_points = np.min(V_points, axis=0)
lower_left = np.max(V_points, axis=0)
upper_right = Rectangle(lower_left, upper_right[0] - lower_left[0], upper_right[1] - lower_left[1], edgecolor='blue', fc='None', lw=2)
rectangle
ax.add_patch(rectangle)
'z1')
plt.xlabel('z2')
plt.ylabel('Set V (Ellipse)', 'Set U (Rectangle)'])
plt.legend(['Approximation of Sets U and V with Ellipse and Rectangle')
plt.title(
'equal') # Ensuring equal scaling for both axes
plt.axis(-1, 5)
plt.xlim(-1, 5)
plt.ylim( plt.show()