Project is available on Streamlit at Numerical Differentiation Analysis · Streamlit
This interactive web app is built with Streamlit and is designed to analyze and visualize errors in numerical differentiation methods. The project focuses on approximating the derivative of the function at using two common methods:
- Forward Difference:
- Central Difference:
For each method, the app computes:
- Actual Error: The difference between the numerical derivative and the exact derivative ().
- Truncation Error: The error from approximating the derivative by neglecting higher-order terms in the Taylor series.
- Rounding Error: The error introduced by the finite precision of computer arithmetic.
It then visualizes these errors on log-log plots so that you can see how they change as the step size varies. Also, the app calculates the optimal values for minimizing the total error.
- **Interactive Controls:**Adjust the range for (expressed as ), the number of points, and the machine epsilon () via the sidebar.
- **Theoretical Background:**An expandable section explains the mathematics behind the forward and central difference methods, including the expected orders of truncation and rounding errors.
- **Visualization:**Log-log plots display the actual error, truncation error, and rounding error for each method, making the power-law relationships clear.
- **Optimal Error Calculation:**The app computes and displays the optimal step size that minimizes the total error for both differentiation methods.
- Comparison Table: A summary comparing the properties of the forward and central difference methods is provided.
- Python 3.13
- Pip (Python package installer)
The project requires the following Python libraries:
numpymatplotlibstreamlit
You can install the dependencies using pip:
pip install numpy matplotlib streamlit-
**Download the Repository:**Save the provided code
-
**Run the Application:**Open a terminal, navigate to the directory containing
streamlit_app, and run:streamlit run streamlit_app.py
-
Interact with the App:
- Use the sidebar to set the minimum and maximum exponent for (e.g., ), choose the number of points, and adjust the machine epsilon.
- View the log-log plots that display the actual error, truncation error, and rounding error for both the forward and central difference methods.
- Expand the "Theoretical Background" section to review the underlying mathematics.
- Check the optimal values and comparison table for a summary of the error analysis.
configure_page(): Sets up the page title, layout, and custom CSS for a clean, professional look. It also loads MathJax to render LaTeX equations.
show_theory(): Displays an expandable section with an explanation of the forward and central difference methods, including their truncation and rounding errors.
get_user_inputs(): Creates sidebar widgets (sliders and number inputs) for the user to define the range of values, the number of data points, and the machine epsilon ().
calculate_errors(): Loops over an array of values to compute:- The approximate derivative using both forward and central differences.
- The actual error (difference from the exact derivative ).
- Estimated truncation error (using Taylor series approximations).
- Estimated rounding error (based on the machine epsilon).
create_error_plot(): Uses Matplotlib to generate log-log plots of the errors. These plots help illustrate the relationships between the error components and the step size .
calculate_optimal_values(): Computes the optimal for minimizing total error:- For forward difference:
- For central difference:
main(): Orchestrates the overall app functionality by:- Configuring the page.
- Displaying the theoretical background.
- Accepting user inputs.
- Calculating errors and generating plots.
- Displaying optimal values and a comparison table summarizing the two methods.