clear; clc; close all;

rng(3); % fix the random seed

N = 10;
x = linspace(0, 1, N); x = x(:);
y = cos(rand(size(x)) * pi / 2) + x .^ 2;
figure; hold on; grid on; plot(x, y, 'o');

A = [x, ones(N, 1)];
coeff = A \ y
x1 = linspace(-0.1, 1.1, 100);
y1 = coeff(1) * x1 + coeff(2);
plot(x1, y1, '--');

A = [x .^ 2, x, ones(N, 1)];
coeff = A \ y
x2 = linspace(-0.1, 1.1, 100)';
y2 = [x2 .^ 2, x2, ones(100, 1)] * coeff;
plot(x2, y2, '--');

coeff = polyfit(x, y, 7)
x3 = linspace(-0.1, 1.1, 100)';
y3 = polyval(coeff, x3);
plot(x3, y3, '--');

