# Chapter 6 MATLAB-to-Python conversions

This folder contains literal Python conversions of the supplied Chapter 6 MATLAB programs.

## Converted programs

- `fig6_1.py`
- `fig6_3.py`
- `fig6_6.py`
- `fig6_7.py`
- `fig6_8.py`
- `fig6_9.py`
- `fig6_10.py`
- `fig6_12.py`
- `fig6_13.py`
- `fig6_14.py`
- `fig6_16.py`
- `fig6_17.py`
- `table6_3.py`
- `table6_4.py`
- `simpA.py`

## Support files

- `splineA.py` supplies the natural cubic-spline behavior used by `fig6_1.py` and `fig6_10.py`.
- `plot_helpers.py` contains the shared MATLAB color order, bold tick formatting, and an arrowhead helper.

## Conversion conventions

- MATLAB variable names, loop structure, formulas, printed tables, and comments were retained whenever Python syntax allowed.
- MATLAB identifiers that conflict with Python keywords were changed minimally. For example, the MATLAB loop variable `in` is written as `in_`, `is` is written as `is_`, and `lambda` is written as `lambda_`.
- Plot colors, line styles, line widths, marker styles, marker sizes, axes, ticks, labels, legends, and annotations follow the MATLAB files.
- Every explicitly stated font size was reduced by 2 points. For example, MATLAB `FontSize`, 16 becomes Python size 14, and size 25 becomes size 23.
- Figure dimensions were converted to practical Matplotlib sizes so the complete plots fit inside ordinary plotting windows.
- The custom MATLAB `subaxis` calls were reproduced with Matplotlib figure margins and subplot spacing.
- MATLAB's `spline` command is represented by SciPy's default not-a-knot `CubicSpline`.
- MATLAB's older `quad` comparison in `fig6_16.py` is represented by `scipy.integrate.quad`.
- The original commented alternative examples remain in the Python files.

## Installation

From a terminal in this folder, run:

```bash
python3 -m pip install -r requirements.txt
```

## Running a program

Examples:

```bash
python3 fig6_8.py
python3 table6_3.py
python3 simpA.py
```

Each Python file can also be imported, and its same-named function can be called from another program.

## Performance note

`fig6_17.py` preserves the original default iteration counts. Its midpoint calculation grows to more than one hundred million subintervals and can therefore require substantial execution time and memory. For a quick test, call:

```python
from fig6_17 import fig6_17
fig6_17(nk_midpoint=5, nk_simpson=5)
```

The numerical results can differ slightly from MATLAB because Python, NumPy, SciPy, and MATLAB may use different floating-point and library implementations.
