Cody

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

Solution 2962183

Submitted on 18 Sep 2020 by Rafael S.T. Vieira
  • Size: 99
  • This is the leading solution.
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: 161.753772 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 111.129733 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 59.715572 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 50.475228 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 47.516629 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 49.029813 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 48.064213 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 55.016943 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 51.520480 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 52.993153 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 44.032294 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 53.028784 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 85.937831 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 83.183151 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 107.865236 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 128.430388 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 81.750795 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 150.450812 ans = [] Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 311.063239 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 159.251240 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 53.895255 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 89.309278 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 170.009373 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 115.907438 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 99.614213 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 68.987159 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 63.136708 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 66.195732 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 64.957047 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 47.954885 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 23.010449 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 49.283068 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 23.868421 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 49.656594 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 67.370061 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 105.350133 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 71.875918 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 53.503826 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 108.562362 ans = [] Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 137.968134 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 132.809026 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 112.027694 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 66.530468 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 75.815210 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 73.067230 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 40.866748 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 35.865681 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 28.001838 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 30.254510 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 56.302919 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 26.399218 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 30.704231 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 33.364159 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 77.461028 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 23.059671 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 22.516748 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 23.015796 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 22.509829 ans = [] Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 37.863969 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 68.845849 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 65.824678 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 84.850736 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 55.955282 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 50.643383 Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 42.5...

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!