Least Square Method for Curve Fitting | Numerical Methods Lab
Numerical Methods - Least Square Method

Lab 4: Least Square Method for Curve Fitting

Lab 4: Least Square Method for Linear, Exponential and Polynomial Curve Fitting

Experiment Information

Experiment: Least Square Method for Linear, Exponential and Polynomial Curve Fitting

Course Code: Numerical Methods

Description: Complete lab report covering theory, algorithm, Python implementation and analysis of least square method for curve fitting

Linear & Exponential Curve Fitting Report

1. Theory of Least Square Method

1.1 Mathematical derivation for linear fitting

1.2 Transformation for exponential fitting

1.3 Error minimization principle

2. Algorithm

2.1 Step-by-step procedure for linear fitting

2.2 Steps for exponential transformation

2.3 Error calculation methods

3. Implementation

3.1 Python code for linear fitting

3.2 Python code for exponential fitting

3.3 Visualization with matplotlib

4. Observations

4.1 Data recording table

4.2 Sample calculations

4.3 Error analysis

5. Results

5.1 Fitted parameters

5.2 Graphical representation

5.3 Comparison of different models

6. Discussion

6.1 Advantages and limitations

6.2 Applications in engineering

6.3 Error sources and mitigation

Polynomial Curve Fitting Report

1. Theory of Polynomial Least Squares

1.1 Normal equations formulation

1.2 Matrix representation

1.3 Degree selection criteria

2. Algorithm

2.1 Setting up normal equations

2.2 Solving the system

2.3 Overfitting considerations

3. Implementation

3.1 Python code with numpy

3.2 Polynomial degree selection

3.3 Visualization techniques

4. Observations

4.1 Data recording table

4.2 Coefficient calculation

4.3 Error vs degree analysis

5. Results

5.1 Optimal polynomial degree

5.2 Fitted curve visualization

5.3 Residual analysis

6. Discussion

6.1 Overfitting vs underfitting

6.2 Applications in engineering

6.3 Limitations of polynomial fitting

Python Implementation of Linear Least Squares

linear_least_squares.py
from math import * import matplotlib.pyplot as plt xstr=input(“Enter Value of X pressing Space:”) ystr=input(“Enter Value of y pressing Space:”) x=[float(num) for num in xstr.split()] y=[float(num) for num in ystr.split()] m=len(x) n=len(y) if m!=n or n<2: raise Exception("Enter equal no. of x and y") sumx=sum(x) sumy=sum(y) sumX2=sum([num1**2 for num1 in x]) sumXY=sum([num1*num2 for num1,num2 in zip(x,y)]) d1=sumX2*sumy-sumXY*sumx d2=n*sumXY-sumy*sumx d=sumX2*n-sumx*sumx a=d1/d b=d2/d print("a,b=",a,b) xo=min(x) xn=max(x) yo=a+b*xo yn=a+b*xn plt.figure() plt.plot(x,y,color="red", linestyle="--", label="actual data") plt.plot([xo,xn],[yo,yn],linestyle="-",color="blue",label="fitted data") plt.xlabel('x') plt.ylabel('y') plt.legend() plt.show()

Python Implementation of Exponential Least Squares

exp_least_squares.py
from math import log, exp import matplotlib.pyplot as plt xstr = input(“Enter values of X separated by spaces: “) ystr = input(“Enter values of Y separated by spaces: “) x = [float(num) for num in xstr.split()] y = [float(num) for num in ystr.split()] if len(x) != len(y) or len(y) < 2: raise Exception("Enter an equal number of x and y values, with at least two points.") ly = [log(yi) for yi in y] n = len(x) sx = sum(x) sly= sum(ly) sx2 = sum([xi ** 2 for xi in x]) sxly= sum([xi * ly for xi, ly in zip(x, ly)]) d = n * sx2 - sx ** 2 A = (sly * sx2 - sx * sxly) / d b = (n * sxly - sx* sly) / d a = exp(A) print(f"Parameters: a = {round(a,3)}, b = {round(b,3)}") import numpy as np x_fit = np.linspace(min(x), max(x), 100) y_fit = [a * exp(b * xi) for xi in x_fit] plt.figure() plt.scatter(x, y, color="red", label="Actual Data") plt.plot(x_fit, y_fit, color="blue", label="Fitted Exponential Curve") plt.xlabel("x") plt.ylabel("y") plt.legend() plt.show()
×

Disclaimer

The educational materials provided on this website are intended as supplementary resources to support your learning journey. These lab reports are sample documents designed to help students understand proper formatting and content organization.

We have made every effort to ensure the accuracy of the content. However, we recommend students to perform their own experiments and prepare original reports. These samples should be used as references only.

We respect intellectual property rights. If you believe any content should be credited differently or removed, please don’t hesitate to contact us. We’re happy to make appropriate corrections or give proper attribution.

Leave a Comment

Scroll to Top