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

x_range = linspace(0, 1, 100);
y_range = [x_range', ones(100, 1)] * coeff;
plot(x_range, y_range, '.--');
polyfit(x, y, 1)

A = [x .^ 2, x, ones(N, 1)];
coeff = A \ y

x_range = linspace(0, 1, 100);
y_range = [x_range' .^ 2, x_range', ones(100, 1)] * coeff;
plot(x_range, y_range, '.--');