Here is matlab script for representing data into polynomial equation:
function [A] = polynomialfit(xi,yi)Save it as m-file with name "polynomialfit.m".
% regresi polynomial
% y = a0 + a1.x + a2.x'2 + ... + an.x'n
nxi = numel(xi);
nyi = numel(yi);
if nxi ~= nyi
disp('ukuran data tidak sama');
return;
end
%instruksi pemilihan orde polinomial
polorde = input('masukkan nilai orde polinomial=');
%matrix X
for barissigmax=1:polorde+1
for kolomsigmax=1:polorde+1
xin = xi.^(barissigmax+kolomsigmax-2);
sxin = sum(xin);
sigmax(barissigmax,kolomsigmax) = sxin;
end
end
%matrix Y
for barissigmay=1:polorde+1
xiyin = yi.*(xi.^(barissigmay-1));
sxiyin = sum(xiyin);
sigmay(barissigmay,1) = sxiyin;
end
%matrix A
A = sigmax\sigmay;
%display persamaan polinomial hasil regresi
namakurva = ['y = ',num2str(A(1))];
for datanama = 1:polorde;
if datanama == 1
if A(polorde+1) > 0
namakurva = [namakurva, ' + ', num2str(A(datanama+1)),'x'];
elseif A(polorde+1) < 0
namakurva = [namakurva, ' - ', num2str(abs(A(datanama+1))),'x'];
else
break;
end
else
if A(polorde+1) > 0
namakurva = [namakurva, ' + ', num2str(A(datanama+1)),'x^',num2str(datanama)];
elseif A(polorde+1) < 0
namakurva = [namakurva, ' - ', num2str(abs(A(datanama+1))),'x^',num2str(datanama)];
else
break;
end
end
end
disp(namakurva);
%kurva hasil regresi
xir= xi(1):((xi(nxi)-xi(1))/100):xi(nxi);
yregresi = 0;
for dataorde = 0:polorde
yregresi = yregresi + A(dataorde+1)*(xir.^dataorde);
end
%plotting grafik kurva dan plot data
figure(polorde);
scatter(xi,yi);
hold on;
plot(xir,yregresi,'r');
text(xir(1)+(5*((xi(nxi)-xi(1))/100)),yregresi(1),namakurva);
hold off;
end
Just call "polynomialfit(a,b)" in command window.
Variable a (array of independent variable data) and b (dependent variable data) must have been defined before.
Then, some instructions will appear:
masukkan nilai orde polinomial=Just type your desired polynomial-order of equation that is equal or larger than 1 (such as 1, 2, 3, 24, etc).
Enter.
There will be figure window containing graph which show scatter of data and plot of best fit polynomial equation.