# Chapter 1 MATLAB-to-Python Conversions

This folder contains literal Python conversions of the uploaded MATLAB files:

- `fig1_1.py` through `fig1_10.py`
- `table1_1.py`
- `table1_4.py`

## Conversion conventions

- Original variable names and comments were retained wherever Python syntax permits.
- Plot colors, line styles, line widths, limits, ticks, labels, legends, and original pixel dimensions were retained where Matplotlib provides an equivalent.
- Every explicit MATLAB font size was reduced by 2 points. For example, `FontSize = 16` became `fontsize = 14`, and `FontSize = 18` became `fontsize = 16`.
- Matplotlib's constrained layout is used in place of MATLAB's `subaxis.m` and `parseArgs.m`, so labels, legends, and tick marks fit inside the plot window.
- MATLAB's external `arrowhead` call in `fig1_4.m` and `fig1_5.m` was replaced with Matplotlib's `FancyArrowPatch`.
- MATLAB's one-based indexing was retained explicitly where it materially affects the algorithm, particularly in `fig1_7.py` and `fig1_8.py`.

## Installation

Open Terminal in this folder and run:

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

## Running the files

Run any converted file with Python 3. For example:

```bash
python3 fig1_1.py
python3 table1_1.py
```

The figure scripts open a Matplotlib plot window. The table scripts print their results in Terminal.

## Runtime note

`fig1_6.py` and `table1_4.py` preserve the original nested summation loops and original values of `nn` and `n`. Consequently, they require extremely large numbers of Python loop iterations and may take a very long time to finish. This is intentional because the requested conversion is literal. Reducing `nn`, shortening the exponent range, or replacing the loops with optimized compiled routines would make them faster but would no longer reproduce the original summation procedure as directly.

## Files

- `fig1_1.py`: comparison of the expanded polynomial and `(x - 1)^8`
- `fig1_2.py`: cancellation in `(sqrt(16 + k) - 4)/k`
- `fig1_3.py`: relative sine-evaluation error for large arguments
- `fig1_4.py`: number-line spacing diagram
- `fig1_5.py`: floating-point interval diagram
- `fig1_6.py`: harmonic-series summation-order errors
- `fig1_7.py`: Horner evaluation comparison
- `fig1_8.py`: detailed floating-point cancellation segments
- `fig1_9.py`: cancellation in `(sqrt(1 + x^2) - 1)/x^2`
- `fig1_10.py`: cancellation in `(exp(x) - 1)/x`
- `table1_1.py`: difference between two harmonic summation orders
- `table1_4.py`: compensated harmonic summation table
