File:Linear least squares example.png
From Wikimedia Commons, the free media repository
Jump to navigation
Jump to search
Linear_least_squares_example.png (428 × 526 pixels, file size: 12 KB, MIME type: image/png)
File information
Structured data
Captions
DescriptionLinear least squares example.png | Illustration of linear least squares. |
Date | (UTC) |
Source | self-made with MATLAB |
Author | Oleg Alexandrov |
This diagram was created with MATLAB.
Public domainPublic domainfalsefalse |
I, the copyright holder of this work, release this work into the public domain. This applies worldwide. In some countries this may not be legally possible; if so: I grant anyone the right to use this work for any purpose, without any conditions, unless such conditions are required by law. |
Source code (MATLAB)[edit]
% 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
Xr = [-1, 0, 2, 4];
Yr = [3 6 8 9];
N = 100;
small = 0.2;
X = linspace(min(Xr)-4*small, max(Xr)+4*small, 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;
myrad = 0.1;
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];
V=Mat'*Yr;
V=(Mat'*Mat)\V;
pe = V(1); qe = V(2);
plot(X, pe+qe*X, 'b', 'linewidth', lw);
grid on;
set(gca, 'GridLineStyle', '-', 'xcolor', gray);
set(gca, 'GridLineStyle', '-', 'ycolor', gray);
set(gca, 'XTick', min(Xr):1:max(Xr));
axis equal;
axis([min(Xr)-2*small max(Xr)+2*small min(Yr)-2*small max(Yr)+4*small]);
saveas(gcf, 'Linear_least_squares_example.eps', 'psc2'); % save as eps
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')
This math image could be re-created 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 available, please upload it and afterwards replace this template with
{{vector version available|new image name}} .
It is recommended to name the SVG file “Linear least squares example.svg”—then the template Vector version available (or Vva) does not need the new image name parameter. |
File history
Click on a date/time to view the file as it appeared at that time.
Date/Time | Thumbnail | Dimensions | User | Comment | |
---|---|---|---|---|---|
current | 05:30, 22 May 2008 | 428 × 526 (12 KB) | Oleg Alexandrov (talk | contribs) | {{Information |Description=Illustration of linear least squares. |Source=self-made with MATLAB |Date=~~~~~ |Author= Oleg Alexandrov |Permission=See below |other_versions= }} {{PD-self}} ==Source code |
You cannot overwrite this file.
File usage on Commons
There are no pages that use this file.