CSC212 Numerical Method

Numerical MethodUnit 912 min read

Curve Fitting & Regression: Models, Least Squares, and Applications

Unit 9 of Numerical Method covers regression analysis (linear, polynomial, exponential), least squares approximation, curve fitting techniques, and their distinctions from interpolation, with worked examples and algorithmic implementations.

TAKEAWAYS:

  • Regression vs. Interpolation: Regression fits a model to approximate data trends (minimizing error globally), while interpolation fits a curve exactly through given points (zero error at data points).
  • Least Squares Method: The foundation of regression—minimizes the sum of squared residuals to find the best-fit parameters (e.g., slope/intercept in linear regression).
  • Model Selection: Choose between linear, polynomial, exponential, or logarithmic regression based on data behavior (e.g., exponential decay for radioactive data, quadratic for projectile motion).
  • Applications: Regression predicts trends (e.g., stock prices, population growth), while curve fitting models physical laws (e.g., Newton’s cooling law).
  • Algorithmic Workflow: Transform data (e.g., log-transform for exponential fits), solve normal equations, or use iterative methods (e.g., gradient descent) for large datasets.
  • Error Metrics: Use (coefficient of determination) or RMSE (root mean squared error) to evaluate fit quality—higher (closer to 1) indicates better fit.

1. Definitions and Key Concepts

1.1 Curve Fitting

Curve fitting is the process of constructing a mathematical function (model) that best describes the relationship between variables in a dataset. Unlike interpolation, which passes exactly through given points, curve fitting aims to approximate the underlying trend with minimal error.

1.2 Regression Analysis

A statistical tool within curve fitting that estimates the relationships among variables. It predicts the dependent variable () based on one or more independent variables ().

1.3 Least Squares Approximation

The most common method for regression, where the best-fit curve minimizes the sum of the squares of the residuals (differences between observed and predicted values). Mathematically, for a model , we minimize:


2. Types of Regression Models

2.1 Linear Regression

Fits a straight-line model to data. Used when the relationship between and is approximately linear.

Example: Fit to the data:

x 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4
y 2.0 2.6 3.9 6.0 9.3 15 20.6 30.4

Solution:

  1. Transform data: The exponential trend suggests a logarithmic transformation. Let , so .

  2. Compute :

    x 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4
    0.693 0.955 1.361 1.792 2.230 2.708 3.025 3.411
  3. Apply linear regression to vs. :

    • Calculate means: , .
    • Slope ():
    • Intercept ():
  4. Final model:

2.2 Polynomial Regression

Fits a polynomial model to data. Useful for nonlinear trends (e.g., quadratic for projectile motion).

Example: Fit a quadratic curve to:

x 1 3 4 5 6
y 2 7 8 7 5

Solution:

  1. Set up normal equations for :
  2. Compute sums:
  3. Solve the system: Using matrix methods or substitution, we get: Final model:
  4. Prediction: At :

2.3 Exponential Regression

Fits models like or . Used for growth/decay processes (e.g., population, radioactive decay).

Example: Fit to:

x 0 1 3 5 7 9
y 1.0 0.891 0.708 0.563 0.447 0.355

Solution:

  1. Take natural logs: .
  2. Let and . Solve for and using linear regression on .
  3. Compute means: , .
  4. Slope ():
  5. Intercept ():
  6. Solve for : Final model:

3. Least Squares Method: Theory and Implementation

3.1 Derivation

For a linear model , the least squares solution is: where is the design matrix:

3.2 Steps for Polynomial Regression

  1. Construct the design matrix for the polynomial degree.
  2. Solve the normal equations .
  3. Evaluate the model at desired values.

3.3 Advantages/Disadvantages

Advantages Disadvantages
Simple to implement Sensitive to outliers
Works for linear/nonlinear models Overfitting with high-degree polynomials
Provides closed-form solutions Assumes errors are normally distributed

4. Regression vs. Interpolation: Key Differences

mindmap
  root((Curve Fitting))
    Regression
      Approximation
      Minimizes global error
      Used for prediction/trend analysis
      Example: Stock prices, population growth
    Interpolation
      Exact fit
      Zero error at data points
      Used for precise reconstruction
      Example: CAD/CAM, signal processing

Comparison Table:

Feature Regression Interpolation
Goal Approximate trend Exact fit through points
Error Minimizes squared residuals Zero error at data points
Extrapolation Possible (but unreliable) Not recommended
Model Complexity Can be simple (linear) or complex Often high-degree polynomials
Use Case Predictive analytics, physics Data reconstruction, smoothing

5. Algorithms and Pseudocode

5.1 Lagrange Interpolation (for comparison)

def lagrange_interpolation(x, y, x_query):
    n = len(x)
    result = 0.0
    for i in range(n):
        term = y[i]
        for j in range(n):
            if i != j:
                term *= (x_query - x[j]) / (x[i] - x[j])
        result += term
    return result

5.2 Least Squares Regression (Linear)

import numpy as np

def linear_regression(x, y):
    A = np.vstack([np.ones(len(x)), x]).T
    beta = np.linalg.lstsq(A, y, rcond=None)[0]
    return beta[0], beta[1]  # intercept, slope

6. Applications

  1. Physics: Fitting experimental data to theoretical models (e.g., Hooke’s law for springs).
  2. Economics: Predicting sales trends or cost functions.
  3. Biology: Modeling population growth or drug concentration over time.
  4. Engineering: Calibrating sensors or optimizing system parameters.

7. Error Metrics

  1. Sum of Squared Residuals (SSR):
  2. Coefficient of Determination ():
    • : Perfect fit.
    • : Model explains no variance.
  3. Root Mean Squared Error (RMSE):

8. Exam Tip

What Examiners Look For

  1. Correct Model Selection:

    • Identify whether data suggests linear, polynomial, or exponential trends (e.g., exponential decay for radioactive data).
    • Justify your choice (e.g., "The data shows a multiplicative trend, so exponential regression is appropriate").
  2. Mathematical Rigor:

    • Show all steps for deriving normal equations or transformations (e.g., log-transform for exponential fits).
    • For polynomial regression, explicitly write the system of equations and solve it (even if using matrix methods).
  3. Numerical Accuracy:

    • Compute means, sums, and coefficients precisely. Avoid rounding intermediate steps.
    • Example: For linear regression, calculate , , , and accurately.
  4. Comparison with Interpolation:

    • Clearly distinguish between the two in definitions, goals, and examples.
    • Example answer for "How does interpolation differ from regression?":

      Interpolation constructs a function that passes exactly through all given data points, ensuring zero error at those points but potentially large errors elsewhere. It is used for precise reconstruction (e.g., in computer graphics). In contrast, regression approximates the underlying trend by minimizing global error, making it robust for prediction and trend analysis but not exact at data points.

  5. Programming Questions:

    • For Lagrange interpolation, show the nested loop structure and explain the term-by-term construction.
    • For least squares, demonstrate matrix operations (e.g., ) or iterative methods like gradient descent.
  6. Common Pitfalls:

    • Overfitting: Avoid high-degree polynomials unless justified. Use or cross-validation to check.
    • Extrapolation: Never assume regression models hold outside the data range.
    • Nonlinear Models: Always transform data (e.g., log, reciprocal) before applying linear regression techniques.

Sample Exam Questions and Solutions

Q1: Fit the curve to the data:

x 1 2 3 4
y 1.65 2.70 4.50 7.35

Solution:

  1. Take logs: .
  2. Compute :
    x 1 2 3 4
    0.501 0.993 1.504 1.993
  3. Apply linear regression to :
    • , .
    • Slope ():
    • Intercept ():
    • Thus, .
  4. Final model:

Q2: How does polynomial interpolation differ from least squares approximation?

Solution:

Aspect Polynomial Interpolation Least Squares Approximation
Error at Data Points Zero error (exact fit) Non-zero error (approximate fit)
Degree of Polynomial Degree for points Can be lower than to avoid overfitting
Extrapolation Unreliable outside data range Also unreliable, but often used cautiously
Use Case Data reconstruction, CAD Trend analysis, prediction
Sensitivity Highly sensitive to outliers More robust to noise

9. Practice Problems

  1. Fit a second-order polynomial to the data:

    x 2 4 6 8 10
    y 1.4 2.0 2.4 2.6 2.8
  2. Fit to the data:

    x 0 1 2 3
    y 5 10 20 40
  3. Compare the values for linear and quadratic fits to the data in Problem 1. Which model is better?


10. References

  • Burden, R.L., & Faires, J.D. (2011). Numerical Analysis (9th ed.). Brooks/Cole.
  • Chapra, S.C., & Canale, R.P. (2010). Numerical Methods for Engineers (6th ed.). McGraw-Hill.
  • Wikipedia: "Curve fitting", "Polynomial interpolation"

Based on the TU BSc CSIT syllabus for Numerical Method (CSC212), unit 9.

Discussion

Loading…