|
1 | | -# Maclaurin Series Demo for sin(x) (MATLAB) |
| 1 | +# Numerical Approximation of the Sine Function in MATLAB |
2 | 2 |
|
3 | | -A self-contained MATLAB demo illustrating **Maclaurin series approximations of sin(x)** using a fully numerical approach (no Symbolic Toolbox required). |
| 3 | +This repository studies how mathematically equivalent-looking approximations of `sin(x)` behave under finite-precision arithmetic. The project question is: **how accurately and efficiently can sine be approximated when algorithm design, range reduction, basis choice, and floating-point effects are all considered?** MATLAB's built-in `sin` is the practical reference implementation; this project does not claim to replace it. |
4 | 4 |
|
5 | | -The script visualizes approximation quality, error behavior, convergence via an animated GIF, a 3D error surface, and includes a classic **non-analytic counterexample** where the Maclaurin series fails. |
| 5 | +## Methods |
6 | 6 |
|
7 | | ---- |
| 7 | +| Method | Main idea | |
| 8 | +|---|---| |
| 9 | +| Direct Taylor | Explicit powers and factorials as a transparent baseline. | |
| 10 | +| Recurrence Taylor | Reuses consecutive terms to avoid repeated powers and factorials. | |
| 11 | +| Reduced Taylor | Reduces arguments to approximately `[-pi/4, pi/4]` and reconstructs by quadrant symmetry. | |
| 12 | +| Chebyshev | Computes interval-specific coefficients from Chebyshev nodes. | |
| 13 | +| Clenshaw | Evaluates Chebyshev expansions by backward recurrence. | |
8 | 14 |
|
9 | | -## Features |
| 15 | +The numerical routines validate finite real inputs and evaluate them in MATLAB double precision; compatible numeric inputs may be converted to double by MATLAB argument validation. The simple range reducer is intended for moderate arguments, not arbitrary huge arguments or correctly rounded argument reduction. |
10 | 16 |
|
11 | | -- Overlay plots of `sin(x)` and multiple Maclaurin polynomials |
12 | | -- Approximation vs absolute error for a selected polynomial degree |
13 | | -- Animated GIF showing convergence as the degree increases |
14 | | -- Comparison of true error with the “next-term” error proxy |
15 | | -- 3D surface plot: error vs `x` and number of included terms |
16 | | -- Non-analytic counterexample at `x = 0` where the Maclaurin series fails |
| 17 | +## Key mathematical ideas |
17 | 18 |
|
18 | | ---- |
| 19 | +The study connects Taylor remainder bounds, interval approximation, cancellation, range reduction, recurrence evaluation, Clenshaw evaluation, machine spacing, unit roundoff `u = eps/2`, and accuracy–runtime trade-offs. Safeguarded relative error near a zero of sine is reported separately from ordinary relative error because its denominator is `max(abs(reference),eps)`. |
19 | 20 |
|
20 | | -## File |
| 21 | +## Repository structure |
21 | 22 |
|
22 | | -- **`maclaurin_sin_demo.m`** |
| 23 | +- `src/+numapprox`: reusable numerical algorithms. |
| 24 | +- `tests`: `matlab.unittest` tests, including invalid-input and shape tests. |
| 25 | +- `experiments`: convergence, range-reduction, Chebyshev, and floating-point studies. |
| 26 | +- `benchmarks`: degree-sweep accuracy/runtime benchmark. |
| 27 | +- `report`: [technical study](report/numerical_approximation_study.md). |
| 28 | +- `results/raw`: generated machine-specific outputs. |
| 29 | +- `results/reference`: selected outputs suitable for review after real execution. |
| 30 | +- `results/figures`: generated plots. |
| 31 | +- `.github/workflows`: MATLAB test and reproduction workflows. |
23 | 32 |
|
24 | | ---- |
| 33 | +## Quick start and tests |
25 | 34 |
|
26 | | -## Requirements |
27 | | - |
28 | | -- MATLAB (standard installation) |
29 | | -- No Symbolic Math Toolbox required |
30 | | -- Uses built-in MATLAB functions for plotting and GIF generation |
31 | | - |
32 | | ---- |
33 | | - |
34 | | -## How to Run |
35 | | - |
36 | | -1. Open MATLAB and set the **Current Folder** to the project directory. |
37 | | -2. Run the script: |
| 35 | +From the repository root in MATLAB: |
38 | 36 |
|
39 | 37 | ```matlab |
40 | | -maclaurin_sin_demo |
| 38 | +addpath('src'); |
| 39 | +results = runtests('tests', 'IncludeSubfolders', true); |
| 40 | +assert(all([results.Passed]), 'One or more tests failed.'); |
41 | 41 | ``` |
42 | 42 |
|
43 | | -The script will generate several figures and optionally save a GIF file in the current folder. |
44 | | - |
45 | | ---- |
| 43 | +The tests cover zero, positive and negative inputs, row/column/matrix shapes, odd symmetry, degree handling, range-reduction boundaries, invalid intervals, non-finite inputs, Chebyshev coefficient sizes, known Chebyshev polynomials, Clenshaw, error metrics, and Taylor remainder bounds. |
46 | 44 |
|
47 | | -## Output |
| 45 | +## Reproducing experiments |
48 | 46 |
|
49 | | -- Multiple MATLAB figures: |
50 | | - - `sin(x)` vs Maclaurin approximations |
51 | | - - Approximation and absolute error (two-panel plot) |
52 | | - - Error vs next-term proxy |
53 | | - - 3D error surface |
54 | | - - Non-analytic counterexample plot |
55 | | -- Animated GIF: |
56 | | - - `maclaurin_sin.gif` (if enabled) |
| 47 | +`generateAllFigures` is a function, so add its directory and call it directly: |
57 | 48 |
|
58 | | ---- |
59 | | - |
60 | | -## Configuration |
61 | | - |
62 | | -You can easily customize the demo by editing the parameters at the top of the script: |
| 49 | +```matlab |
| 50 | +addpath('src'); |
| 51 | +addpath('experiments'); |
| 52 | +generateAllFigures; |
| 53 | +``` |
63 | 54 |
|
64 | | -- Domain and resolution of `x` |
65 | | -- Polynomial degrees used for approximation |
66 | | -- Degree used for error analysis |
67 | | -- GIF generation options (enable/disable, delay time) |
68 | | -- 3D error surface resolution |
| 55 | +This generates raw CSV files, selected reference CSV summaries, and PNG figures under `results/`. It also runs the benchmark degree sweep. The workflow can run the same entry point on demand and uploads the generated files as an artifact; it does not commit generated files automatically. |
69 | 56 |
|
70 | | ---- |
| 57 | +## Results status |
71 | 58 |
|
72 | | -## Notes |
| 59 | +**RESULTS PENDING MATLAB EXECUTION in this sandbox.** No numerical benchmark, CSV, or figure is claimed here unless generated by MATLAB. After local or GitHub Actions execution, selected genuine outputs can be copied from `results/raw` to `results/reference` and committed deliberately. |
73 | 60 |
|
74 | | -- The “next-term proxy” provides an intuitive estimate of the error near `x = 0`, but it is **not a strict bound** for all `x`. |
75 | | -- The non-analytic example demonstrates that even when all derivatives at a point exist and are zero, the Maclaurin series may still fail to represent the function. |
| 61 | +## Limitations |
76 | 62 |
|
77 | | ---- |
| 63 | +The range reducer uses nearest-integer arithmetic with MATLAB's `pi` and is documented only for moderate arguments. Chebyshev coefficients are interval-specific and are not intended for extrapolation. Runtime depends on MATLAB release, hardware, JIT warm-up, and vector size. The experiment scripts are evidence-generation tools, not production replacements for MATLAB's numerical library. |
78 | 64 |
|
79 | 65 | ## License |
80 | 66 |
|
81 | | -MIT License (recommended). |
82 | | -Feel free to use, modify, and share for educational purposes. |
| 67 | +MIT. See [LICENSE](LICENSE). |
0 commit comments