Derivatives
Derivatives
## Plotting
fig, ax = plt.subplots()
ax.grid()
ax.legend()
plt.show()
Medium ℎ: A balance between capturing the overall shape and smoothing out the noise.
Large ℎ: The approximation becomes less accurate and the function may smooth out too much, leading to loss of detail in the derivative.
*Visualizing the forward difference will show increasing smoothing as ℎ increases, but with small ℎ , you'll notice more sensitivity to the high-frequency component.
## Plotting
fig, ax = plt.subplots()
ax.grid()
ax.legend()
plt.show()
Small ℎ, both methods will capture the high-frequency oscillations more precisely, but these oscillations can be interpreted as noise, which may not be desirable in certain contexts (e.g., if you're only interested in the large-scale behavior).
Increasing ℎ helps smooth out these oscillations, but it comes at the cost of losing detail in the overall behavior of the function.
Central difference is more resistant to noise for a given ℎ and is thus preferable if you are trying to mitigate the impact of high-frequency oscillations.
In conclusion, if you consider the 0.1sin(30x) term as noise, using a larger ℎ or the central difference method would reduce its impact, providing a cleaner approximation of the primary behavior of the function (dominated by 5sin(x)).
X, Y = np.meshgrid(x_grid, y_grid)
phi = X**2 - 2*X + Y**4 - 2*Y**2 + Y
# Visualization
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
ax[0].matshow(dphi_dx[1:-1, 1:-1] / h, extent=[x_grid.min(), x_grid.max(), y_grid.min(), y_grid.max()], origin='lower')
ax[0].set_title('Partial Derivative ∂ϕ/∂x')
ax[1].matshow(dphi_dy[1:-1, 1:-1] / h, extent=[x_grid.min(), x_grid.max(), y_grid.min(), y_grid.max()], origin='lower')
ax[1].set_title('Partial Derivative ∂ϕ/∂y')
plt.colorbar(ax[0].matshow(dphi_dx[1:-1, 1:-1] / h), ax=ax[0])
plt.colorbar(ax[1].matshow(dphi_dy[1:-1, 1:-1] / h), ax=ax[1])
plt.show()
X, Y = np.meshgrid(x_grid, y_grid)
phi = X**2 - 2*X + Y**4 - 2*Y**2 + Y
plt.tight_layout()
plt.show()
*GRADIENT: QUESTION 1
Here we computed the first derivatives using forward differences, revealing the spatial behavior of the scalar field.
∂ϕ/∂x: This derivative measures the rate of change of the scalar field 𝜙 with respect to x. Positive values indicate that 𝜙 is increasing as 𝑥 increases, while negative values indicate a decrease.
∂ϕ/∂y: Similarly, this derivative measures the rate of change with respect to y. The plots show how the field varies in the y direction.
The output helps visualize how the scalar field changes in space, identifying regions where the field is increasing or decreasing.
X, Y = np.meshgrid(x_grid, y_grid)
phi = X**2 - 2*X + Y**4 - 2*Y**2 + Y
# Laplacian
laplacian_phi = laplacian_x + laplacian_y
# Visualization
plt.figure(figsize=(6, 5))
plt.matshow(laplacian_phi[1:-1, 1:-1] / (h**2), extent=[x_grid.min(), x_grid.max(), y_grid.min(), y_grid.max()], origin='lower')
plt.title('Laplacian ∇²ϕ(x, y)')
plt.colorbar()
plt.show()
# Laplacian
laplacian_phi = laplacian_x + laplacian_y
# Visualization of Laplacian
plt.figure(figsize=(6, 5))
contour_laplacian = plt.contour(X[1:-1, 1:-1], Y[1:-1, 1:-1], laplacian_phi[1:-1, 1:-1] / (h**2), levels=20, cmap='plasma')
plt.title('Contour of Laplacian ∇²ϕ(x, y)')
plt.xlabel('x')
plt.ylabel('y')
plt.colorbar(contour_laplacian)
plt.show()
GRADIENT: QUESTION 2
Here we used central differences to calculate the Laplacian, which provides insights into the curvature and overall distribution of the field. The Laplacian ∇^(2)𝜙 gives insight into the curvature of the scalar field. Positive values indicate areas of local minimum (concave regions), while negative values indicate local maximum (convex regions). Regions where the
Laplacian is zero imply that the function is locally flat. The output helps in understanding how the scalar field behaves spatially, indicating the points of acceleration or deceleration of the field.
Original Scalar Field: The first plot shows the original scalar field defined as 𝜙(𝑥,𝑦) = x^2 − 2x + y4 − 2y^2+y. This field is a combination of polynomial functions of x and y. The contours represent levels of constant 𝜙 across the xy-plane. The regions of interest include areas of high values (indicating peaks) and low values (indicating valleys or depressions).
Laplacian, on the other hand, the second plot visualizes the Laplacian of the field. The Laplacian indicates how 𝜙 curves in space; positive values suggest that the field is locally convex (indicating a local minimum), while negative values suggest local concavity (indicating a local maximum). A zero value suggests flatness in that region, indicating stability in the
potential field described by 𝜙.
Physical Implications: The original field is relevant in contexts like potential energy surfaces, where understanding the local minima and maxima can inform stability and equilibrium conditions. The Laplacian is crucial in physical phenomena like heat distribution and fluid dynamics, where it can indicate how heat or other quantities diffuse across a medium.