Rajshahi University of Engineering & Technology
Department of Electrical & Electronic Engineering
Course No: EEE 3110
Course Title: Computational Methods in Engineering Sessional
Experiment No: 09
Experiment Name: Non-linear polynomial curve fitting method using MATLAB.
SUBMITTED BY SUBMITTED TO
Name: Al-Momin Hosen Sushanto Bosak
Roll: 2101102 Lecturer
Section: B Electrical & Electronic Engineering
Dept.: Electrical & Electronic Engineering Rajshahi University of Engineering &
Session: 2021-2022 Technology (RUET)
Date of Submission: 16/06/2025
Exp No: 09
Exp name: Non-linear polynomial curve fitting method using MATLAB
Objectives:
1. To implement and analyze a non-linear polynomial curve fitting technique using
MATLAB for a given set of data points.
2. To determine the best-fit curve that minimizes the error between the fitted polynomial
and actual data values.
Theory:
Curve fitting is a fundamental technique in data analysis that involves constructing a curve or
mathematical function that best approximates a set of data points. This is essential when
experimental or observed data does not follow a simple, easily understood pattern.
When the relationship between the input (independent variable) and output (dependent variable)
data is non-linear, a higher-degree polynomial function can often describe the trend more
accurately. This process is known as non-linear polynomial curve fitting.
1.Polynomial Curve Fitting :
A polynomial of degree 'n' is an expression of the form:
y = a₀ + a₁x + a₂x² + a₃x³ + ... + aₙxⁿ
Here, y is the predicted value, x is the input variable, and a₀, a₁, ..., a ₙ are coefficients to be
determined. The value of 'n' determines the complexity of the curve. For example, a quadratic
curve has n=2, a cubic curve has n=3, etc.
2. Least Squares Curve Fitting :
To find the best-fit polynomial, the most common method used is the 'Least Squares Method'.
This method minimizes the sum of the squares of the vertical deviations (residuals) between the
observed data points (yᵢ) and the values predicted by the polynomial function (ŷᵢ).
Mathematically:
Error = Σ(yᵢ - ŷᵢ)²
Where ŷᵢ = a₀ + a₁xᵢ + a₂xᵢ² + ... + aₙxᵢⁿ
The optimal polynomial coefficients a₀ to a ₙ are determined by solving this minimization
problem.
Algorithm:
Input: Data points (x, y), order of the polynomial (n), and number of data points (m)
Output: Coefficients of the best-fitting polynomial and visualization of the fitting curve
1. Read the order of the polynomial: n
Set number of coefficients cn ← n + 1
2. Read the x values of the data points: x
Read the y values of the data points: y
3. Read the number of data points: m
Constructing the Right-Hand Side of the Linear System (b matrix)
4. FOR i from 1 to cn DO: Initialize sum ← 0
FOR j from 1 to m DO:
sum ← sum + (x[j]^(i - 1)) * y[j]
END FOR
b[i] ←
sum END FOR
Constructing the Left-Hand Side of the Linear System (c matrix)
5. FOR i from 1 to cn DO:
FOR k from 1 to
cn DO:
Initialize sum ← 0
FOR j from 1 to m
DO:
sum ← sum + x[j]^((i + k) - 2)
END FOR
c[i, k] ← sum
END FOR
END FOR
6. Compute the coefficient vector: a ← inverse(c) * transpose(b)
Determining Polynomial Coefficients
7. FOR i from 1 to cn DO:
an[i] ← a[cn - i + 1]
END FOR
8. Display the polynomial coefficients: an
Plotting the Fitting Curve, Data Points, and Sine Curve
9. (a) Generate x1 ← linspace(0, max(x), 100)
Compute y1 ← evaluate polynomial an at x1
Plot the data points (x, y) as red circles and the fitting curve (x1, y1)
(b) Plot the original data points (x, y)
(c) Generate xm ← evenly spaced points from 0 to 2π
Compute ym ← sin(xm)
Plot the sine curve (xm, ym)
10. END
MATLAB Code & Output:
clc clear all close all
n = input('inter The degree of the polynomial:');
cn = n + 1;
x=[0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 ];
y=[0 0.04799 0.814 0.9974 0.99092 0.5984 0.1411 -0.3507 -0.7568 -
0.9775 -0.95 0.7055 -0.2794];
m = length(x);
b = zeros(cn, 1);
for i = 1:cn
sum_val = 0;
for j = 1:m
sum_val = sum_val + (x(j)^(i - 1)) * y(j);
end
b(i) = sum_val;
end c = zeros(cn,1);
for i = 1:cn
for k = 1:cn
sum_val = 0;
for j = 1:m
sum_val = sum_val + x(j)^((i + k) - 2);
end
c(i, k) = sum_val;
end
end
a = inv(c) * b;
an = zeros(1,cn);
for i = 1:cn
an(i) = a(cn - i + 1);
end
disp('Polynomial coefficients:'); disp(an);
x1 = linspace(0, max(x), 100); y1 = polyval(an, x1);
subplot(2,1,1);
plot(x, y, 'ro', x1, y1, 'b-'); title('Polynomial Fit'); xlabel('x');
ylabel('y');
legend('Data points', 'Fitted curve');
xm = linspace(0, 2*pi, 100); ym = sin(xm);
subplot(2,1,2); plot(xm, ym, 'g-'); title('Sine Curve'); xlabel('x');
ylabel('sin(x)'); legend('Sine function');
Output:
inter The degree of the polynomial:5
Polynomial coefficients:
-0.0108 0.1694 -0.8512 1.3075 0.1189 -0.0584
Fig. 9.1: Output curve for the MATLAB code
Discussion & Conclusion:
In this experiment, polynomial curve fitting was performed manually using MATLAB by solving
the normal equations. A user-defined polynomial degree was taken, and the best-fit coefficients
were calculated for a given set of data points resembling a sine wave.
The fitted curve was found to closely match the original data when a suitable degree was
selected. However, it was observed that higher-degree polynomials led to overfitting and
instability at the boundaries. For validation, the fitted curve was compared with the actual sine
function, and it was seen that good approximation was achieved over a short interval, but
deviations occurred across a wider range.
Through this approach, a clearer understanding of the least squares fitting method was gained
without relying on built-in functions like polyfit().