Uploaded by prettyk8jewel

Laboratory-no.-7-Numerical-Methods1

advertisement
Laboratory Activity no. 7 Curve Fitting using Octave
Objectives:
1.
To find the equations that would fit to a series of data points using Octave.
2.
To solve some applications of polynomial regression using Octave.
Materials


Laptop/ Computer
Octave Software
Introduction:
In linear regression, we fit the data by establishing a linear equation of an
independent variable x. While in non-linear regression, it is necessary to establish a
non-linear relationship between independent variable and dependent variable.
Intuitively, the straight line that fits the data becomes a "curve".
As shown in the figure below, the points are from the change data of the
population in a certain area. If we use linear variance to fit the data, then there is a
"visually visible" error. For such a data distribution, using a curve to fit is much more
suitable.
Curve fitting is a useful tool for representing a data set in a linear or quadratic
fashion. MATLAB and Octave has two functions, polyfit and polyval, which can quickly
and easily fit a polynimial to a set of data points. A first order polynomial is the linear
equation that best fits the data. A polynomial can also be used to fit the data in a
quadratic sense. As a reminder, the general formula for a polynomial is:
𝒇(𝒙) = π’‚πŸŽ 𝑿𝑡 + π’‚πŸ 𝑿𝑡−𝟏 + π’‚πŸ 𝑿𝑡−𝟐 + β‹― + 𝒂𝑴 𝑿𝑴
The degree of a polynomial is equal to the largest value of the exponents of the
independent variable, x.
Curve fitting with Matlab and Octave
"Polyfit" is a MATLAB function that computes a least squares polynomial for a given set of
data. Polyfit actually generates the coefficients of the polynomial (which can be used to
simulate a curve to fit the data) according to the degree specified.
"Polyval" evaluates a polynomial for a given set of x values. So, polyval actually generates a
curve to fit the data based on the coefficients found using polyfit.
MATLAB's explanation of polyfit and polyval are:
POLYFIT- Polynomial curve fitting
POLYFIT(x,y,n) finds the coefficients of a polynomial p(x) of degree n that fits the data, p(x(i))
is approximately equal to y(i), in a least-squares sense.
POLYVAL - Polynomial evaluation
If V is a vector whose elements are the coefficients of a polynomial, then POLYVAL(V,s) is the
value of the polynomial evaluated at s. If s is a matrix or vector, the polynomial is evaluated at
all points in s.
Procedures:
Before you can create the code or program, you must install Octave on your device.
However, there are alternatives available if your laptop or personal computer does meet the
high specifications required for software like Matlab, which is good for program simulations. If
not, stick to Octave or Octave online.
This method assumes that the program was preinstalled on your device, and the
processes and illustrations may differ from the alternatives indicated above at some point.
1. Search and open the Octave software on your device.
2. Go to editor and create a new script.
3. Type the sample code for systems of linear equation in the opened script and saved
as curvefit.
Code:
T= [1 2 3 4];
R=[6 11 18 27];
plot(T,R,'*b');
grid; title('X and Y, n=7'); xlabel('X'); ylabel('Y');
hold %
p=polyfit(T,R,2);
a=0: 1: 10;
q=polyval(p,a);
plot(a,q)
fprintf('\n')
disp('Coefficients ao, a1 and a2 are:'); fprintf('\n'); disp(p);
format bank
4. After typing the script, go to command window and type curvefit and see the answer
and the graph.
EXERCISES:
1. Fit the following polynomial functions from the given data below. (Manual
computation):
a.
X
0
1
2
3
4
5
Y
2.1
7.7
13.6
27.2
40.9
61.1
b.
X
1
2
3
4
Y
6
11
18
27
c.
X
1
2
3
4
5
6
7
8
9
Y
1
1.5
2
3
4
5
8
10
13
X
0
0.5
1
1.5
2
2.5
Y
0
0.25
1
2.25
4
6.25
X
1
2
3
4
5.5
7
10
Y
3
7
9
15
22
21
21
d.
e.
After finding the answers, screenshot the algorithm and the outputs.
Reference:
https://gnindia.dronacharya.info/IT/Downloads/Labmanuals/Lab_Manual_Numerical_
Technique.pdf
https://www.mathworks.com/help/matlab/ref/polyfit.html
https://www.mathworks.com/help/matlab/ref/polyval.html?searchHighlight=polyval&s
_tid=srchtitle_support_results_1_polyval
Download