import numpy as np
import matplotlib.pyplot as plt
def linear_transformation(x, y):
= 2*x + y
u = x - y
v return u, v
= np.linspace(-5, 5, 100)
x_range = np.linspace(-5, 5, 100)
y_range = np.meshgrid(x_range, y_range)
x, y = linear_transformation(x, y)
u, v # Jacobian matrix for the linear transformation
= np.array([[2, 1],
Jacobian 1, -1]])
[=(10, 5))
plt.figure(figsize# Original grid
1, 2, 1)
plt.subplot("Original Grid (x, y)")
plt.title(=10, scale_units='xy')
plt.quiver(x, y, np.ones_like(x), np.zeros_like(y), scale-5, 5)
plt.xlim(-5, 5)
plt.ylim("x")
plt.xlabel("y")
plt.ylabel(
# Transformed grid
1, 2, 2)
plt.subplot("Transformed Grid (u, v)")
plt.title(=10, scale_units='xy')
plt.quiver(u, v, np.ones_like(u), np.zeros_like(v), scale-10, 10)
plt.xlim(-10, 10)
plt.ylim("u")
plt.xlabel("v")
plt.ylabel(
plt.tight_layout() plt.show()
Change of Variables with Jacobian Matrix
In this notebook, we’ll delve into the fundamental concept of changing variables using the Jacobian matrix. Discover why it’s crucial in math, physics, and statistics. We’ll demonstrate the process, visualize transformations, and explore the significance of the Jacobian matrix in coordinate transformations.
Its Importance:
We often change the variables in math to make integrals easier to solve. But there’s another reason for doing this – to make the area or volume we’re working with more convenient to handle. When we switch to polar, cylindrical, or spherical coordinates, it’s usually straightforward to figure out the new boundaries of the area or volume. However, this isn’t always the case. So, before we jump into using variable changes in multiple integrals, we first need to understand how these changes affect the shape and size of the region we’re working with.
import numpy as np
import matplotlib.pyplot as plt
def nonlinear_transformation(x, y):
= x
u = 6 * y # Scale the y-coordinate by a factor of 6
v return u, v
= np.linspace(-5, 5, 1000)
x_range = np.linspace(-5, 5, 1000)
y_range = np.meshgrid(x_range, y_range)
x, y = nonlinear_transformation(x, y)
u, v
=(10, 5))
plt.figure(figsize# Original grid
1, 2, 1)
plt.subplot("Original Grid (x, y)")
plt.title(# Plot the original ellipse with the correct contour level
**2 + (y/6)**2, levels=[1], colors='b')
plt.contour(x, y, x-5, 5)
plt.xlim(-50, 50) # Adjust the y-coordinate range
plt.ylim("x")
plt.xlabel("y")
plt.ylabel(
# Transformed grid
1, 2, 2)
plt.subplot("Transformed Grid (u, v)")
plt.title(# Plot the transformed circle
**2 + v**2, levels=[1], colors='r')
plt.contour(u, v, u-10, 10)
plt.xlim(-10, 10) # Adjust the y-coordinate range for visualization
plt.ylim("u")
plt.xlabel("v")
plt.ylabel(
plt.tight_layout() plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Define ellipse equation: x^2 + y^2 / 36 = 1
= 6 # Semi-major axis length
a = 6 # Semi-minor axis length
b
# Generate grid
= np.linspace(-10, 10, 200)
x = np.linspace(-10, 10, 200)
y = np.meshgrid(x, y)
X, Y
# Define ellipse equation
= X**2 + Y**2 / b**2 - 1
ellipse
# Define arrow properties
= 0.5
arrow_length = 'gray'
arrow_color = 0.5
arrow_alpha = 20 # Number of arrows
num_arrows
# Compute arrow positions
= np.linspace(0, len(x) - 1, num_arrows, dtype=int)
arrow_positions
# Apply transformation: u = x, v = 6*y
= X
U = 6 * Y
V
# Define transformed circle equation
= U**2 + V**2 - a**2
circle
# Define arrow directions (all pointing in a single direction)
# Change this value to -1 to reverse the direction
= np.array([1])
arrow_direction
# First plot: Ellipse with uniform arrows
=(8, 8))
plt.figure(figsize=[1], colors='b',
plt.contour(X, Y, ellipse, levels='dashed', label='Ellipse')
linestyles
plt.quiver(X[arrow_positions[:, np.newaxis], arrow_positions], Y[arrow_positions[:, np.newaxis], arrow_positions],
arrow_direction, arrow_direction,=10, pivot='mid', color=arrow_color, alpha=arrow_alpha, label='Arrows')
scale'X')
plt.xlabel('Y')
plt.ylabel('Ellipse with Uniform Arrows')
plt.title(
plt.legend()'equal')
plt.axis(True)
plt.grid(
plt.show()
# Second plot: Transformed Circle with uniform arrows
=(8, 8))
plt.figure(figsize=[1], colors='r',
plt.contour(U, V, circle, levels='solid', label='Circle')
linestyles
plt.quiver(U[arrow_positions[:, np.newaxis], arrow_positions], V[arrow_positions[:, np.newaxis], arrow_positions],
arrow_direction, arrow_direction,=10, pivot='mid', color=arrow_color, alpha=arrow_alpha, label='Arrows')
scale'U')
plt.xlabel('V')
plt.ylabel('Transformed Circle with Uniform Arrows')
plt.title(
plt.legend()'equal')
plt.axis(True)
plt.grid( plt.show()
C:\Users\asus\AppData\Local\Temp\ipykernel_14680\3513024967.py:37: UserWarning: The following kwargs were not used by contour: 'label'
plt.contour(X, Y, ellipse, levels=[1], colors='b', linestyles='dashed', label='Ellipse')
C:\Users\asus\AppData\Local\Temp\ipykernel_14680\3513024967.py:51: UserWarning: The following kwargs were not used by contour: 'label'
plt.contour(U, V, circle, levels=[1], colors='r', linestyles='solid', label='Circle')
The above plots demonstrate how the grid changes and figures are transformed after changing variables using the jacobian. Next we see how transforming each unit rectangle using eulers method actually gives us the area of the ellipse that is piab. This is an example case of how jacobian determinant can help us transform figures to more workable ones to find area easily
import numpy as np
= 6
b
= np.linspace(-10, 10, 200)
x = np.linspace(-10, 10, 200)
y = np.meshgrid(x, y)
X, Y
= X**2 + Y**2 / b**2 - 1
ellipse
= X
U = 6 * Y
V = 6
jacobian_det = U**2 + V**2 - 1
circle
= x[1] - x[0]
dx = y[1] - y[0]
dy = 0.0
ellipse_area
for i in range(len(x)-1):
for j in range(len(y)-1):
if ellipse[i, j] <= 0:
+= dx * dy
ellipse_area
print("Approximated area of the ellipse using Euler's method: {:.2f}".format(
ellipse_area))
= 0.0
circle_area
for i in range(len(x) - 1):
for j in range(len(y) - 1):
if circle[i, j] <= 0:
+= dx * dy*jacobian_det # to transform dx dy to du dv
circle_area
print("Approximated area of the circle in the transformed domain: {:.2f}".format(
circle_area))
Approximated area of the ellipse using Euler's method: 18.95
Approximated area of the circle in the transformed domain: 3.15
We see that the area of circle comes out to be nearly pi, when we will multiply this by jacobian determinat to the final area of ellipse, we will get pi16 = 6pi, which is the area of ellipse. Therefore we saw how integration will work when two integrals are present and we simplify using jacobian determinant. Int(int(complex function))=int(int(transformed function))*jacobian determinant