Chapter6_Prob22
Chapter6_Prob22
6.22 Write a MATLAB user-defined function that determines the coefficients of a quadratic polynomial,
f ( x ) = a 2 x 2 + a 1 x + a0 , that best fits a given set of data points. Name the function a = QuadFit(x,y),
where the input arguments x and y are vectors with the coordinates of the data points, and the output argu-
ment a is a three-element vector with the values of the coefficients a 2 , a 1 and a 0 .
(a) Use the function to find the quadratic polynomial that best fits the data in Example 6-2.
(b) Write a program in a script file that plots the data points and the curve of the quadratic polynomial that
best fits the data.
Solution
The listing of the user-defined function QuadFit is:
function a = QuadFit(x, y)
% QuadFit calculates the coefficients a2,a1 and a0 of the quadratic
% polynomial that best fits n data points.
% Input variables:
% x A vector with the coordinates x of the data points.
% y A vector with the coordinates y of the data points.
% Output variables:
% a A vector with the values of a2,a1 and a0
% The vector [a] is determined by solving the system [X][a]=[Y]
nx = length(x);
ny = length(y);
m = 3; %number of coefficients
if nx ~= ny
disp('ERROR: The number of elements in x must be the same as in y.')
a = 'Error';
else
for i=1:(m+1)
xsum(i)=sum(x.^(i));
end
% First row of matrix [X] and first element of column vector [Y]
X(1,1)=nx;
Y(1,1)=sum(y);
for j=2:m
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
2
X(1,j)=xsum(j-1);
end
% Rows 2 and 3 of matrix [X] and column vector [Y]
for i=2:m
for j=1:m
X(i,j)=xsum(j+i-2);
end
Y(i,1)=sum(x.^(i-1).*y);
end
a=X\Y;
end
(a) The user-defined function QuadFit is used in the Command Window to find the quadratic polynomial
that best fits the data from Example 6-2:
The quadratic polynomial that best fits the data from Example 6-2 is:
f ( x ) = 0.0141x 2 – 0.7480x + 10.7215
(b) The following program in a script file plots the data points and the curve of the quadratic polynomial
that best fits the data.
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
3
tt=linspace(t(1),t(n),50);
VV=a(3).*tt.^2+a(2).*tt+a(1);
plot(t,V,'o',tt,VV,':');
xlabel('t (s)'); ylabel('v_R (V)');
legend('Data','Polynomial fit');
When the program is executed, the following plot is displayed in the Figure Window:
10
Data
9 Polynomial fit
6
v (V)
5
R
0
0 5 10 15 20 25 30
t (s)
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.