%NMA_aproximace
x=[-2 -1 0 0 0 1 2];               %zadané jako řádek
y=[6.5 1 0.3 1.2 0 5 14.5];    %zadané jako řádek
%"polopatické" sestavení soustavy rovnic
pocet=length(x);    %počet prvků
soucetX= sum(x);
soucetX2 = sum(x.^2);
soucetX3 = sum(x.^3);
soucetX4 = sum(x.^4);
soucetY   = sum(y);
soucetXY  = sum(x.*y);
soucetX2Y = sum(x.^2.*y);
% matice pro polynom 1. stupne
A1=[pocet   soucetX
    soucetX soucetX2];
% prava strana
b1=[soucetY
    soucetXY];
%koeficienty polynomu 1. stupne
P1=A1\b1; a0=P1(1);a1=P1(2);
fprintf("Polynom 1. stupně\nSoustava rovnic:\n")
fprintf("(%4.4f) a0 + (%4.4f) a1 = %4.4f\n",A1(1,1), A1(1,2),b1(1))
fprintf("(%4.4f) a0 + (%4.4f) a1 = %4.4f\n",A1(2,1), A1(2,2),b1(2))
fprintf("Polynom 1. stupně\n")
fprintf("p(x)=(%4.4f)  + (%4.4f) x \n", P1(1),P1(2))
%-------------
% matice pro polynom 2. stupne
A2=[pocet    soucetX  soucetX2
    soucetX  soucetX2 soucetX3
    soucetX2 soucetX3 soucetX4];
% prava strana
b2=[soucetY
    soucetXY
    soucetX2Y];
%koeficienty polynomu 2. stupne
P2=A2\b2;
a0=P2(1);
a1=P2(2);
a2=P2(3);
fprintf("Polynom 2. stupně\nSoustava rovnic:\n")
fprintf("(%4.4f) a0 + (%4.4f) a1 + (%4.4f) a2 = %4.4f\n",A2(1,1),A2(1,2),A2(1,3),b2(1))
fprintf("(%4.4f) a0 + (%4.4f) a1 + (%4.4f) a2 = %4.4f\n",A2(2,1),A2(2,2),A2(2,3),b2(2))
fprintf("(%4.4f) a0 + (%4.4f) a1 + (%4.4f) a2 = %4.4f\n",A2(3,1),A2(3,2),A2(3,3),b2(3))
fprintf("Polynom 2. stupně\n")
fprintf("p(x)=(%4.4f)  + (%4.4f) x + (%4.4f) x^2\n", P2(1),P2(2),P2(3))


