1
6.23 Write a MATLAB user-defined function that de termines the coefficients of a cubic polynomial,
, that best fits a g iven set of d ata points. The function should al so calculate
the overall error E according to Eq. (6.21). Name the function [a,Er] = CubicPolyFit(x,y), where
the input arguments x and y are vectors with the coordinates of the data points, and the output argument a
is a four-element vector with the values of the coefficients , , , and . The output argument Er is
the value of the overall error.
(a)Use CubicPolyFit to determine the cubic polynomial that best fits the data in Example 6-3.
(b) Write a program in a script file that plots the data points and the curve of the cubic polynomial that best
fits the data.
Solution
The listing of the user-defined function CubicPolyFit is:
function [a,Er] = CubicPolyFit(x, y)
% CubicPolyFit calculates the coefficients a3,a2,a1 and a0 of the cubic