32 lines
628 B
Matlab
32 lines
628 B
Matlab
m = 2.0; % slope
|
|
sigma = 1.0; % standard deviation
|
|
n = 100; % number of data pairs
|
|
|
|
% data pairs:
|
|
x = 5.0*rand(n, 1);
|
|
y = m*x + sigma*randn(n, 1);
|
|
|
|
% fit:
|
|
slope = mleslope(x, y);
|
|
fprintf('slopes:\n');
|
|
fprintf('original = %.2f\n', m);
|
|
fprintf(' fit = %.2f\n', slope);
|
|
|
|
% lines:
|
|
xx = 0.0:0.1:5.0; % x-axis values
|
|
yorg = m*xx;
|
|
yfit = slope*xx;
|
|
|
|
% plot:
|
|
plot(xx, yorg, '-r', 'linewidth', 5);
|
|
hold on;
|
|
plot(xx, yfit, '-g', 'linewidth', 2);
|
|
plot(x, y, 'ob');
|
|
hold off;
|
|
legend('data', 'original', 'fit', 'Location', 'NorthWest');
|
|
legend('boxoff')
|
|
xlabel('x');
|
|
ylabel('y');
|
|
|
|
savefigpdf(gcf, 'mlepropfit.pdf', 12, 7);
|