MATLAB: New to matlab and am getting index exceed matrix dimensions error

MATLABmatlab novicematrix error

I am very new to matlab so forgive me if this is rudimentary. I am trying to find the sum of absolute deviations for some models and I am getting this matrix error. I would appreciate it if anyone could help me figure out whats going on! (The trouble is coming from where I try to define 'S') I think its occuring when I try to compute the exponent.
%Declare the data sets
x = [17, 19, 20, 22, 23, 25, 28, 31, 32, 33, 36, 37, 39, 42];
y = [19, 25, 32, 51, 57, 71, 113, 140, 154, 187, 192, 205, 250, 260];
plot(x, y, '*');
hold on
% Displays equation in form y = ax^2 + bx + c
p2 = polyfit(x, y, 2);
disp(['Equation is y = ' num2str(p2(1)) '*x^2 + ' num2str(p2(2)) '*x + ' num2str(p2(3))])
F2 = p2 (1).*x.^2 + p2 (2).*x + p2(3);
plot(x, F2, '-r')
title('3b')
xlabel('x')
ylabel('y')
%Calculate the sum of absolute deviations
S = sum(abs(y - p(1)*x.^2 - p(2)*x - p(3)));
disp(['The sum of absolute deviations is ' num2str(S)])
Thank you in advance!

Best Answer

I would use ‘F2’, since you already calculated it:
S = sum(abs(y - F2));
This avoids the error, and the inefficiency of recalculating ‘F2’ within the ‘S’ calculation.