# Chapter 9 MATLAB-to-Python Conversions

This folder contains literal Python conversions of the uploaded Chapter 9 MATLAB programs. Variable names, numerical algorithms, comments, axis limits, tick locations, colors, line styles, line widths, marker sizes, viewpoints, and other plotting choices have been retained wherever Matplotlib provides a direct equivalent.

Every explicit plotting font size was reduced by 2 points. For example, MATLAB `FontSize`, 16 is represented by a Python font size of 14. Figure dimensions are derived from the original MATLAB pixel dimensions and scaled to fit a normal desktop plotting window while preserving the original aspect ratio.

## Converted files

| MATLAB source | Python file |
|---|---|
| `fig9_1(2).m` | `fig9_1.py` |
| `fig9_4(2).m` | `fig9_4.py` |
| `fig9_5(1).m` | `fig9_5.py` |
| `fig9_6(2).m` | `fig9_6.py` |
| `fig9_8(2).m` | `fig9_8.py` |
| `fig9_9(2).m` | `fig9_9.py` |
| `fig9_10(1).m` | `fig9_10.py` |
| `fig9_11(2).m` | `fig9_11.py` |
| `fig9_12(3).m` | `fig9_12.py` |
| `fig9_15(3).m` | `fig9_15.py` |
| `fig9_15T(3).m` | `fig9_15T.py` |
| `fig9_17(2).m` | `fig9_17.py` |
| `fig9_18(3).m` | `fig9_18.py` |
| `fig9_19(2).m` | `fig9_19.py` |
| `fig9_21(1).m` | `fig9_21.py` |
| `fig9_22(3).m` | `fig9_22.py` |
| `parseArgs.m` | `parseArgs.py` |
| `subaxis.m` | `subaxis.py` |

`plot_utils.py` supplies shared figure sizing, MATLAB color-order, font, and optional file-saving helpers.

## Installation

From Terminal, change to this folder and create an optional virtual environment:

```bash
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -r requirements.txt
```

## Running a program

Run a file from this folder. For example:

```bash
python3 fig9_15.py
```

Each figure file also defines a same-named function, so it can be imported:

```python
from fig9_15 import fig9_15

fig9_15()
```

The scripts that generate random matrices or data retain MATLAB's random behavior, so their numerical output and plots can change from one run to the next.

## Optional saved plots

By default, figures are displayed and are not written to disk. Set `FIG9_SAVE_DIR` to save PNG copies in a chosen folder:

```bash
FIG9_SAVE_DIR=plots python3 fig9_12.py
```

## Large-computation controls

The original programs contain several unusually large dense computations. Their published default sizes are retained, except that `fig9_8.py` and `fig9_10.py` use mathematically equivalent SciPy sparse storage so that the original problems fit comfortably in memory.

The following optional environment variables are useful for faster experimentation or testing:

| Variable | Original default | Used by |
|---|---:|---|
| `FIG9_1_N0` | 2000 | contour grid in `fig9_1.py` |
| `FIG9_1_SURFACE_N0` | 60 | surface grid in `fig9_1.py` |
| `FIG9_8_N` | 100 | Laplace grid dimension in `fig9_8.py` |
| `FIG9_9_MAX_ISIZE` | 5 | largest timing case in `fig9_9.py` |
| `FIG9_9_REPEATS` | 6 | timing repetitions in `fig9_9.py` |
| `FIG9_10_N` | 1000 | matrix dimension in `fig9_10.py` |
| `FIG9_11_N` | 100 | matrix dimension in `fig9_11.py` |
| `FIG9_12_N` | 100 for case 2 | matrix dimension in `fig9_12.py` |
| `FIG9_12_ICASE` | 2 | example selection in `fig9_12.py` |
| `FIG9_17_MAX_ITER` | 10000 safety limit | `fig9_17.py` |
| `FIG9_18_MAX_ITER` | 10000 safety limit | `fig9_18.py` |

For example, a quick version of the sparse-versus-dense timing program is:

```bash
FIG9_9_MAX_ISIZE=2 FIG9_9_REPEATS=1 python3 fig9_9.py
```

The full default `fig9_9.py` calculation intentionally constructs dense matrices as the original MATLAB program does and can require substantial memory and execution time.

## Validation

All Python files were checked with Python's byte-code compiler. All 16 figure scripts were also executed successfully with Matplotlib's noninteractive backend. Reduced test sizes were used only for the largest contour and matrix calculations; the original defaults remain in the delivered source files.
