MATLAB Examples 2 (covering Statistics Lectures 3 and 4)
Contents
Example 1: Fit a linearized regression model
x = -20:20;
y = 1.5*x.^2 - 60*x + 10 + 30*randn(size(x));
X = [x(:).^2 x(:) ones(length(x),1)];
w = inv(X'*X)*X'*y(:);
w
w =
1.5368275257135
-60.638944801903
8.15661270608602
modelfit = X*w;
squarederror = sum((y(:)-modelfit).^2);
figure;
hold on;
scatter(x,y,'r.');
ax = axis;
xx = linspace(ax(1),ax(2),100)';
yy = [xx.^2 xx ones(length(xx),1)]*w;
plot(xx,yy,'k-');
xlabel('x');
ylabel('y');
title(sprintf('squared error = %.1f',squarederror));
Example 2: Fit a parametric nonlinear model
x = 0:.05:3;
y = 5*x.^3.5 + 5*randn(size(x));
options = optimset('Display','iter','FunValCheck','on', ...
'MaxFunEvals',Inf,'MaxIter',Inf, ...
'TolFun',1e-6,'TolX',1e-6);
paramslb = [-Inf 0];
paramsub = [ Inf Inf];
params0 = [ 1 1];
modelfun = @(pp,xx) pp(1)*xx.^pp(2);
[params,resnorm,residual,exitflag,output] = lsqcurvefit(modelfun,params0,x,y,paramslb,paramsub,options);
Norm of First-order
Iteration Func-count f(x) step optimality CG-iterations
0 3 431921 7.93e+03
1 6 431921 10 7.93e+03 0
2 9 247013 2.5 8.3e+04 0
3 12 247013 2.81299 8.3e+04 0
4 15 98286.2 0.703246 1.09e+05 0
5 18 17315.7 1.40649 3.78e+05 0
6 21 1894.05 0.327179 2.46e+04 0
7 24 1482.46 0.939262 9.09e+03 0
8 27 1241.08 0.422605 1.33e+03 0
9 30 1237.01 0.00758631 5.5 0
10 33 1237.01 1.29563e-05 7.82e-05 0
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the selected value of the function tolerance.
params
params =
5.4338041940236 3.42528833322854
modelfit = modelfun(params,x);
squarederror = sum((y(:)-modelfit(:)).^2);
figure;
hold on;
scatter(x,y,'r.');
ax = axis;
xx = linspace(ax(1),ax(2),100)';
yy = modelfun(params,xx);
plot(xx,yy,'k-');
xlabel('x');
ylabel('y');
title(sprintf('squared error = %.1f',squarederror));
Example 3: Another optimization example
x = rand(1,1000).^2;
options = optimset('Display','iter','FunValCheck','on', ...
'MaxFunEvals',Inf,'MaxIter',Inf, ...
'TolFun',1e-6,'TolX',1e-6);
paramslb = [];
paramsub = [];
params0 = [0];
costfun = @(pp) sqrt(abs(x-pp));
[params,resnorm,residual,exitflag,output] = lsqnonlin(costfun,params0,paramslb,paramsub,options);
Norm of First-order
Iteration Func-count f(x) step optimality CG-iterations
0 2 346.47 500
1 4 346.385 8.61928e-05 490 0
2 6 341.862 0.00491093 440 0
3 8 333.538 0.0101418 387 0
4 10 315.811 0.0259447 306 0
5 12 308.364 0.0129775 271 0
6 14 292.776 0.0324211 215 0
7 16 279.22 0.035143 170 0
8 18 270.08 0.0314543 118 0
9 20 263.724 0.031138 88 0
10 22 259.555 0.0265819 67 0
11 24 258.266 0.0107348 52 0
12 26 257.181 0.012298 35 0
13 28 256.649 0.00864949 25 0
14 30 256.518 0.00310659 19 0
15 32 256.401 0.00335775 15 0
16 34 256.317 0.00312291 12 0
17 36 256.252 0.00307883 7 0
18 38 256.245 0.000546973 6 0
19 40 256.236 0.000864426 5 0
20 42 256.227 0.0010377 4 0
21 44 256.224 0.000510969 3 0
22 46 256.22 0.000677599 3 0
23 48 256.218 0.000404828 1 0
24 50 256.218 0.000140253 1 0
25 52 256.217 0.000193302 1 0
26 54 256.217 0.00021431 1 0
27 56 256.217 0.000205629 1 0
28 58 256.216 9.93736e-05 5.88e-05 0
Local minimum possible.
lsqnonlin stopped because the final change in the sum of squares relative to
its initial value is less than the selected value of the function tolerance.
params
params =
0.260043242623885
median(x)
ans =
0.260365029783367