Expt No 9 numerical
Expt No 9 numerical
Expt No 9 numerical
Theory:
Curve Fitting is a process used in data analysis to find the best-fit curve that describes the relationship
between variables in a dataset. The goal is to produce a curve that captures the underlying trend or pattern of
the data as closely as possible, even if the relationship is not linear. In non-linear polynomial curve fitting,
we aim to fit a curve described by a polynomial equation to data points where the relationship between the
variables is not linear. A polynomial equation of degree 2 is of the form:
When x=xi , the left hand side of equation (1) is represented by 𝑦̅𝜄 :
Differentiating S with respect to a0, a1, a2 respectively, and setting each of this coefficient equal to zero
we get,
𝑛𝑎0 + 𝑎1 ∑ 𝑥𝑖 + 𝑎2 ∑ 𝑥2�= ∑ 𝑦𝑖 … … … … (4)
𝑎 0 ∑ 𝑥 𝑖 + 𝑎 1 ∑ 𝑥 2 + 𝑎 2 ∑ 𝑥 3 = ∑ 𝑥 𝑖 𝑦 𝑖 … … … … ( 5)
�
𝑖 𝑖
𝑎0 ∑ 𝑥2 + 𝑎1 ∑ 𝑥3 + 𝑎2 ∑ 𝑥4 = ∑ 𝑥2𝑦𝑖 … … … … (6)
𝑖 𝑖 𝑖 𝑖
These are the linear equations of three unknowns. The constants a0, a1, a2 can be obtained by solving
these equations. This basic idea can be extended to fit any nth-order polynomial. In the case of an nth-
order polynomial there will be (n+1) simultaneous equations containing (n+1) unknown constants.
Algorithm:
Plotting best-fitting curve, curve from data points, and sine curve
clc
n= input('Enter order of polynomial:') cn=n+1;
x= input('Enter values of x as a row matrix:')
y= input('Enter values of y as a row matrix:')
m= input('Enter no. of data:')
for i=1:cn
sum=0;
for j=1:m
sum=sum+ (x(j).^(i-1))*y(j);
end
b(i)=sum;
end
b
for i=1:cn
for k=1:cn
sum=0;
for j=1:m
sum=sum+x(j).^((i+k)-2);
end
c(i,k)=sum;
end
end
a=inv(c)*b'
for i=1:cn
an(i)=a(cn-i+1);
end
an
x1= linspace(0, x(:,m),100);
y1= polyval(an,x1);
plot(x,y,'ro',x1,y1);
hold on
plot(x,y);
hold on
xm= 0:0.001:2*pi;
ym=sin(xm);
plot(xm,ym)
Output:
Discussion:
In this experiment, the polynomial curve fitting of a given set of data points was carried out using
MATLAB. The entire procedure was implemented through matrix manipulations. The experiment
involved selecting specific points from a sine wave and fitting them to a polynomial of a chosen order.
The program successfully fitted the selected data points using the polynomial, allowing for comparison
between the sine and fitted polynomial curves.
The flexibility of the program was highlighted by its ability to fit any order of polynomials, depending on
user input provided through the command window. This generalized approach ensures that it can be
adapted for different data sets and polynomial orders without modifying the underlying code.
All commands were executed as expected, and the resulting output, including the graphical representation
of the fitted curve and the original sine wave, was accurate and consistent. The program's performance
demonstrated precision in solving the polynomial fitting problem, ensuring that the experiment was
conducted effectively.
Thus, it can be concluded that the experiment was performed successfully, with the results validating the
correctness of the implemented algorithm.
Heaven’s Light is Our Guide
Lab Report
Department of EEE
Exp. No:09
Exp. Name: Nonlinear polynomial curve fitting in MATLAB