0% found this document useful (0 votes)
8 views

Chapter6_Prob22

The document describes a MATLAB user-defined function called QuadFit that calculates the coefficients of a quadratic polynomial fitting a given set of data points. It provides a sample implementation of the function and demonstrates its use with specific data points, resulting in a polynomial equation. Additionally, it includes a script to plot the data points alongside the fitted polynomial curve.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Chapter6_Prob22

The document describes a MATLAB user-defined function called QuadFit that calculates the coefficients of a quadratic polynomial fitting a given set of data points. It provides a sample implementation of the function and demonstrates its use with specific data points, resulting in a polynomial equation. Additionally, it includes a script to plot the data points alongside the fitted polynomial curve.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1

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:

>> t=[2 4 6 8 10 12 14 16 18 20 22 24 26 28 30];


>> V=[9.7 8.1 6.6 5.1 4.4 3.7 2.8 2.4 2.0 1.6 1.4 1.1 0.85 0.69 0.6];
>> a = QuadFit(t, V)
a =
10.7215
-0.7480
0.0141

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.

clear all; close all; clc;


t=[2 4 6 8 10 12 14 16 18 20 22 24 26 28 30];
V=[9.7 8.1 6.6 5.1 4.4 3.7 2.8 2.4 2.0 1.6 1.4 1.1 0.85 0.69 0.6];
n=length(t);
a = QuadFit(t, V);

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy