Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

% Function Name: uniqueBestFit % Inputs (2): - (double) x-data % - (double) y-da

ID: 3539724 • Letter: #

Question


% Function Name: uniqueBestFit

% Inputs (2): - (double) x-data

%              - (double) y-data

% Outputs (0): - None

% Output Plots (1): Plot of the different subplots leading to a unique fit

%

% Function Description:

%   Write a function called "uniqueBestFit" that takes in x and y data and

%   creates a plot of all of the polynomial fits until the average absolute

%   difference between the input data and the fit is less than 0.75.

%            Each unique fit will be its own subplot, with two subplots per row. If

%            you do not already know curve fitting, you can get more information in

%            this article:

%   http://en.wikipedia.org/wiki/Curve_fitting

%

%   Specifics:

%   - For each fit, you should calculate the average absolute difference

%            (AAD) between the y-values obtained from evaluating the fitted curve

%            at the original x-values and the original y values.

%   - Plot all the original points in each subplot with red "x"s.

%   - If the average absolute difference (mean absolute difference)

%     is less than 0.75, plot it in blue, otherwise, plot it in black.

%   - Use 100 linearly spaced points between the smallest and largest x

%     value for new x values when you are plotting the polynomial fit

%     curves.

%   - The title of the fitted plots should read '__ Order Fit', where the

%     blank is the ordinal number of the fit. Example, a first order fit

%     should read '1st Order Fit', while second order fit will read '2nd

%     Order Fit' and so on.

%   - x-values are not guaranteed to be in any particular order, but the

%     1st x-value will correspond to the 1st y-value and so on.

%   - There is not guaranteed to be a fit with average absolute difference

%     strictly less than 0.75.

%   - You should continue plotting until you have reached the last *UNIQUE*

%     polynomial, even after you have found the first polynomial with AAD

%     less than 0.75.

%

% Constraints:

%   - You may assume that you are to begin with a 1st order fit, i.e. that

%     the input vector is a length of at least 2. Note that even though

%     only one plot will exist when the input vector is of length 2, you

%     must still adhere to the rule that there are to be enough space for

%     two subplots per row.

%   - You may assume that the input vectors are the same size.

%

% Test Cases:

%   uniqueBestFit(1:5,[5 1 15 9 8])

%       => should look like UBF_Solution1.png

%   uniqueBestFit([1 6 2 4 9 8 5 3], [0.5 0.9 1.5 2.0 0.1 0.1 1.3 0.2])

%       => should look like UBF_Solution2.png

%   uniqueBestFit([0.2 0.5 1.2 1.6 2.6 9.6],[1 3 1 6 7 0.1])

%       => should look like UBF_Solution3.png

Explanation / Answer

function uniqueBestFit(x,y)

len=length(y);

label={'1st','2nd','3rd'};

r=floor(len/2);

found=false;

for k=1:(len-1)

subplot(r,2,k), plot(y,'rx');

hold on;

if k<3

title(strcat(label{k},' Order Fit'));

else

title(strcat(num2str(k),'th Order Fit'));

end

p=polyfit(x,y,k);

ynew=polyval(p,x);

AAD=sum(abs(ynew-y))/len;

xplot=min(x):((max(x)-min(x))/100):max(x);

yplot=polyval(p,xplot);

if AAD<0.75 && found==false

plot(xplot,yplot,'b');

found=true;

else

plot(xplot,yplot,'k');

end

  

end

end