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)];
u = A \ y;
x_range = linspace(-0.1, 1.1, 30);
x_range = x_range(:);
y_hat = u(1) * x_range + u(2);
plot(x_range, y_hat, "x--");

A = [x.^2, x, ones(N, 1)];
u = A \ y;
B = [x_range.^2, x_range, ones(30, 1)];
y_hat = B * u;
plot(x_range, y_hat, "x--");

u = polyfit(x, y, 6);
y_hat = polyval(u, x_range);
plot(x_range, y_hat, "x--");




