2019.10.07 — dotyczy: 2019/2020 zima, rw
rand("seed", 6) randn("seed", 6) nsamples = 100; % wygeneruj losowe wartości w dziedzinie [-10, 10] x = interp1([0,1], [-10,10], sort(rand(nsamples,1))); % prosta o równaniu y = 2 * x + 3 + szum P = [2 3]; y = polyval(P,x) + randn(size(x))*5; save linear_regression.mat [x y] % wielomian 3ego stopnia ( -0.1*x^3 + 0.2*x^2 + 1 + szum ) P = [-0.1 0.2 0 1]; y = polyval(P,x) + randn(size(x))*15; save polynomial3_regression.mat [x y] % funkcja harmoniczna y = 0.5 * sin(x + 0.6) + randn(size(x))/5; save harmonic_regression.mat [x y]
load linear_regression.mat A = [x ones(size(x))]; L = -y; X= -inv(A'*A)*(A'*L) hold on plot (x,y,'*'); plot(x,polyval(X,x),"-") hold off print -djpg fig_linear.jpg pause
load polynomial3_regression.mat A = [x.^3 x.^2 x ones(size(x))]; L = -y; X = -inv(A'*A)*(A'*L) hold on plot (x,y,'*'); plot(x,polyval(X,x),"-") hold off print -djpg fig_polynomial.jpg pause