function ex12()
%% a.
in = load("cp.dat");
t = in(:,1);
cp = in(:,2);

tMin = min(t)-2;
tMax = max(t)+2;

poly1 = polyfit(t, cp, 1);
poly2 = polyfit(t, cp, 2);
poly3 = polyfit(t, cp, 3);
poly4 = polyfit(t, cp, 4);
poly8 = polyfit(t, cp, 8);

tF = tMin:0.1:tMax;
cp1 = polyval(poly1, tF);
cp2 = polyval(poly2, tF);
cp3 = polyval(poly3, tF);
cp4 = polyval(poly4, tF);
cp8 = polyval(poly8, tF);

plot(tF, cp1, "b-", tF, cp2, "b-.", tF, cp3, "k-", tF, cp4, "k-.")
hold("on")
plot(tF, cp8, "Color", [0 0.5 0])
plot(t, cp, "r*")
hold("off")
xlabel("t [°C]","FontSize",12);
ylabel("c_p [kJ/(kg K)]","FontSize",12);
legend("1. Ordnung", "2. Ordnung", "3. Ordnung", "4. Ordnung", ...
       "8. Ordnung", "Daten", "Location", "best");

F = getframe(gcf);
imwrite(F.cdata, "bild30.png");

%% b.
in = load("cps.dat");
t = in(:,1);
cp = in(:,2);
sigma = in(:,3);

% Systemgleichungen aufstellen
sigma4Mat = sigma*ones(1, 5);
A4 = [ones(length(t),1), t, t.^2, t.^3, t.^4]./sigma4Mat;
b = cp./sigma;

% Gleichungen lösen -> Polynomkoeffizienten
a4 = A4 \ b;
poly4b = a4(5:-1:1);

tMin = min(t)-2;
tMax = max(t)+2;
tF = tMin:0.1:tMax;
cp4b = polyval(poly4b, tF);

plot(tF, cp4, "k-", tF, cp4b, "k-.", t, cp, "r*")
xlabel("t [°C]","FontSize",12);
ylabel("c_p [kJ/(kg K)]","FontSize",12);
legend("4. Ord. (a)", "4. Ord. (b)", "Daten", "Location", "best");

F = getframe(gcf);
imwrite(F.cdata, "bild31.png");

