Image:Linear least squares2.png

From Wikipedia, the free encyclopedia

Wikimedia Commons logo This is a file from the Wikimedia Commons. The description on its description page there is shown below.
Commons is a freely licensed media file repository. You can help.
Description

Illustration of linear least squares.

Source

self-made with MATLAB, tweaked in Inkscape.

Date

06:21, 5 April 2008 (UTC)

Author

Oleg Alexandrov

Permission
(Reusing this image)

See below


This diagram image should be recreated using vector graphics as an SVG file. This has several advantages; see Commons:Media for cleanup for more information. If an SVG form of this image is already available, please upload it. After uploading an SVG, replace this template with template {{Vector version available|new image name.svg}} in this image.

العربية | Български | Català | Česky | Dansk | Deutsch | English | Esperanto | Español | Français | 한국어 | Italiano | Magyar | Lietuvių | Nederlands | 日本語 | Polski | Português | Română | Русский | Suomi | Svenska | Türkçe | Українська | Tiếng Việt | मराठी | ‪中文(繁體)‬ | ‪中文(简体)‬ | +/-


Public domain I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.

In case this is not legally possible:
I grant anyone the right to use this work for any purpose, without any conditions, unless such conditions are required by law.


Afrikaans | Alemannisch | Aragonés | العربية | Asturianu | Български | Català | Česky | Cymraeg | Dansk | Deutsch | Eʋegbe | Ελληνικά | English | Español | Esperanto | Euskara | Estremeñu | فارسی | Français | Galego | 한국어 | हिन्दी | Hrvatski | Ido | Bahasa Indonesia | Íslenska | Italiano | עברית | Kurdî / كوردی | Latina | Lietuvių | Latviešu | Magyar | Македонски | Bahasa Melayu | Nederlands | ‪Norsk (bokmål)‬ | ‪Norsk (nynorsk)‬ | 日本語 | Polski | Português | Ripoarisch | Română | Русский | Shqip | Slovenčina | Slovenščina | Српски / Srpski | Svenska | ไทย | Tagalog | Türkçe | Українська | Tiếng Việt | Walon | ‪中文(简体)‬ | ‪中文(繁體)‬ | zh-yue-hant | +/-

[edit] Source code (MATLAB)

% Illustration of linear least squares.
function main()
 
   % KSmrq's colors
   red    = [0.867 0.06 0.14];
   blue   = [0, 129, 205]/256;
   green  = [0, 200,  70]/256;
   yellow = [254, 194,   0]/256;
   white = 0.99*[1, 1, 1];
   gray = 0.1*white;
 
% Set up the grid and other parameters
   N = 100;
   A = -2.2; B = 2; 
   X = linspace(A, B, N);
   C=-4; D = 4;
 
   % Set up the figure
   lw = 5; % linewidth
   lw2 = lw/2;
   fs = 22; % font size
   figure(1); clf; hold on;
   set(gca, 'fontsize', fs);
   set(gca, 'linewidth', lw2)
   hold on; grid on;
 
   % random numbers
   s=0.16;
   a = 1.2; b = 3; c = 1;
   p = 1; q = 6.5; r = 1.3;
   M = 50;
   p = s*p; q = s*q; r = s*r;
   XX=linspace(A, B, M+1);
   YY = p+q*XX+r*XX.^2;
 
   Xr = 0*(1:M);
   Yr = Xr;
   for i=1:M
      rd=rand(1);
      Xr(i) = XX(i)*rd+XX(i+1)*(1-rd);
      Yr(i) = p+q*Xr(i)+r*Xr(i)^2 + c*(rand(1)-0.5);
   end
 
   myrad = 0.05;
   for i=1:length(Xr)
      ball(Xr(i), Yr(i), myrad, red);
   end
 
   axis equal;
 
 
   % least squares fitting
   Yr = Yr';
   Xr=Xr';
   Mat = [(0*Xr+1) Xr Xr.^2];
   V=Mat'*Yr;
   V=(Mat'*Mat)\V;
   pe = V(1); qe = V(2); re=V(3);
 
   plot(X, pe+qe*X+re*X.^2, 'b', 'linewidth', lw);
 
   grid on;
   set(gca, 'GridLineStyle', '-', 'xcolor', gray);
   set(gca, 'GridLineStyle', '-', 'ycolor', gray);
   set(gca, 'XTick', [-2 -1 0 1 2]);
   plot([-2 2], [3.5 3.5], 'linewidth', lw2, 'color', gray);
 
   axis equal;
   axis([-2, 2, -1.5, 3.5]);
 
   saveas(gcf, 'Linear_least_squares2.eps', 'psc2'); % save as eps
   %plot2svg('Linear_least_squares.svg'); % save as svg
 
 
function ball(x, y, r, color)
   Theta=0:0.1:2*pi;
   X=r*cos(Theta)+x;
   Y=r*sin(Theta)+y;
   H=fill(X, Y, color);
   set(H, 'EdgeColor', 'none')

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeDimensionsUserComment
current06:32, 5 April 2008889×1,067 (34 KB)Oleg Alexandrov (tweak)
06:30, 5 April 2008889×1,067 (35 KB)Oleg Alexandrov (tweak)
06:29, 5 April 2008889×1,067 (34 KB)Oleg Alexandrov (tweak)
06:28, 5 April 2008444×533 (17 KB)Oleg Alexandrov
06:25, 5 April 2008444×533 (16 KB)Oleg Alexandrov (tweak)
06:21, 5 April 2008444×533 (16 KB)Oleg Alexandrov ({{Information |Description=Illustration of linear least squares. |Source=self-made with MATLAB, tweaked in Inkscape. |Date=~~~~~ |Author= Oleg Alexandrov |Permission=See below |other_versions= }} {{PD)
The following pages on the English Wikipedia link to this file (pages on other projects are not listed):