.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/extras/gradient.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_extras_gradient.py: Gradient of array ================== In this example, we calculate the gradient of a 2D array using finite difference methods. We will explore different orders of derivatives and visualize their effects on the input array. .. GENERATED FROM PYTHON SOURCE LINES 10-13 Importing required packages --------------------------- Here we import the necessary libraries for numerical computations, rendering, and finite difference operations. .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: Python import numpy as np from PyFinitDiff.finite_difference_2D import get_array_derivative, Boundaries import matplotlib.pyplot as plt from PyFinitDiff import BoundaryValue .. GENERATED FROM PYTHON SOURCE LINES 20-23 Creating the input mesh ------------------------ We define a 2D Gaussian mesh using two 1D exponential arrays. .. GENERATED FROM PYTHON SOURCE LINES 23-32 .. code-block:: Python idx = np.linspace(-5, 5, 100) x_array = np.exp(-idx**2) y_array = np.exp(-idx**2) y_array, x_array = np.meshgrid(x_array, y_array) mesh = x_array * y_array .. GENERATED FROM PYTHON SOURCE LINES 33-36 Setting boundary conditions --------------------------- Define boundary conditions for the gradient calculation. Here, we use 'none' for all boundaries. .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python boundaries = Boundaries(top=BoundaryValue.NONE, bottom=BoundaryValue.NONE, left=BoundaryValue.NONE, right=BoundaryValue.NONE) .. GENERATED FROM PYTHON SOURCE LINES 40-43 Visualizing the gradient for different derivatives --------------------------------------------------- We compute the gradient for first, second, and third derivatives and visualize them. .. GENERATED FROM PYTHON SOURCE LINES 43-64 .. code-block:: Python figure, axes = plt.subplots(1, 3, figsize=(12, 4), constrained_layout=True) axes = axes.flatten() for ax, derivative in zip(axes, [1, 2, 3]): gradient = get_array_derivative( array=mesh, accuracy=6, derivative=derivative, x_derivative=True, y_derivative=True, boundaries=boundaries ) image = ax.pcolormesh(gradient.real, shading='auto', cmap='viridis') ax.set_title(f'Derivative: {derivative}') ax.set_aspect('equal') plt.colorbar(image, ax=ax) plt.show() .. image-sg:: /gallery/extras/images/sphx_glr_gradient_001.png :alt: Derivative: 1, Derivative: 2, Derivative: 3 :srcset: /gallery/extras/images/sphx_glr_gradient_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.018 seconds) .. _sphx_glr_download_gallery_extras_gradient.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: gradient.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: gradient.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: gradient.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_