Expt No 9 numerical

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Expt No: 09

Expt Name: Nonlinear polynomial curve fitting in MATLAB


Objectives:
 To understand the nonlinear polynomial curve fitting technique.
 To create a code to find the coefficients associated with the degree of the polynomials.
 To compare the ideal curve to the curve fitted with given data points.

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:

𝑦 = 𝑎2𝑥2 + 𝑎1𝑥 + 𝑎0 … … … … … (1)

When x=xi , the left hand side of equation (1) is represented by 𝑦̅𝜄 :

𝑦̅𝜄 = 𝑎𝑖𝑥𝑖2 + 𝑎𝑖𝑥𝑖 + 𝑎0 … … … … … (2)

The sum of the square of the deviations is given by:

𝑆 = ∑(𝑦𝑖 − 𝑦̅𝜄 ) 2 = ∑(𝑦𝑖 − 𝑎2𝑥𝑖2 − 𝑎1𝑥𝑖 − 𝑎0)2........(3)

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:

Taking input of data points and order of polynomial

1. Read the order of the polynomial: nnn


o Set cn=n+1(i.e., number of constants).
2. Take the x values of the points to be fitted: x.
3. Take the y values of the points to be fitted: y.
4. Read the number of data points: m.

Taking right-hand side of linear equations into b matrix

5. Do for i=1 to cn:


o Set sum = 0;
o Do for j=1 to mm:
 sum = sum + x(j)^(i−1)×y(j);
o End for j;
o Set b(i)=sumb(i);
o End for i.

Taking left-hand side of linear equations into c matrix

6. Do for i=1 to cn:


o Do for k=to cn:
 Set sum = 0;
 Do for j=1to m:
 sum = sum + x(j)^(i+k−2);
 End for j;
 Set c(i,k)=sum;
o End for k ;
o End for i;
o Compute a=inv(c)×b′.

Determination of constant coefficients and putting them into an matrix

7. Do for i=1 to cn:


o an(i)=a(cn−i+1);
o End for i;
o Display the an matrix.

Plotting best-fitting curve, curve from data points, and sine curve

8. (a) Generate points for best-fitting curve:


o x1=linspace(0,x(:,m),100), 100);
o y1=polyval(an,x1);
o Plot the data points and best-fitting curve:
 plot(x,y,′ro′,x1,y1)
o Use hold-on to retain the plot.
9. (b) Plot the original curve from the given data points:
o plot(x,y);
o Use hold-on to retain the plot.
10. (c) Plot a sine curve for xm from 0 to 2π:
o xm=0:0.001:2π;
o ym=sin(xm);
o plot(xm,ym).
MATLAB Code:

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:

Enter order of polynomial: 8


Enter values of x as a row matrix: [0 pi/4 pi/2 3*pi/4 pi 5*pi/4 3*pi/2
7*pi/4 2*pi]
Enter values of y as a row matrix: [0 0.7071 1 0.7071 0 -0.7071 -1 -
0.7071 0]
Enter the number of data points: 9
Polynomial Coefficients (an):
0.0000 0.0002 -0.0034 0.0236 -0.0389 -0.1107 -0.0419
1.0123 0.0000
Graph:

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

Rajshahi University of Engineering & Technology


(RUET), Rajshahi

Lab Report
Department of EEE
Exp. No:09
Exp. Name: Nonlinear polynomial curve fitting in MATLAB

Subject: Computational Methods Submitted To: Md. Mahmudul


in Engineering Sessional Hasan
Course No: EEE-3110 Submitted By: Farhan Shahriar
Date of Exp.: 2-9-2024 Roll No: 2001091
Date of Sub.: 9-9-2024 Session: 2020-2021

You might also like