# Chapter 8 MATLAB-to-Python conversions

This folder contains literal Python conversions of the supplied MATLAB programs:

- `fig8_3.py`
- `fig8_4.py`
- `fig8_5.py`
- `fig8_10.py`
- `fig8_11.py`
- `fig8_13.py`
- `fig8_14.py`
- `fig8_16.py`
- `fig8_17.py`
- `fig8_18.py`
- `fig8_19.py`
- `fig8_20.py`
- `table8_3.py`

The supplied data files are also included under the filenames expected by the Python scripts:

- `data8_14C.txt`
- `surf_data.txt`
- `tempss.txt`
- `testing_data.txt`
- `training_data.txt`

## Conversion choices

- Variable names and the numerical structure of the MATLAB programs were retained wherever Python permits.
- Original comments were retained and adjusted only where Python syntax or a Python-library replacement needed to be identified.
- MATLAB plot colors, line widths, line styles, marker types, marker sizes, axis limits, ticks, labels, legends, grids, and aspect-ratio choices were retained.
- Every explicit MATLAB font size was reduced by 2. For example, 20 became 18, 16 became 14, and 14 became 12.
- MATLAB pixel-based figure positions were translated to aspect-ratio-preserving Matplotlib figure sizes. Matplotlib's constrained layout is used so labels and legends fit in the plot window.
- The MATLAB `subaxis` calls were replaced by Matplotlib subplot layouts with constrained spacing.
- MATLAB `fminsearch` was replaced by `scipy.optimize.minimize(..., method="Nelder-Mead")`.
- MATLAB `ode45` was replaced by `scipy.integrate.solve_ivp`.
- The external MATLAB spline routines `splineA`, `fspline`, and `nspline` were replaced by the corresponding SciPy cubic-spline operations.
- The large nested surface loops in `fig8_3.py` were vectorized without changing the mathematical quantities being evaluated.

## One necessary source correction

The original `fig8_5.m` uses `condc16` before assigning it. The Python version assigns the intended value,

```python
condc16 = np.linalg.cond(A.T @ A, np.inf)
```

so that the program runs and the table can be printed.

## Installation

Open Terminal in this folder and run:

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

## Running a program

For example:

```bash
python3 fig8_11.py
python3 fig8_20.py
```

The scripts locate their data files relative to their own folder, so they can be launched from another working directory.

## Large calculations

`fig8_3.py` retains the original default surface-grid sizes of 1100 and 2000. These settings require considerably more memory and rendering time than the other figure programs.

`table8_3.py` retains the original matrix sizes of 100, 500, 1000, 2000, and 4000. Dense QR factorizations at the largest sizes can require substantial memory and time. For a smaller check, use:

```bash
python3 table8_3.py --sizes 100 500
```

The original conditioning choice is `icase=2`. The well-conditioned case can be selected with:

```bash
python3 table8_3.py --icase 1
```

## Validation performed

- All Python files were compiled with `python -m py_compile`.
- All ordinary-size scripts were executed with a noninteractive Matplotlib backend.
- `fig8_3.py` and `table8_3.py` were execution-tested at reduced sizes; their source defaults remain the original full sizes.
