How can I build a Polynomial Equation from an array without using Polyval/Polyfit

14 views (last 30 days)
So, I've completed my homework assignment and it works great! But my Professor said that I couldn't use polyval or poly fit for Polynomial Regression.
So, I've started a new code, and I understand what to do to make it work the long way (i.e. without the poly functions) but I need to build a code that takes the data from a 28x2 matrix and build the polynomial equation, and I'd rather not just code in X'.^27,X^26,X^25...... etc.
I'm sure it's an easy code to make, but I've spent a couple days digging through the internet and pretty much everything tells me to use the poly functions.
My original code is the first set of code, my new code..... minus the power function is the second set of code and all the bells and whistles to find r^2 and plot in lenear,quadratic and cubic form.
function O=PolyLeast(x,y,A)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Establish index so program will solve to a degree based on matrix length %
fid=fopen('data-1.txt','rt');
C=textscan(fid,'%f%f','MultipleDelimsAsOne',true,'Delimiter',...
'[;','HeaderLines',1);
fclose(fid);
A=cell2mat(C);
x=A(:,1);
y=A(:,2);
%a=str2double(inputdlg({'Degree'},...
% 'Input desired degree to solve for',[1 75;]));
n=length(x);
plot(x,y,'o','MarkerSize',6,'MarkerFaceColor','b')
hold on
p1=polyfit(x,y,1);
f1=polyval(p1,x);
p2=polyfit(x,y,2);
f2=polyval(p2,x);
p3=polyfit(x,y,3);
f3=polyval(p3,x);
pn=polyfit(x,y,n);
fn=polyval(pn,x);
plot(x,f1,'r','LineWidth',2)
plot(x,f2,'m','LineWidth',2)
plot(x,f3,'k','LineWidth',2)
legend('Original Data','Linear Data','Quadratic Data','Cubic Data')
title('Polynomial Least Square')
hold off
disp('--------------------------------------------------------------------')
disp('Calculated y-Coefficients')
disp('--------------------------------------------------------------------')
Q=[f1,f2,f3,fn];
R={'Linear','Quadratic','Cubic','n-th degree'};
M=[R;num2cell(Q)];
disp(M)
disp('--------------------------------------------------------------------')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Solves for Coefficient of Determination and Reports to Command Window %
disp('Coefficient of Determination and Regression Polynomials')
disp('--------------------------------------------------------------------')
F=p1(1)*x+p1(2);
Fy=y-F;
Xpy=sum(Fy.^2);
XPxy=(length(y)-1)*var(y);
O=1-Xpy/XPxy;
fprintf('Linear Coefficient of Determination= %f\n',O);
formatspec='Linear Regression Polynomial: y=%5.3f(x)+%5.3f\n';
fprintf(formatspec,p1(1),p1(2));
F1=p2(1)*x.^2+p2(2)*x+p2(3);
F1y=y-F1;
Xpy1=sum(F1y.^2);
Xpxy1=(length(y)-1)*var(y);
O1=1-Xpy1/Xpxy1;
fprintf('Quadratic Coefficient of Determination= %f\n',O1);
formatspec='Quadratic Regression Polynomial: y=%5.3f(x^2)+%5.3f(x)+%5.3f\n';
fprintf(formatspec,p2(1),p2(2),p2(3));
F2=p3(1)*x.^3+p3(2)*x.^2+p3(3)*x+p3(4);
F2y=y-F2;
Xpy2=sum(F2y.^2);
Xpxy2=(length(y)-1)*var(y);
O2=1-Xpy2/Xpxy2;
fprintf('Cubic Coefficient of Determination= %f\n',O2);
formatspec='Cubic Regression Polynomial: y=%5.3f(x^3)+%5.3f(x^2)+%5.3f(x)+%5.3f\n';
fprintf(formatspec,p3(1),p3(2),p3(3),p3(4));
Fn=pn(1)*x.^27+pn(2)*x.^26+pn(3)*x.^25+pn(4)*x.^24;
Fny=y-Fn;
Xpy3=sum(Fny.^2);
Xpxy3=(length(y)-1)*var(y);
O3=1-Xpy3/Xpxy3;
fprintf('n-th Coefficient of Determination= %f\n',O3);
formatspec=...
'n-th order Regression Polynomial:\n y=%7.7e(x^27)+%7.7e(x^26)+%7.5e(x^25)+%7.5e(x^24)\n';
fprintf(formatspec,pn(1),pn(2),pn(3),pn(4));
disp('And so on for 23 more iterations')
disp('--------------------------------------------------------------------')
-----------------------------------------new unfinished code below -----------------------------------------------------------------
fid=fopen('data-1.txt','rt');
C=textscan(fid,'%f%f','MultipleDelimsAsOne',true,'Delimiter',...
'[;','HeaderLines',1);
fclose(fid);
A=cell2mat(C);
x=A(:,1);
y=A(:,2);
n=length(x);
X=[ones(n,1) x(:)];
f=% Power function goes here
a=(X'.*X)\(X'.*f(:));
plot(x,y,'.b')
  3 Comments
Terry Poole
Terry Poole on 21 Apr 2019
Thats what I needed, got the linear one done in seconds. Just gotta work out a few things to get the quadratic and cubic, but thanks a lot!
Walter Roberson
Walter Roberson on 21 Apr 2019
[ones(n,1), x(:), x(:).^2] \ f(:)
You do not need the X'. part: it complicates matters without any benefit.

Sign in to comment.

Answers (0)

Categories

Find more on Polynomials in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!