Cody

Problem 45854. Least Absolute Deviations (L1-norm) line fit - degree n

Solution 2962180

Submitted on 18 Sep 2020 by Rafael S.T. Vieira
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
d=2; X=[-2 -1 0 1 2 3]'; Y=[7.14 -1.87 -0.89 -0.75 2.08 7.86]'; P=LADfit(X,Y,d); % P_correct = [1.387 -1.243 -0.894] res=sum(abs(Y-polyval(P_lad,X))); % Optimal residual = 3.698 assert(res<4.0)

Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 3.715401 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 5.563974 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 3.698000 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 3.954702 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 3.698000 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 3.698000 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 3.698145 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 3.698000 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 3.698000 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 3.698000 ans = []

2   Pass
d=3; X=[-4 -3 -2 -1 0 1 2 3 4]'; Y=[186 408 21 8 2 -4 -21 -57 -121]'; P=LADfit(X,Y,d); % P_correct = [-2.0431 2.1486 -5.6861 -1.8778] res=sum(abs(Y-polyval(P_lad,X))); % Optimal residual = 341.23 assert(res<350)

Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 365.066608 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 360.613211 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 344.140816 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 364.210624 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 341.420639 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 347.227020 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 349.672006 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 343.783050 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 344.384457 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 357.230254 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 360.931033 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 346.752534 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 380.832007 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 369.588197 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 405.141332 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 432.453038 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 389.519112 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 456.227572 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 444.818698 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 421.271475 ans = []

3   Pass
d=3; n=10; % Nr of points no=2; % Nr of outliers max_res = 0; max_ratio = 0; for cycle=1:10 rng('shuffle'); P=(rand(d,1)-0.5)*10; % Generate model X=(rand(n,1)-0.5)*10; % Generate X Y=polyval(P,X)+(rand(n,1)-0.5)*2; % Generate Y and add noise Y(1:no)=Y(1:no)+(rand(no,1)-0.5)*50; % Add outliers P_lad=LADfit(X,Y); P_lin=polyfit(X,Y,1); Y_lad=polyval(P_lad,X); res_lad=sum(abs(Y-Y_lad)); Y_lin=polyval(P_lin,X); res_lin=sum(abs(Y-Y_lin)); max_res=max(max_res,res_lad); max_ratio=max(max_ratio,res_lad/res_lin); end assert(max_res<50.0 & max_ratio<0.98); % Should be always better than L-2 and under 50

Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 190.201082 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 156.906264 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 78.644647 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 163.849634 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 111.969516 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 91.768074 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 56.933385 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 53.423260 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 27.565204 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 29.375561 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 31.794636 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 38.655094 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 29.699361 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 27.329469 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 70.465770 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 39.007474 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 127.442457 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 138.359317 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 92.677200 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 44.951469 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 94.330415 ans = [] Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 58.706764 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 45.760530 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 56.974878 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 63.649355 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 68.020766 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 45.010479 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 38.695194 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 38.419030 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 48.970578 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 41.885637 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 41.049314 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 40.503087 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 50.820073 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 39.991654 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 91.143096 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 84.549727 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 88.377640 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 100.021237 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 54.156009 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 52.824250 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 137.655046 ans = [] Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 94.815339 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 102.899143 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 134.825577 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 105.611302 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 82.984051 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 49.903311 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 97.635293 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 76.785900 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 59.028911 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 55.393176 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 84.098822 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 60.771091 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 40.452633 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 67.491944 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 42.305670 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 65.356283 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 75.831664 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 56.981465 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 41.116467 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 49.413139 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 105.081394 ans = [] Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 180.778908 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 30.49...

Suggested Problems

More from this Author10

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!