# Chapter 7 MATLAB-to-Python conversions

This folder contains literal Python conversions of all supplied Chapter 7 MATLAB files.

## Converted files

- `fig7_1.py`
- `fig7_3.py`
- `fig7_4.py`
- `fig7_5.py`
- `fig7_6.py`
- `fig7_7.py`
- `fig7_8.py`
- `fig7_9.py`
- `fig7_10.py`
- `fig7_11.py`
- `fig7_13.py`
- `fig7_14.py`
- `fig7_15.py`
- `fig7_16.py`
- `splineA.py`

The uploaded `fig7_16(1).m` file defines a MATLAB function named `errors`. The Python file retains that function name and calls it when the file is run directly.

## Support file

- `plot_helpers.py` contains the shared MATLAB color order, bold tick formatting, and arrowhead drawing used by several figures.

## Conversion conventions

- Variable names, loop structures, formulas, comments, and printed output were retained whenever Python syntax allowed.
- MATLAB names that conflict with Python syntax were changed minimally. Examples include `np_` for a variable named `np` and Python-safe loop indices where necessary.
- Plot colors, line styles, line widths, markers, marker sizes, axes, ticks, legends, labels, and annotations follow the MATLAB programs.
- Every explicitly specified font size was reduced by 2 points. For example, size 16 becomes 14 and size 20 becomes 18.
- Figure sizes and subplot spacing were adjusted so the complete plots fit in normal Matplotlib windows.
- The MATLAB `subaxis` layouts were reproduced with Matplotlib subplots and margin controls.
- The supplied `splineA.m` algorithm was converted directly rather than replaced by SciPy's spline routine.
- The original commented alternatives and examples were retained as Python comments.
- The original `rk4R` storage line in `fig7_9.m` stores the old value `y` rather than `yy`. The Python conversion retains that line literally and marks it with a comment.

## Installation

Run this command from a terminal in the folder:

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

## Running a program

Examples:

```bash
python3 fig7_3.py
python3 fig7_10.py
python3 fig7_15.py
```

Each file may also be imported and its function called from another program.

## Programs with large default calculations

The original defaults are retained. Some examples can take a substantial amount of time:

- `fig7_5.py` includes an Euler calculation with 10,000,000 steps.
- `fig7_7.py` and `fig7_8.py` include implicit methods with up to 100,000 time steps.
- `fig7_9.py` performs several methods for two problems.
- `fig7_11.py` and `fig7_13.py` integrate to time 4000.

For quicker checks, these functions accept smaller optional arrays:

```python
from fig7_5 import fig7_5
fig7_5([10, 100, 1000])

from fig7_7 import fig7_7
fig7_7([20, 100, 1000])

from fig7_8 import fig7_8
fig7_8([20, 100, 1000])

from fig7_9 import fig7_9
fig7_9([10, 100])
```

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