---
# System prepended metadata

title: Kamil Breguła - Lab 5 - ONIMN

---

# Kamil Breguła - Lab 5 - ONIMN

## Laboraitum 5 - Interpolacja i Aproksymacja

```matlab
% Zaadanie 1
% A
plot(x_z1a, y_z1a, 'o')

fla = polyfit(x_z1a, y_z1a, 5)

y1a = polyval(fla, x_z1a)


% B
plot(x_z1b, y_z1b, 'o')

flb = polyfit(x_z1b, y_z1b, 3)

y1b = polyval(flb, x_z1b)


% Wykresy
clf

hold on
plot(x_z1a, y_z1a, 'o')
plot(x_z1a, y1a, 'g-')
legend('dane', '7 * x^3 +  4 * x^3 4.7270 * x^2  5x + 5.0110')
hold off

clf

hold on
plot(x_z1b, y_z1b, 'o')
plot(x_z1b, y1b, 'g-')
legend('dane', '1x^3 + 5x^2 + 6x + 8')
hold off

% Zadanie 2

x1 = [0:0.2:40]
y1 = interp1(x_z2, y_z2, x1, 'linear')
y2 = interp1(x_z2, y_z2, x1, 'cubic')
y3 = interp1(x_z2, y_z2, x1, 'spline')

clf
hold on
plot(x_z2, y_z2, 'r s-', 'MarkerSize',15)
plot(x1, y1, 'b * -')
plot(x1, y2, 'g ^ -')
plot(x1, y3, 'k d -')
legend('data', 'linear', 'cubic', 'spline')
hold off

% Zadanie 3
clf
xi = (datetime(2018, 1,1):minutes(1):datetime(2018, 1,2))
V = interp1(x_z3, T_z3, xi, 'spline')

clf
hold on
plot(x_z3, T_z3, 'r s -', 'MarkerSize', 15)
plot(xi, V, 'g ^ -', 'MarkerSize', 1)
legend('data', 'spline')
hold off;

% Zaadanie 4
fa = polyfit(x_z4, y_z4, 1);
fb = polyfit(x_z4, y_z4, 3);
fc = polyfit(x_z4, y_z4, 4);

ya = y_z4;
r_x = min(x_z4):0.1:max(x_z4)
r_ya = polyval(fa, r_x)
r_yb = polyval(fb, r_x)
r_yc = polyval(fc, r_x)

clf
grid on;

hold on
plot(x_z4, y_z4, 's', 'MarkerSize', 10)
plot(x_z4, y_z4, 'o')

plot(r_x, r_ya)
plot(r_x, r_yb)
plot(r_x, r_yc)

legend('ya', 'y', '1', 'rzad length(x) = 3', '4')
hold off

err1 = sum((y_z4 - polyval(fa, x_z4)).^2)
err2 = sum((y_z4 - polyval(fb, x_z4)).^2)
err3 = sum((y_z4 - polyval(fc, x_z4)).^2)

[err1, err2, err3]

# Zadaanie 5
clf
x = x_z5
y = y_z5

A=[1./x, x]

c1 = A\y
% c1 4.3531 c2 =1.2521
f1 = @(x, c)  c1(1) ./ x + c1(2) .* x
rx1 = (min(x):0.02:max(x))
ry1 = f1(rx1, c1)

% B
x = x_z5
y = y_z5

A = [x.^2 x ones(7, 1)];
c2 = A\y
f2 = @(x, c) c2(1) .* (x.^2)  + c2(2) .* x + c2(3)

rx2 = (min(x):0.02:max(x))
ry2 = f2(rx2, c2)

% C
x = x_z5
y = y_z5

A = [x.^3 x.^2 1./x];
c3 = A\y
f3 = @(x, c) c3(1) .* (x.^3)  + c3(2) .* (x.^2) + c3(3).* (1./x)

rx3 = (min(x):0.02:max(x))
ry3 = f3(rx3, c3)

% Wykres
hold on
plot(x, y, 'd')
plot(rx1, ry1, 'b')
plot(rx2, ry2, 'g')
plot(rx3, ry3, 'm')
legend( ...
    'dane', ...
    '4.3513 ./ x + 1.2521 .* x', ...
    '0.553 .* (x.^2)  + (-2.3885) .* x + (7.3262)', ...
    '(-0.186) .* (x.^3)  + (0.9625) .* (x.^2) + (4.7925).* (1./x)' ...
    )
title('Zadanie 5')
hold off

err1 = sum((y - f1(x)).^2)
err2 = sum((y - f2(x)).^2)
err3 = sum((y - f3(x)).^2)

[err1, err2, err3]
%     0.0601    0.1692    0.0140

```

![](https://i.imgur.com/DF8LwgV.png)
![](https://i.imgur.com/BjdZA2N.png)
![](https://i.imgur.com/0ErnZeX.png)
![](https://i.imgur.com/lUoXiiB.png)
![](https://i.imgur.com/soAWEI4.png)
![](https://i.imgur.com/wi00Br0.png)
