Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works

Random project

least-squares-method

This is an experiment in reviving legacy code. The original source code was photographed from a printed listing and can be found in the `data/` folder. I modified the code to compile and run on **Turbo Pascal 5.5**

View on GitHub

Anyone may press Interesting, signed in or not. It helps others find this project.

Member rating

No ratings yet.

Sign in to rate this project.

README

METODA NEJMENSICH CTVERCU (Least Squares Method)

!!!! Pull requests are welcome!

Dedicated to Frantisek — whom I admired as a child. He was a great programmer and always repaired our family computer when it was broken.

This is an experiment in reviving legacy code. The original source code was photographed from a printed listing and can be found in the data/ folder. I modified the code to compile and run on Turbo Pascal 5.5 (the original was written for TP 3.x).

The original source: a printed listing, photographed

The original source: a printed listing, photographed.

My plan is to compare the results with a modern Python implementation to verify correctness and explore the numerical differences.

Legacy Turbo Pascal (DOS) program for computing a least-squares polynomial approximation of measured data points. Originally written for Borland Turbo Pascal and intended to run in a DOS environment.

Note: The program interface is in Czech. An English version is planned for the future.


📌 Overview

This program implements the method of least squares to approximate a set of measured points
((x_i, y_i)) using a polynomial of low degree.

The user:

  1. Enters measured data points
  2. Chooses the degree of the polynomial
  3. The program computes polynomial coefficients
  4. Displays approximation errors
  5. Allows evaluation of the polynomial for arbitrary x

🧮 Mathematical Background

The program constructs a polynomial:

[ P(x) = a_0 + a_1 x + a_2 x^2 + \dots + a_n x^n ]

Coefficients (a_i) are computed by solving the normal equations:

[ (A^T A) a = A^T y ]

Where:

  • (A) is the Vandermonde matrix of input x values
  • (y) is the vector of measured y values

A custom Gaussian elimination routine is used to solve the linear system.


🗂 Program Structure

Constants

  • maxt = 2 — maximum polynomial degree
  • max = 100 — maximum number of data points

Data Types

  • pole — array of real values (input data)
  • vektor — vector of polynomial coefficients
  • matice — augmented matrix for linear equations

🔧 Main Components

eliminace

Performs Gaussian elimination and back-substitution to solve a linear system.

polynom

Evaluates the polynomial using Horner’s method.

mocnina

Computes integer powers of a real number.


🖥 User Interaction

  • Text-based interface using the CRT unit
  • Single-key input with ReadKey
  • Interactive correction of entered points
  • Live evaluation of polynomial values

⚠ Limitations

  • Requires DOSBox + Turbo Pascal 5.5 to run
  • Limited polynomial degree (maxt = 2)
  • Numerical robustness — see Numerical Stability Considerations below

▶ How to Run

Requirements

  • DOSBox - DOS emulator
    bash
    sudo apt install dosbox

First Time Setup

After cloning, extract Turbo Pascal from the included disk images:

bash
cd turbo_pascal
./setup_turbopascal.sh

Run the Program

bash
cd turbo_pascal
./run_mnc.sh

This opens DOSBox, compiles and runs the program.

More Info

See turbo_pascal/README.md for:

  • Turbo Pascal IDE usage
  • Code changes from original TP3 to TP5.5
  • Troubleshooting

🔢 Numerical Stability Considerations

The implementation of the least squares method in this program uses normal equations combined with a hand-written Gaussian elimination routine.

While this approach is simple and historically common, it has known numerical drawbacks:

  • Forming normal equations squares the condition number of the system
  • No pivoting is used during Gaussian elimination
  • Rows are not normalized during elimination
  • Intermediate values can grow rapidly

As a result, the algorithm may suffer from:

  • Loss of precision for closely spaced x values
  • Large rounding errors for higher polynomial degrees
  • Numerical instability for larger datasets

Practical Impact

In its current configuration, the program is reasonably safe only for:

  • Very low polynomial degrees (maxt = 2)
  • Small numbers of data points
  • Well-scaled input values

Future Improvements

If this code is ever modernized, recommended improvements include:

  • Using QR decomposition instead of normal equations
  • Adding partial or full pivoting to Gaussian elimination
  • Scaling or normalizing input data
  • Using numerically stable linear algebra libraries

🔄 Future Plan (Optional)

Phase 2 — Porting & Modernization

Potential future improvements:

  • Port to Free Pascal or modern Pascal
  • Remove CRT dependencies
  • Numerical fixes — see Future Improvements above
  • Increase supported polynomial degree
  • Add file-based input/output

📁 Files

pascal
????????? README.MD                        # This document
????????? data/                            # Photos of original printed code
????????? turbo_pascal/
    ????????? README.md                    # Technical details & setup
    ????????? MNC.PAS                      # Working TP5.5 version
    ????????? MNC_ORIGINAL.PAS             # Original TP3 source (historical)
    ????????? installed/                   # Turbo Pascal 5.5 installation (created by setup_turbopascal.sh)
    ????????? *.sh                         # Run scripts

🕰 Historical Note

This code reflects classic 1980s numerical programming practices:

  • Manual matrix handling
  • Custom solvers
  • Text-mode UI

Preserved for educational, historical, and experimental purposes.


✍ Author / Origin

  • Original program: DALSAN soft
  • Turbo Pascal program lister © 1983 Borland International
  • Documentation & execution plan added later