%%% Section 1.1 %%% Line-fitting with LLSE clear close all m = 1/3; b = -2; x = [-2;4]; y = [x,ones(2,1)]*[m;b]; xl = [-10:1:10]'; Xl = [xl,ones(21,1)]; yl = Xl*[m;b]; figure(1) plot(xl,yl) hold on scatter(x,y) hold off xlim([min(xl)-3,max(xl)+3]) ylim([min(yl)-3,max(yl)+3]) grid on text(-5,0,['Y = ',num2str(m),'x - ',num2str(b)]) %%% LLSE Line Fitting x = 20*(rand(24,1)-.5) X = [x,ones(24,1)]; y = X*[m;b]+.5*randn(24,1); z = inv(X'*X)*X'*y yle = Xl*z; figure(2) plot(xl,yl,'-') hold on plot(xl,yle,'k--','Linewidth',2) scatter(x,y) hold off xlim([min(xl)-3,max(xl)+3]) ylim([min(yl)-3,max(yl)+3]) grid on text(-5,0,['Y = ',num2str(m),'x - ',num2str(b)]) text(0,-5,['Y = ',num2str(z(1)),'x - ',num2str(z(2))]) legend('true line','fitted line') %%% m,b estimates. for n1 = 1:10000 x = 25*(rand(24,1)-.5) X = [x,ones(24,1)]; y = X*[m;b]+.5*randn(24,1); z = inv(X'*X)*X'*y; z_llse(:,n1) = z; end figure(3) % scatter(z_llse(1,:),z_llse(2,:),'Markeredgealpha',.3) % % xlim([0 0.5]), ylim([-2.5 -1.5]) % xlabel('m') % ylabel('b') % hold on % errorbar(mean(z_llse(1,:)'),mean(z_llse(2,:)'),std(z_llse(2,:)'),'-s','MarkerSize',10,... % 'MarkerEdgeColor','red','MarkerFaceColor','red') % histogram2(z_llse(1,:),z_llse(2,:),50,'DisplayStyle','tile') xlabel('m') ylabel('b') xlim([0.2 0.5]), ylim([-2.5 -1.5])