% Curve fitting is a useful tool for representing a data set in a linear % or quadratic fashion. % MATHPRO has two functions, polyfit and polyval, which can quickly % and easily fit a polynimial to a set of data points. % Enter independant variable of the data set x = [0,1,2,4]; % Enter dependant variable of the data set y = [5,4,7,25]; % coefficients = polyfit(x,y,n) finds the coefficients of a polynomial p(x) % of degree n that fits the data, p(x(i)) to y(i), in a least squares sense. % The result coefficients is a row vector of length n+1 containing the polynomial coefficients in descending powers. coefficients = polyfit(x,y,2); % generates the coefficients of a second degree polynomial best characterizing the data set % estimate y at xp xp = 1.5; % If coefficients is a vector whose elements are the coefficients of a polynomial, % then polyval(coefficients,xp) is the value of the polynomial evaluated at xp. yp = polyval(coefficients,xp); % generates a new value (estimate) of y based on the coefficients found with polyfit % display the result write("y value at xp = ",yp); write("Coefficients of the polynomial are ",coefficients);