{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2025-12-14T01:33:56.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2025-12-14T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":44843,"title":"Nth Order Polynomial Regression","description":"Inputs: \r\n\r\n  -two vectors x and y (row or column) \r\n\r\n  -order of polynomial, p\r\n\r\nOutputs: \r\n\r\n  -vector of coefficients [b0 b1 b2 ... bn]\r\n\r\n  -R-squared value\r\n\r\n  -vector of residuals, e\r\n","description_html":"\u003cp\u003eInputs:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e-two vectors x and y (row or column) \r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003e-order of polynomial, p\r\n\u003c/pre\u003e\u003cp\u003eOutputs:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e-vector of coefficients [b0 b1 b2 ... bn]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003e-R-squared value\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003e-vector of residuals, e\r\n\u003c/pre\u003e","function_template":"function [b,r2,e] = RegressionN(x,y,p)\r\n  b = x;\r\n  e = x;\r\n  r2 = x;\r\nend","test_suite":"%%\r\nx = 0:5;\r\ny = 2*x;\r\np = 1;\r\nb_correct = [0;2];\r\nr2_correct = 1;\r\ne_correct = [0,0,0,0,0,0]';\r\n[b,r2,e] = RegressionN(x,y,p)\r\nassert(nnz(abs(b-b_correct)\u003c1e-12)==p+1)\r\nassert(abs(r2-r2_correct)\u003c1e-12)\r\nassert(nnz(abs(e-e_correct)\u003c1e-12)==length(x))\r\n\r\n%%\r\nx = linspace(2,5,12);\r\ny = exp(x)';\r\np = 3;\r\nb_correct = [-110.9230,126.8980,-46.7825,6.3467]';\r\nr2_correct = 0.9997;\r\ne_correct = [0.8726,-0.6361,-0.8963,-0.4527,0.2214,0.7465,0.8663,0.4864,-0.2751,-1.0207,-0.9863,1.0742]';\r\n[b,r2,e] = RegressionN(x,y,p)\r\nassert(nnz(abs(b-b_correct)\u003c1e-4)==p+1)\r\nassert(abs(r2-r2_correct)\u003c1e-4)\r\nassert(nnz(abs(e-e_correct)\u003c1e-4)==length(x))\r\n\r\n%%\r\nx = linspace(5,7,8);\r\ny = [-0.9238   -1.5011   -0.2963   -0.2268   -0.9897   -0.0372    0.1509    1.5324];\r\np = 3;\r\nb_correct = [-196.1472 101.5622 -17.6070 1.0186]';\r\nr2_correct = 0.8063;\r\ne_correct = [0.2569 -0.6944 0.3739 0.4017 -0.4505 0.2224 -0.2019 0.0919]';\r\n[b,r2,e] = RegressionN(x,y,p)\r\nassert(nnz(abs(b-b_correct)\u003c1e-4)==p+1)\r\nassert(abs(r2-r2_correct)\u003c1e-4)\r\nassert(nnz(abs(e-e_correct)\u003c1e-4)==length(x))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":183622,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2019-01-30T14:42:51.000Z","updated_at":"2026-03-11T08:42:08.000Z","published_at":"2019-01-30T17:17:50.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eInputs:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[-two vectors x and y (row or column) \\n\\n-order of polynomial, p]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOutputs:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[-vector of coefficients [b0 b1 b2 ... bn]\\n\\n-R-squared value\\n\\n-vector of residuals, e]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":44314,"title":"A Simple Tide Gauge with MATLAB","description":"*\u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767* \r\n\r\nYou are standing in a few inches of sea water on a beach.\r\n\r\nYou are wondering whether the high tide is coming soon or it has just passed. \r\n\r\nTherefore, you will write a code in MATLAB to analyze following data. \r\n\r\nYou followed the sequence of water lines left by several swash of waves. \r\n\r\nThe data array A contains the distances the water traveled past your feet during each upward swash of waves. \r\n\r\nYour code will return 1 if the high tide is coming soon. \r\n\r\nYour code will return 0 if the high tide has just passed.    \r\n","description_html":"\u003cp\u003e\u003cb\u003e\u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767\u003c/b\u003e\u003c/p\u003e\u003cp\u003eYou are standing in a few inches of sea water on a beach.\u003c/p\u003e\u003cp\u003eYou are wondering whether the high tide is coming soon or it has just passed.\u003c/p\u003e\u003cp\u003eTherefore, you will write a code in MATLAB to analyze following data.\u003c/p\u003e\u003cp\u003eYou followed the sequence of water lines left by several swash of waves.\u003c/p\u003e\u003cp\u003eThe data array A contains the distances the water traveled past your feet during each upward swash of waves.\u003c/p\u003e\u003cp\u003eYour code will return 1 if the high tide is coming soon.\u003c/p\u003e\u003cp\u003eYour code will return 0 if the high tide has just passed.\u003c/p\u003e","function_template":"function tide = gauge(A)\r\n  tide=max(A)-min(A);\r\n  tide=tide*0;\r\nend","test_suite":"%%\r\nA = [5 8 10 12 8 13 14 10 10 15];\r\ntide_correct = 1;\r\nassert(isequal(gauge(A),tide_correct))\r\n%%\r\nA = [15 16 11 9 10 15 7 12 6 11 5 6];\r\ntide_correct = 0;\r\nassert(isequal(gauge(A),tide_correct))\r\n%%\r\nA = [9 15 3 9 5 18 4 17 18 19 8 13 12 21 17 24];\r\ntide_correct = 1;\r\nassert(isequal(gauge(A),tide_correct))\r\n%%\r\nA = [22 12 22 12 9 14 17 16 15 8 13 6 10 7 13 3];\r\ntide_correct = 0;\r\nassert(isequal(gauge(A),tide_correct))\r\n%%\r\n","published":true,"deleted":false,"likes_count":4,"comments_count":3,"created_by":166,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":394,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":34,"created_at":"2017-09-12T00:26:53.000Z","updated_at":"2026-03-25T04:12:58.000Z","published_at":"2017-10-16T01:45:07.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e\u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are standing in a few inches of sea water on a beach.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are wondering whether the high tide is coming soon or it has just passed.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eTherefore, you will write a code in MATLAB to analyze following data.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou followed the sequence of water lines left by several swash of waves.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe data array A contains the distances the water traveled past your feet during each upward swash of waves.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour code will return 1 if the high tide is coming soon.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour code will return 0 if the high tide has just passed.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":45851,"title":"Least Absolute Deviations (L1-norm) line fit - degree 1","description":"Matlab's polyfit function is very handy to find least-squares regression. It minimizes the (L2-norm) of the estimation errors, by solving a linear system.\r\n\u003chttps://www.mathworks.com/help/matlab/ref/polyfit.html\u003e\r\n\r\nAn often overlooked way to deal with these situations is to use Least Absolute Deviations (LAD) line fitting. It minimizes the L1-norm of the residuals, thus it is less sensitive to outliers that fall far away from the underlying model\r\nhttps://en.wikipedia.org/wiki/Least_absolute_deviations\r\n\r\n- - -\r\n\r\nYou are given two vectors X and Y (coordinates of observations on a plane).\r\nReturn a row vector *P = [a, b]* with the coefficients of the best-fit line, in the L1-norm sense. I.e., find *a* and *b* that minimize *sum( abs( Y - a*X - b ) )* .\r\n\r\n(compare your results with polyfit on the test suite!)","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 225px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 112.5px; transform-origin: 407px 112.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eMatlab's polyfit function is very handy to find least-squares regression. It minimizes the (L2-norm) of the estimation errors, by solving a linear system.\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/help/matlab/ref/polyfit.html\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e\u0026lt;https://www.mathworks.com/help/matlab/ref/polyfit.html\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e\u0026gt;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 31.5px; text-align: left; transform-origin: 384px 31.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eAn often overlooked way to deal with these situations is to use Least Absolute Deviations (LAD) line fitting. It minimizes the L1-norm of the residuals, thus it is less sensitive to outliers that fall far away from the underlying model\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://en.wikipedia.org/wiki/Least_absolute_deviations\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003ehttps://en.wikipedia.org/wiki/Least_absolute_deviations\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e- - -\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYou are given two vectors X and Y (coordinates of observations on a plane). Return a row vector\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eP = [a, b]\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e with the coefficients of the best-fit line, in the L1-norm sense. I.e., find\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003ea\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e and\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eb\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e that minimize\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003esum( abs( Y - a*X - b ) )\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e .\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e(compare your results with polyfit on the test suite!)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function P = LADfit(X,Y)\r\nP = polyfit(X,Y,1);\r\nend","test_suite":"%%\r\nX=[-2.8 -4.8 0.9 2.3 3.6 -4.2 -0.7 3.0 0.1 -1.0]';\r\nY=[12.0 6.5 -1.6 -2.1 -2.6 0.1 -1.1 -2.4 -1.3 -1.0]';\r\nP=LADfit(X,Y); % P_correct = [-10/27 -19/15]\r\nres=sum(abs(Y-polyval(P,X))); % Optimal residual = 1007/54\r\nassert(res\u003c19)\r\n\r\n%%\r\nX=[1 2 3 4 5]';\r\nY=[1 2 3 4 3]';\r\nP=LADfit(X,Y); % P_correct = [1 0]\r\nres=sum(abs(Y-polyval(P,X))); % Optimal residual = 2.0\r\nassert(res\u003c2.1)\r\n\r\n%%\r\nX=[1 2 3 4 5]';\r\nY=[1 0 0 0 0]';\r\nP=LADfit(X,Y); % P_correct = [0 0]\r\nres=sum(abs(Y-polyval(P,X))); % Optimal residual = 1.0\r\nassert(res\u003c1.01)\r\n\r\n%%\r\nn=10; % Nr of points\r\nno=2; % Nr of outliers\r\nmax_res = 0;\r\nmax_ratio = 0;\r\nfor cycle=1:10\r\n    rng('shuffle');\r\n    P=(rand(2,1)-0.5)*10; % Generate model\r\n    X=(rand(n,1)-0.5)*2; % Generate X\r\n    Y=polyval(P,X)+(rand(n,1)-0.5);  % Generate Y and add noise\r\n    Y(1:no)=Y(1:no)+(rand(no,1)-0.5)*10; % Add outliers\r\n\r\n    P_lad=LADfit(X,Y);\r\n    P_lin=polyfit(X,Y,1);\r\n\r\n    Y_lad=polyval(P_lad,X);\r\n    res_lad=sum(abs(Y-Y_lad));\r\n\r\n    Y_lin=polyval(P_lin,X);\r\n    res_lin=sum(abs(Y-Y_lin));\r\n\r\n    max_res=max(max_res,res_lad);\r\n    max_ratio=max(max_ratio,res_lad/res_lin);\r\nend\r\nassert(max_res\u003c12.0 \u0026 max_ratio\u003c0.99); % Should be always better than L-2 and under 12.0","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":452188,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":6,"test_suite_updated_at":"2020-11-22T01:25:26.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-06-10T07:18:59.000Z","updated_at":"2020-11-22T01:25:26.000Z","published_at":"2020-06-10T07:18:59.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMatlab's polyfit function is very handy to find least-squares regression. It minimizes the (L2-norm) of the estimation errors, by solving a linear system.\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/help/matlab/ref/polyfit.html\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026lt;https://www.mathworks.com/help/matlab/ref/polyfit.html\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026gt;\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAn often overlooked way to deal with these situations is to use Least Absolute Deviations (LAD) line fitting. It minimizes the L1-norm of the residuals, thus it is less sensitive to outliers that fall far away from the underlying model\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Least_absolute_deviations\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ehttps://en.wikipedia.org/wiki/Least_absolute_deviations\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e- - -\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are given two vectors X and Y (coordinates of observations on a plane). Return a row vector\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP = [a, b]\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e with the coefficients of the best-fit line, in the L1-norm sense. I.e., find\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ea\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eb\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that minimize\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003esum( abs( Y - a*X - b ) )\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e .\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(compare your results with polyfit on the test suite!)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":45854,"title":"Least Absolute Deviations (L1-norm) line fit - degree n","description":"This is a generalization of Problem 45851 for degree n\r\n\r\n\u003chttps://www.mathworks.com/matlabcentral/cody/problems/45851-least-absolute-deviations-l1-norm-line-fit-degree-1\u003e\r\n\r\n- - - -\r\n\r\nYou are given two vectors X and Y (coordinates of observations on a plane), and a degree d of the underlying model.\r\nReturn a row vector *P* with the *d+1* coefficients of the best-fit polynomial, in the L1-norm sense. I.e., find *P* of degree *d* that minimizes *sum( abs( Y - polyval(P,X) ) )* .\r\n\r\n(compare your results with polyfit on the test suite!)","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 183px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 91.5px; transform-origin: 407px 91.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 171.5px 8px; transform-origin: 171.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThis is a generalization of Problem 45851 for degree n\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/45851-least-absolute-deviations-l1-norm-line-fit-degree-1\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e\u0026lt;https://www.mathworks.com/matlabcentral/cody/problems/45851-least-absolute-deviations-l1-norm-line-fit-degree-1\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4px 8px; transform-origin: 4px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e\u0026gt;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 16px 8px; transform-origin: 16px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e- - - -\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 31.5px; text-align: left; transform-origin: 384px 31.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 370px 8px; transform-origin: 370px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eYou are given two vectors X and Y (coordinates of observations on a plane), and a degree d of the underlying model. Return a row vector\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4.5px 8px; transform-origin: 4.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eP\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 26px 8px; transform-origin: 26px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e with the\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 12.5px 8px; transform-origin: 12.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003ed+1\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 216.5px 8px; transform-origin: 216.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e coefficients of the best-fit polynomial, in the L1-norm sense. I.e., find\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4.5px 8px; transform-origin: 4.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eP\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 32.5px 8px; transform-origin: 32.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e of degree\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4.5px 8px; transform-origin: 4.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003ed\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e that minimizes\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 96.5px 8px; transform-origin: 96.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003esum( abs( Y - polyval(P,X) ) )\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4px 8px; transform-origin: 4px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e .\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 161px 8px; transform-origin: 161px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e(compare your results with polyfit on the test suite!)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function P = LADfit(X,Y,d)\r\nP = polyfit(X,Y,d);\r\nend","test_suite":"%%\r\nd=2;\r\nX=[-2 -1 0 1 2 3]';\r\nY=[7.14 -1.87 -0.89 -0.75 2.08 7.86]';\r\nP=LADfit(X,Y,d); % P_correct = [1.387 -1.243 -0.894]\r\nres=sum(abs(Y-polyval(P,X))); % Optimal residual = 3.698\r\nassert(res\u003c4.0)\r\n\r\n%%\r\nd=3;\r\nX=[-4 -3 -2 -1 0 1 2 3 4]';\r\nY=[186 408 21 8 2 -4 -21 -57 -121]';\r\nP=LADfit(X,Y,d); % P_correct = [-2.0431 2.1486 -5.6861 -1.8778]\r\nres=sum(abs(Y-polyval(P,X))); % Optimal residual = 341.23\r\nassert(res\u003c350)\r\n\r\n%%\r\nd=3;\r\nn=10; % Nr of points\r\nno=2; % Nr of outliers\r\nmax_res = 0;\r\nmax_ratio = 0;\r\nfor cycle=1:10\r\n    rng('shuffle');\r\n    P=(rand(d,1)-0.5)*10; % Generate model\r\n    X=(rand(n,1)-0.5)*10; % Generate X\r\n    Y=polyval(P,X)+(rand(n,1)-0.5)*2;  % Generate Y and add noise\r\n    Y(1:no)=Y(1:no)+(rand(no,1)-0.5)*50; % Add outliers\r\n    P_lad=LADfit(X,Y,d);\r\n    P_lin=polyfit(X,Y,1);\r\n    Y_lad=polyval(P_lad,X);\r\n    res_lad=sum(abs(Y-Y_lad));\r\n    Y_lin=polyval(P_lin,X);\r\n    res_lin=sum(abs(Y-Y_lin));\r\n    max_res=max(max_res,res_lad);\r\n    max_ratio=max(max_ratio,res_lad/res_lin);\r\nend\r\nassert(max_res\u003c50.0 \u0026 max_ratio\u003c0.98); % Should be always better than L-2 and under 50","published":true,"deleted":false,"likes_count":1,"comments_count":7,"created_by":452188,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":2,"test_suite_updated_at":"2021-04-19T10:10:15.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-06-10T08:00:01.000Z","updated_at":"2021-04-19T10:10:15.000Z","published_at":"2020-06-10T08:00:01.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is a generalization of Problem 45851 for degree n\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/45851-least-absolute-deviations-l1-norm-line-fit-degree-1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026lt;https://www.mathworks.com/matlabcentral/cody/problems/45851-least-absolute-deviations-l1-norm-line-fit-degree-1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026gt;\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e- - - -\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are given two vectors X and Y (coordinates of observations on a plane), and a degree d of the underlying model. Return a row vector\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e with the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ed+1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e coefficients of the best-fit polynomial, in the L1-norm sense. I.e., find\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of degree\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ed\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that minimizes\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003esum( abs( Y - polyval(P,X) ) )\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e .\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(compare your results with polyfit on the test suite!)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":44655,"title":"Computational power of Cody servers","description":"It has been \u003chttps://en.wikipedia.org/wiki/Moore%27s_law#History predicted\u003e that the performance of integrated circuits would _double_ every 18 months.  That suggests the time to perform a given computation should _halve_ roughly every 18 months.  \r\n\r\nWhat about on Cody?  Observational data is available from the final test case of \u003chttps://www.mathworks.com/matlabcentral/cody/problems/963-we-love-vectorized-solutions-problem-1-remove-the-row-average Problem 963\u003e to help us quantify the improvement in performance!  And thereby even make predictions for future computations.\r\n\r\n Solution    Date\t   Runtime [s]\r\n ...\r\n 144393\t     04-Oct-12\t   4.230\r\n ...\r\n 654812\t     17-Apr-15\t   3.099\r\n ...\r\n 1272817     20-Sep-17\t   2.0402\r\n ...\r\n\r\nThe complete data set will be provided to you as input.  You should assume the general trend can be described by the following law:\r\n\r\n runtime = r0 - δ [1 - exp(-t/k)]\r\n runtime = r∞ + δ exp(-t/k)\r\n\r\nwhere |r0| is the runtime at the start of the period in seconds, |r∞| is the predicted runtime (in seconds) that will be approached far in the future, |δ = r0 - r∞|, and |t| is the time in nominal years since the start of the period, and |k| is a kinetic parameter (in nominal years).  \r\n\r\nThe start of the period is defined by the earliest date in the series.  Compute the number of days exactly, and assume that a nominal year comprises 365.24 days.\r\n\r\nYour task is to \u003chttps://au.mathworks.com/help/matlab/math/example-curve-fitting-via-optimization.html fit the curve\u003e and thereby predict the runtime for various future dates.  Your output should be rounded to four decimal places.  \r\n\r\nMETHOD:  You _must_ use \u003chttps://au.mathworks.com/help/matlab/ref/fminsearch.html |fminsearch|\u003e to perform the non-linear regression, and you _must_ set the options using \u003chttps://au.mathworks.com/help/matlab/ref/optimset.html |optimset|\u003e to ensure sufficient accuracy.  The 'best' fit is defined — _for this problem, as in the common convention_ — as that which minimises the sum of the squares of the residuals.  \r\n\r\nEXAMPLE:\r\n\r\n % Input\r\n futureDate = '21-Nov-2023';\r\n data.solutionNumber = [1036949, ..., 1272817];\r\n data.date = ['29-Oct-2016'; ...; '20-Sep-2017'];\r\n data.runtime = [1.2630, ..., 2.0402];\r\n % Output\r\n predictedRuntime = 0.3619;  % seconds\r\n\r\n","description_html":"\u003cp\u003eIt has been \u003ca href = \"https://en.wikipedia.org/wiki/Moore%27s_law#History\"\u003epredicted\u003c/a\u003e that the performance of integrated circuits would \u003ci\u003edouble\u003c/i\u003e every 18 months.  That suggests the time to perform a given computation should \u003ci\u003ehalve\u003c/i\u003e roughly every 18 months.\u003c/p\u003e\u003cp\u003eWhat about on Cody?  Observational data is available from the final test case of \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/963-we-love-vectorized-solutions-problem-1-remove-the-row-average\"\u003eProblem 963\u003c/a\u003e to help us quantify the improvement in performance!  And thereby even make predictions for future computations.\u003c/p\u003e\u003cpre\u003e Solution    Date\t   Runtime [s]\r\n ...\r\n 144393\t     04-Oct-12\t   4.230\r\n ...\r\n 654812\t     17-Apr-15\t   3.099\r\n ...\r\n 1272817     20-Sep-17\t   2.0402\r\n ...\u003c/pre\u003e\u003cp\u003eThe complete data set will be provided to you as input.  You should assume the general trend can be described by the following law:\u003c/p\u003e\u003cpre\u003e runtime = r0 - δ [1 - exp(-t/k)]\r\n runtime = r∞ + δ exp(-t/k)\u003c/pre\u003e\u003cp\u003ewhere \u003ctt\u003er0\u003c/tt\u003e is the runtime at the start of the period in seconds, \u003ctt\u003er∞\u003c/tt\u003e is the predicted runtime (in seconds) that will be approached far in the future, \u003ctt\u003eδ = r0 - r∞\u003c/tt\u003e, and \u003ctt\u003et\u003c/tt\u003e is the time in nominal years since the start of the period, and \u003ctt\u003ek\u003c/tt\u003e is a kinetic parameter (in nominal years).\u003c/p\u003e\u003cp\u003eThe start of the period is defined by the earliest date in the series.  Compute the number of days exactly, and assume that a nominal year comprises 365.24 days.\u003c/p\u003e\u003cp\u003eYour task is to \u003ca href = \"https://au.mathworks.com/help/matlab/math/example-curve-fitting-via-optimization.html\"\u003efit the curve\u003c/a\u003e and thereby predict the runtime for various future dates.  Your output should be rounded to four decimal places.\u003c/p\u003e\u003cp\u003eMETHOD:  You \u003ci\u003emust\u003c/i\u003e use \u003ca href = \"https://au.mathworks.com/help/matlab/ref/fminsearch.html\"\u003e\u003ctt\u003efminsearch\u003c/tt\u003e\u003c/a\u003e to perform the non-linear regression, and you \u003ci\u003emust\u003c/i\u003e set the options using \u003ca href = \"https://au.mathworks.com/help/matlab/ref/optimset.html\"\u003e\u003ctt\u003eoptimset\u003c/tt\u003e\u003c/a\u003e to ensure sufficient accuracy.  The 'best' fit is defined — \u003ci\u003efor this problem, as in the common convention\u003c/i\u003e — as that which minimises the sum of the squares of the residuals.\u003c/p\u003e\u003cp\u003eEXAMPLE:\u003c/p\u003e\u003cpre\u003e % Input\r\n futureDate = '21-Nov-2023';\r\n data.solutionNumber = [1036949, ..., 1272817];\r\n data.date = ['29-Oct-2016'; ...; '20-Sep-2017'];\r\n data.runtime = [1.2630, ..., 2.0402];\r\n % Output\r\n predictedRuntime = 0.3619;  % seconds\u003c/pre\u003e","function_template":"function predictedRuntime = predictor(futureDate, data)\r\n    % Preliminaries:\r\n    ...\r\n    \r\n    \r\n    % Use fminsearch to find the parameters that minimize the objective function.\r\n    myObjectiveFun = ...\r\n    myOptions = ...\r\n    ... = fminsearch\r\n\r\n    % Report your prediction of the runtime at the specified future date.\r\n    predictedRuntime = ...\r\nend\r\n\r\n% Function that accepts a trial set of parameters and the observed data, \r\n% and returns the sum of squared errors for the model . \r\n% https://au.mathworks.com/help/matlab/math/example-curve-fitting-via-optimization.html\r\nfunction SSE = sseVal(...)\r\n    % runtime = r0 - δ [1 - exp(-t/k)]\r\n    % runtime = r∞ + δ exp(-t/k)\r\n    \r\n    ...\r\n    \r\n    SSE = ...\r\nend\r\n","test_suite":"data.solutionNumber = [1036949\t1048171\t1051202\t1078061\t1078699\t1081561\t886761\t1165117\t1202850\t1211446\t1241478\t1241530\t1329044\t1323931\t1382633\t1345204\t1281524\t1272814\t1272815\t1272823\t1272845\t1287012\t1287386\t1262442\t1262445\t1251993\t1252843\t1407137\t1412687\t1448359\t1448368\t1410187\t1465616\t1472214\t1472225\t1490596\t1490602\t1541085\t1541132\t860114\t143879\t144156\t144393\t202249\t210894\t539555\t182974\t276887\t1133109\t654812\t1272817];\r\ndata.date = ['29-Oct-2016';\t'08-Nov-2016';\t'12-Nov-2016';\t'09-Dec-2016';\t'10-Dec-2016';\t'14-Dec-2016';\t'06-May-2016';\t'18-Apr-2017';\t'02-Jun-2017';\t'13-Jun-2017';\t'28-Jul-2017';\t'28-Jul-2017';\t'06-Nov-2017';\t'01-Nov-2017';\t'16-Dec-2017';\t'15-Nov-2017';\t'05-Oct-2017';\t'20-Sep-2017';\t'20-Sep-2017';\t'20-Sep-2017';\t'20-Sep-2017';\t'15-Oct-2017';\t'15-Oct-2017';\t'05-Sep-2017';\t'05-Sep-2017';\t'16-Aug-2017';\t'17-Aug-2017';\t'06-Jan-2018';\t'10-Jan-2018';\t'24-Feb-2018';\t'24-Feb-2018';\t'08-Jan-2018';\t'18-Mar-2018';\t'27-Mar-2018';\t'27-Mar-2018';\t'13-Apr-2018';\t'13-Apr-2018';\t'26-May-2018';\t'26-May-2018';\t'31-Mar-2016';\t'02-Oct-2012';\t'03-Oct-2012';\t'04-Oct-2012';\t'08-Feb-2013';\t'01-Mar-2013';\t'03-Dec-2014';\t'29-Dec-2012';\t'09-Jul-2013';\t'03-Mar-2017';\t'17-Apr-2015';\t'20-Sep-2017'];\r\ndata.runtime = [1.2630\t0.624\t1.3326\t1.2939\t0.5995\t1.1307\t1.3505\t1.2613\t1.2737\t1.3081\t1.2774\t1.252\t1.2179\t1.4431\t1.2637\t1.1614\t1.2542\t1.9135\t1.2996\t1.2595\t1.2664\t1.1858\t1.1501\t1.2516\t1.2786\t1.9461\t1.2492\t1.3654\t1.3263\t1.1484\t1.1728\t1.6177\t1.0538\t1.0571\t1.0454\t1.3873\t1.0600\t1.1154\t1.0635\t1.4359\t4.498\t4.198\t4.230\t5.264\t4.022\t3.153\t4.740\t4.112\t2.3409\t3.099\t2.0402];\r\n\r\n%% Anti-hacking provision\r\n% ADDED 2019-06-29.  \r\n% Ensure only builtin functions will be called.  \r\n! rm -v fileread.m\r\n! rm -v assert.m\r\n\r\n%% Required methodology\r\nassessFunctionPresence({'fminsearch', 'optimset'}, 'FileName','predictor.m', 'Feedback','You must use the specified methodology')\r\n\r\n%% Anti-hardcoding test\r\n% Adapted from a comment at https://www.mathworks.com/matlabcentral/cody/problems/44343 .\r\nassert(~any(cellfun(@(x)ismember(max([0,str2num(x)]),[2012,2019:2023,3000]),regexp(fileread('predictor.m'),'[\\d\\.\\+\\-\\*\\/\\(\\) \\[\\]\\,\\;]+','match'))), 'Please don''t hard-code your ''solution''.') \r\nassert(~any(cellfun(@(x)ismember(max([0,str2num(x)]),[2012,2019:2023,3000]),regexp(fileread('predictor.m'),'\\d+','match'))), 'Please do not hard-code your ''solution''.') \r\n\r\n%% January 2019\r\nfutureDate = '22-Jan-2019';\r\npredictedRuntime_correct = round(0.935043294718475, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '23-Jan-2019';\r\npredictedRuntime_correct = round(0.934448955935569, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '24-Jan-2019';\r\npredictedRuntime_correct = round(0.933855075296061, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% January 2020\r\nfutureDate = '09-Jan-2020';\r\npredictedRuntime_correct = round(0.751755426901932, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '13-Jan-2020';\r\npredictedRuntime_correct = round(0.749945312632780, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% January 2021\r\nfutureDate = '06-Jan-2021';\r\npredictedRuntime_correct = round(0.608254613313523, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '11-Jan-2021';\r\npredictedRuntime_correct = round(0.606545074994730, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% February 2022\r\nfutureDate = '06-Feb-2022';\r\npredictedRuntime_correct = round(0.491354117473879, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '10-Feb-2022';\r\npredictedRuntime_correct = round(0.490345992043568, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% November 2023\r\nfutureDate = '06-Nov-2023';\r\npredictedRuntime_correct = round(0.364155581026111, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '21-Nov-2023';\r\npredictedRuntime_correct = round(0.361853937995679, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '25-Nov-2023';\r\npredictedRuntime_correct = round(0.361244649648014, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '27-Nov-2023';\r\npredictedRuntime_correct = round(0.360940709522436, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% Let's all meet up in the year 3000\r\nfutureDate = '01-Jan-3000';\r\npredictedRuntime_correct = round(0.164021404839, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% Enforce antihacking\r\n% Assure no 'workarounds' that avoid appropriate use of optimset.\r\n% (Added 29 May 2018.)\r\nfutureDate = {'19-Dec-2019';\t'04-Oct-2020';\t'06-Nov-2020';\t'20-Nov-2020';\t'07-Dec-2020';\t'09-Dec-2020';\t'11-Dec-2020';\t'25-Feb-2021';\t'04-Mar-2021';\t'18-Apr-2021';\t'19-May-2021';\t'23-Jul-2021'};\r\npRuntime_c_unrounded = {0.761350673860113\t0.641651747331991\t0.629650497697623\t0.624650604424140\t0.618651428148657\t0.617950799242008\t0.617251250072375\t0.591452258418511\t0.589151203003048\t0.574651625923890\t0.564951737257372\t0.545350739105932};\r\nfor j = 1 : length(futureDate)\r\n    assert(isequal(predictor(futureDate{j}, data), round(pRuntime_c_unrounded{j}, 4)))\r\nend;\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":64439,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":2,"test_suite_updated_at":"2019-06-29T12:58:19.000Z","rescore_all_solutions":true,"group_id":1,"created_at":"2018-05-27T08:32:09.000Z","updated_at":"2019-06-29T12:58:19.000Z","published_at":"2018-05-27T12:35:16.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIt has been\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Moore%27s_law#History\\\"\u003e\u003cw:r\u003e\u003cw:t\u003epredicted\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e that the performance of integrated circuits would\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003edouble\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e every 18 months. That suggests the time to perform a given computation should\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ehalve\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e roughly every 18 months.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWhat about on Cody? Observational data is available from the final test case of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/963-we-love-vectorized-solutions-problem-1-remove-the-row-average\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 963\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e to help us quantify the improvement in performance! And thereby even make predictions for future computations.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Solution    Date     Runtime [s]\\n ...\\n 144393       04-Oct-12     4.230\\n ...\\n 654812       17-Apr-15     3.099\\n ...\\n 1272817     20-Sep-17     2.0402\\n ...]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe complete data set will be provided to you as input. You should assume the general trend can be described by the following law:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ runtime = r0 - δ [1 - exp(-t/k)]\\n runtime = r∞ + δ exp(-t/k)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewhere\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003er0\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is the runtime at the start of the period in seconds,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003er∞\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is the predicted runtime (in seconds) that will be approached far in the future,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eδ = r0 - r∞\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003et\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is the time in nominal years since the start of the period, and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ek\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a kinetic parameter (in nominal years).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe start of the period is defined by the earliest date in the series. Compute the number of days exactly, and assume that a nominal year comprises 365.24 days.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour task is to\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://au.mathworks.com/help/matlab/math/example-curve-fitting-via-optimization.html\\\"\u003e\u003cw:r\u003e\u003cw:t\u003efit the curve\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e and thereby predict the runtime for various future dates. Your output should be rounded to four decimal places.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMETHOD: You\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emust\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e use\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://au.mathworks.com/help/matlab/ref/fminsearch.html\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efminsearch\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e to perform the non-linear regression, and you\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emust\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e set the options using\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://au.mathworks.com/help/matlab/ref/optimset.html\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eoptimset\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e to ensure sufficient accuracy. The 'best' fit is defined —\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efor this problem, as in the common convention\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e — as that which minimises the sum of the squares of the residuals.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ % Input\\n futureDate = '21-Nov-2023';\\n data.solutionNumber = [1036949, ..., 1272817];\\n data.date = ['29-Oct-2016'; ...; '20-Sep-2017'];\\n data.runtime = [1.2630, ..., 2.0402];\\n % Output\\n predictedRuntime = 0.3619;  % seconds]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":44843,"title":"Nth Order Polynomial Regression","description":"Inputs: \r\n\r\n  -two vectors x and y (row or column) \r\n\r\n  -order of polynomial, p\r\n\r\nOutputs: \r\n\r\n  -vector of coefficients [b0 b1 b2 ... bn]\r\n\r\n  -R-squared value\r\n\r\n  -vector of residuals, e\r\n","description_html":"\u003cp\u003eInputs:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e-two vectors x and y (row or column) \r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003e-order of polynomial, p\r\n\u003c/pre\u003e\u003cp\u003eOutputs:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e-vector of coefficients [b0 b1 b2 ... bn]\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003e-R-squared value\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003e-vector of residuals, e\r\n\u003c/pre\u003e","function_template":"function [b,r2,e] = RegressionN(x,y,p)\r\n  b = x;\r\n  e = x;\r\n  r2 = x;\r\nend","test_suite":"%%\r\nx = 0:5;\r\ny = 2*x;\r\np = 1;\r\nb_correct = [0;2];\r\nr2_correct = 1;\r\ne_correct = [0,0,0,0,0,0]';\r\n[b,r2,e] = RegressionN(x,y,p)\r\nassert(nnz(abs(b-b_correct)\u003c1e-12)==p+1)\r\nassert(abs(r2-r2_correct)\u003c1e-12)\r\nassert(nnz(abs(e-e_correct)\u003c1e-12)==length(x))\r\n\r\n%%\r\nx = linspace(2,5,12);\r\ny = exp(x)';\r\np = 3;\r\nb_correct = [-110.9230,126.8980,-46.7825,6.3467]';\r\nr2_correct = 0.9997;\r\ne_correct = [0.8726,-0.6361,-0.8963,-0.4527,0.2214,0.7465,0.8663,0.4864,-0.2751,-1.0207,-0.9863,1.0742]';\r\n[b,r2,e] = RegressionN(x,y,p)\r\nassert(nnz(abs(b-b_correct)\u003c1e-4)==p+1)\r\nassert(abs(r2-r2_correct)\u003c1e-4)\r\nassert(nnz(abs(e-e_correct)\u003c1e-4)==length(x))\r\n\r\n%%\r\nx = linspace(5,7,8);\r\ny = [-0.9238   -1.5011   -0.2963   -0.2268   -0.9897   -0.0372    0.1509    1.5324];\r\np = 3;\r\nb_correct = [-196.1472 101.5622 -17.6070 1.0186]';\r\nr2_correct = 0.8063;\r\ne_correct = [0.2569 -0.6944 0.3739 0.4017 -0.4505 0.2224 -0.2019 0.0919]';\r\n[b,r2,e] = RegressionN(x,y,p)\r\nassert(nnz(abs(b-b_correct)\u003c1e-4)==p+1)\r\nassert(abs(r2-r2_correct)\u003c1e-4)\r\nassert(nnz(abs(e-e_correct)\u003c1e-4)==length(x))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":183622,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2019-01-30T14:42:51.000Z","updated_at":"2026-03-11T08:42:08.000Z","published_at":"2019-01-30T17:17:50.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eInputs:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[-two vectors x and y (row or column) \\n\\n-order of polynomial, p]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOutputs:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[-vector of coefficients [b0 b1 b2 ... bn]\\n\\n-R-squared value\\n\\n-vector of residuals, e]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":44314,"title":"A Simple Tide Gauge with MATLAB","description":"*\u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767* \r\n\r\nYou are standing in a few inches of sea water on a beach.\r\n\r\nYou are wondering whether the high tide is coming soon or it has just passed. \r\n\r\nTherefore, you will write a code in MATLAB to analyze following data. \r\n\r\nYou followed the sequence of water lines left by several swash of waves. \r\n\r\nThe data array A contains the distances the water traveled past your feet during each upward swash of waves. \r\n\r\nYour code will return 1 if the high tide is coming soon. \r\n\r\nYour code will return 0 if the high tide has just passed.    \r\n","description_html":"\u003cp\u003e\u003cb\u003e\u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767 \u0026#8767\u003c/b\u003e\u003c/p\u003e\u003cp\u003eYou are standing in a few inches of sea water on a beach.\u003c/p\u003e\u003cp\u003eYou are wondering whether the high tide is coming soon or it has just passed.\u003c/p\u003e\u003cp\u003eTherefore, you will write a code in MATLAB to analyze following data.\u003c/p\u003e\u003cp\u003eYou followed the sequence of water lines left by several swash of waves.\u003c/p\u003e\u003cp\u003eThe data array A contains the distances the water traveled past your feet during each upward swash of waves.\u003c/p\u003e\u003cp\u003eYour code will return 1 if the high tide is coming soon.\u003c/p\u003e\u003cp\u003eYour code will return 0 if the high tide has just passed.\u003c/p\u003e","function_template":"function tide = gauge(A)\r\n  tide=max(A)-min(A);\r\n  tide=tide*0;\r\nend","test_suite":"%%\r\nA = [5 8 10 12 8 13 14 10 10 15];\r\ntide_correct = 1;\r\nassert(isequal(gauge(A),tide_correct))\r\n%%\r\nA = [15 16 11 9 10 15 7 12 6 11 5 6];\r\ntide_correct = 0;\r\nassert(isequal(gauge(A),tide_correct))\r\n%%\r\nA = [9 15 3 9 5 18 4 17 18 19 8 13 12 21 17 24];\r\ntide_correct = 1;\r\nassert(isequal(gauge(A),tide_correct))\r\n%%\r\nA = [22 12 22 12 9 14 17 16 15 8 13 6 10 7 13 3];\r\ntide_correct = 0;\r\nassert(isequal(gauge(A),tide_correct))\r\n%%\r\n","published":true,"deleted":false,"likes_count":4,"comments_count":3,"created_by":166,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":394,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":34,"created_at":"2017-09-12T00:26:53.000Z","updated_at":"2026-03-25T04:12:58.000Z","published_at":"2017-10-16T01:45:07.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e\u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767 \u0026amp;#8767\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are standing in a few inches of sea water on a beach.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are wondering whether the high tide is coming soon or it has just passed.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eTherefore, you will write a code in MATLAB to analyze following data.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou followed the sequence of water lines left by several swash of waves.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe data array A contains the distances the water traveled past your feet during each upward swash of waves.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour code will return 1 if the high tide is coming soon.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour code will return 0 if the high tide has just passed.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":45851,"title":"Least Absolute Deviations (L1-norm) line fit - degree 1","description":"Matlab's polyfit function is very handy to find least-squares regression. It minimizes the (L2-norm) of the estimation errors, by solving a linear system.\r\n\u003chttps://www.mathworks.com/help/matlab/ref/polyfit.html\u003e\r\n\r\nAn often overlooked way to deal with these situations is to use Least Absolute Deviations (LAD) line fitting. It minimizes the L1-norm of the residuals, thus it is less sensitive to outliers that fall far away from the underlying model\r\nhttps://en.wikipedia.org/wiki/Least_absolute_deviations\r\n\r\n- - -\r\n\r\nYou are given two vectors X and Y (coordinates of observations on a plane).\r\nReturn a row vector *P = [a, b]* with the coefficients of the best-fit line, in the L1-norm sense. I.e., find *a* and *b* that minimize *sum( abs( Y - a*X - b ) )* .\r\n\r\n(compare your results with polyfit on the test suite!)","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 225px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 112.5px; transform-origin: 407px 112.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eMatlab's polyfit function is very handy to find least-squares regression. It minimizes the (L2-norm) of the estimation errors, by solving a linear system.\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/help/matlab/ref/polyfit.html\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e\u0026lt;https://www.mathworks.com/help/matlab/ref/polyfit.html\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e\u0026gt;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 31.5px; text-align: left; transform-origin: 384px 31.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eAn often overlooked way to deal with these situations is to use Least Absolute Deviations (LAD) line fitting. It minimizes the L1-norm of the residuals, thus it is less sensitive to outliers that fall far away from the underlying model\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://en.wikipedia.org/wiki/Least_absolute_deviations\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003ehttps://en.wikipedia.org/wiki/Least_absolute_deviations\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e- - -\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYou are given two vectors X and Y (coordinates of observations on a plane). Return a row vector\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eP = [a, b]\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e with the coefficients of the best-fit line, in the L1-norm sense. I.e., find\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003ea\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e and\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eb\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e that minimize\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003esum( abs( Y - a*X - b ) )\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e .\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e(compare your results with polyfit on the test suite!)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function P = LADfit(X,Y)\r\nP = polyfit(X,Y,1);\r\nend","test_suite":"%%\r\nX=[-2.8 -4.8 0.9 2.3 3.6 -4.2 -0.7 3.0 0.1 -1.0]';\r\nY=[12.0 6.5 -1.6 -2.1 -2.6 0.1 -1.1 -2.4 -1.3 -1.0]';\r\nP=LADfit(X,Y); % P_correct = [-10/27 -19/15]\r\nres=sum(abs(Y-polyval(P,X))); % Optimal residual = 1007/54\r\nassert(res\u003c19)\r\n\r\n%%\r\nX=[1 2 3 4 5]';\r\nY=[1 2 3 4 3]';\r\nP=LADfit(X,Y); % P_correct = [1 0]\r\nres=sum(abs(Y-polyval(P,X))); % Optimal residual = 2.0\r\nassert(res\u003c2.1)\r\n\r\n%%\r\nX=[1 2 3 4 5]';\r\nY=[1 0 0 0 0]';\r\nP=LADfit(X,Y); % P_correct = [0 0]\r\nres=sum(abs(Y-polyval(P,X))); % Optimal residual = 1.0\r\nassert(res\u003c1.01)\r\n\r\n%%\r\nn=10; % Nr of points\r\nno=2; % Nr of outliers\r\nmax_res = 0;\r\nmax_ratio = 0;\r\nfor cycle=1:10\r\n    rng('shuffle');\r\n    P=(rand(2,1)-0.5)*10; % Generate model\r\n    X=(rand(n,1)-0.5)*2; % Generate X\r\n    Y=polyval(P,X)+(rand(n,1)-0.5);  % Generate Y and add noise\r\n    Y(1:no)=Y(1:no)+(rand(no,1)-0.5)*10; % Add outliers\r\n\r\n    P_lad=LADfit(X,Y);\r\n    P_lin=polyfit(X,Y,1);\r\n\r\n    Y_lad=polyval(P_lad,X);\r\n    res_lad=sum(abs(Y-Y_lad));\r\n\r\n    Y_lin=polyval(P_lin,X);\r\n    res_lin=sum(abs(Y-Y_lin));\r\n\r\n    max_res=max(max_res,res_lad);\r\n    max_ratio=max(max_ratio,res_lad/res_lin);\r\nend\r\nassert(max_res\u003c12.0 \u0026 max_ratio\u003c0.99); % Should be always better than L-2 and under 12.0","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":452188,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":6,"test_suite_updated_at":"2020-11-22T01:25:26.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-06-10T07:18:59.000Z","updated_at":"2020-11-22T01:25:26.000Z","published_at":"2020-06-10T07:18:59.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMatlab's polyfit function is very handy to find least-squares regression. It minimizes the (L2-norm) of the estimation errors, by solving a linear system.\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/help/matlab/ref/polyfit.html\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026lt;https://www.mathworks.com/help/matlab/ref/polyfit.html\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026gt;\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAn often overlooked way to deal with these situations is to use Least Absolute Deviations (LAD) line fitting. It minimizes the L1-norm of the residuals, thus it is less sensitive to outliers that fall far away from the underlying model\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Least_absolute_deviations\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ehttps://en.wikipedia.org/wiki/Least_absolute_deviations\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e- - -\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are given two vectors X and Y (coordinates of observations on a plane). Return a row vector\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP = [a, b]\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e with the coefficients of the best-fit line, in the L1-norm sense. I.e., find\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ea\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eb\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that minimize\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003esum( abs( Y - a*X - b ) )\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e .\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(compare your results with polyfit on the test suite!)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":45854,"title":"Least Absolute Deviations (L1-norm) line fit - degree n","description":"This is a generalization of Problem 45851 for degree n\r\n\r\n\u003chttps://www.mathworks.com/matlabcentral/cody/problems/45851-least-absolute-deviations-l1-norm-line-fit-degree-1\u003e\r\n\r\n- - - -\r\n\r\nYou are given two vectors X and Y (coordinates of observations on a plane), and a degree d of the underlying model.\r\nReturn a row vector *P* with the *d+1* coefficients of the best-fit polynomial, in the L1-norm sense. I.e., find *P* of degree *d* that minimizes *sum( abs( Y - polyval(P,X) ) )* .\r\n\r\n(compare your results with polyfit on the test suite!)","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 183px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 91.5px; transform-origin: 407px 91.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 171.5px 8px; transform-origin: 171.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThis is a generalization of Problem 45851 for degree n\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/45851-least-absolute-deviations-l1-norm-line-fit-degree-1\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e\u0026lt;https://www.mathworks.com/matlabcentral/cody/problems/45851-least-absolute-deviations-l1-norm-line-fit-degree-1\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4px 8px; transform-origin: 4px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e\u0026gt;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 16px 8px; transform-origin: 16px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e- - - -\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 31.5px; text-align: left; transform-origin: 384px 31.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 370px 8px; transform-origin: 370px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eYou are given two vectors X and Y (coordinates of observations on a plane), and a degree d of the underlying model. Return a row vector\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4.5px 8px; transform-origin: 4.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eP\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 26px 8px; transform-origin: 26px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e with the\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 12.5px 8px; transform-origin: 12.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003ed+1\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 216.5px 8px; transform-origin: 216.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e coefficients of the best-fit polynomial, in the L1-norm sense. I.e., find\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4.5px 8px; transform-origin: 4.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eP\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 32.5px 8px; transform-origin: 32.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e of degree\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4.5px 8px; transform-origin: 4.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003ed\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e that minimizes\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 96.5px 8px; transform-origin: 96.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003esum( abs( Y - polyval(P,X) ) )\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4px 8px; transform-origin: 4px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e .\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 161px 8px; transform-origin: 161px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e(compare your results with polyfit on the test suite!)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function P = LADfit(X,Y,d)\r\nP = polyfit(X,Y,d);\r\nend","test_suite":"%%\r\nd=2;\r\nX=[-2 -1 0 1 2 3]';\r\nY=[7.14 -1.87 -0.89 -0.75 2.08 7.86]';\r\nP=LADfit(X,Y,d); % P_correct = [1.387 -1.243 -0.894]\r\nres=sum(abs(Y-polyval(P,X))); % Optimal residual = 3.698\r\nassert(res\u003c4.0)\r\n\r\n%%\r\nd=3;\r\nX=[-4 -3 -2 -1 0 1 2 3 4]';\r\nY=[186 408 21 8 2 -4 -21 -57 -121]';\r\nP=LADfit(X,Y,d); % P_correct = [-2.0431 2.1486 -5.6861 -1.8778]\r\nres=sum(abs(Y-polyval(P,X))); % Optimal residual = 341.23\r\nassert(res\u003c350)\r\n\r\n%%\r\nd=3;\r\nn=10; % Nr of points\r\nno=2; % Nr of outliers\r\nmax_res = 0;\r\nmax_ratio = 0;\r\nfor cycle=1:10\r\n    rng('shuffle');\r\n    P=(rand(d,1)-0.5)*10; % Generate model\r\n    X=(rand(n,1)-0.5)*10; % Generate X\r\n    Y=polyval(P,X)+(rand(n,1)-0.5)*2;  % Generate Y and add noise\r\n    Y(1:no)=Y(1:no)+(rand(no,1)-0.5)*50; % Add outliers\r\n    P_lad=LADfit(X,Y,d);\r\n    P_lin=polyfit(X,Y,1);\r\n    Y_lad=polyval(P_lad,X);\r\n    res_lad=sum(abs(Y-Y_lad));\r\n    Y_lin=polyval(P_lin,X);\r\n    res_lin=sum(abs(Y-Y_lin));\r\n    max_res=max(max_res,res_lad);\r\n    max_ratio=max(max_ratio,res_lad/res_lin);\r\nend\r\nassert(max_res\u003c50.0 \u0026 max_ratio\u003c0.98); % Should be always better than L-2 and under 50","published":true,"deleted":false,"likes_count":1,"comments_count":7,"created_by":452188,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":2,"test_suite_updated_at":"2021-04-19T10:10:15.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-06-10T08:00:01.000Z","updated_at":"2021-04-19T10:10:15.000Z","published_at":"2020-06-10T08:00:01.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is a generalization of Problem 45851 for degree n\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/45851-least-absolute-deviations-l1-norm-line-fit-degree-1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026lt;https://www.mathworks.com/matlabcentral/cody/problems/45851-least-absolute-deviations-l1-norm-line-fit-degree-1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026gt;\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e- - - -\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are given two vectors X and Y (coordinates of observations on a plane), and a degree d of the underlying model. Return a row vector\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e with the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ed+1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e coefficients of the best-fit polynomial, in the L1-norm sense. I.e., find\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of degree\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ed\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that minimizes\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003esum( abs( Y - polyval(P,X) ) )\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e .\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(compare your results with polyfit on the test suite!)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":44655,"title":"Computational power of Cody servers","description":"It has been \u003chttps://en.wikipedia.org/wiki/Moore%27s_law#History predicted\u003e that the performance of integrated circuits would _double_ every 18 months.  That suggests the time to perform a given computation should _halve_ roughly every 18 months.  \r\n\r\nWhat about on Cody?  Observational data is available from the final test case of \u003chttps://www.mathworks.com/matlabcentral/cody/problems/963-we-love-vectorized-solutions-problem-1-remove-the-row-average Problem 963\u003e to help us quantify the improvement in performance!  And thereby even make predictions for future computations.\r\n\r\n Solution    Date\t   Runtime [s]\r\n ...\r\n 144393\t     04-Oct-12\t   4.230\r\n ...\r\n 654812\t     17-Apr-15\t   3.099\r\n ...\r\n 1272817     20-Sep-17\t   2.0402\r\n ...\r\n\r\nThe complete data set will be provided to you as input.  You should assume the general trend can be described by the following law:\r\n\r\n runtime = r0 - δ [1 - exp(-t/k)]\r\n runtime = r∞ + δ exp(-t/k)\r\n\r\nwhere |r0| is the runtime at the start of the period in seconds, |r∞| is the predicted runtime (in seconds) that will be approached far in the future, |δ = r0 - r∞|, and |t| is the time in nominal years since the start of the period, and |k| is a kinetic parameter (in nominal years).  \r\n\r\nThe start of the period is defined by the earliest date in the series.  Compute the number of days exactly, and assume that a nominal year comprises 365.24 days.\r\n\r\nYour task is to \u003chttps://au.mathworks.com/help/matlab/math/example-curve-fitting-via-optimization.html fit the curve\u003e and thereby predict the runtime for various future dates.  Your output should be rounded to four decimal places.  \r\n\r\nMETHOD:  You _must_ use \u003chttps://au.mathworks.com/help/matlab/ref/fminsearch.html |fminsearch|\u003e to perform the non-linear regression, and you _must_ set the options using \u003chttps://au.mathworks.com/help/matlab/ref/optimset.html |optimset|\u003e to ensure sufficient accuracy.  The 'best' fit is defined — _for this problem, as in the common convention_ — as that which minimises the sum of the squares of the residuals.  \r\n\r\nEXAMPLE:\r\n\r\n % Input\r\n futureDate = '21-Nov-2023';\r\n data.solutionNumber = [1036949, ..., 1272817];\r\n data.date = ['29-Oct-2016'; ...; '20-Sep-2017'];\r\n data.runtime = [1.2630, ..., 2.0402];\r\n % Output\r\n predictedRuntime = 0.3619;  % seconds\r\n\r\n","description_html":"\u003cp\u003eIt has been \u003ca href = \"https://en.wikipedia.org/wiki/Moore%27s_law#History\"\u003epredicted\u003c/a\u003e that the performance of integrated circuits would \u003ci\u003edouble\u003c/i\u003e every 18 months.  That suggests the time to perform a given computation should \u003ci\u003ehalve\u003c/i\u003e roughly every 18 months.\u003c/p\u003e\u003cp\u003eWhat about on Cody?  Observational data is available from the final test case of \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/963-we-love-vectorized-solutions-problem-1-remove-the-row-average\"\u003eProblem 963\u003c/a\u003e to help us quantify the improvement in performance!  And thereby even make predictions for future computations.\u003c/p\u003e\u003cpre\u003e Solution    Date\t   Runtime [s]\r\n ...\r\n 144393\t     04-Oct-12\t   4.230\r\n ...\r\n 654812\t     17-Apr-15\t   3.099\r\n ...\r\n 1272817     20-Sep-17\t   2.0402\r\n ...\u003c/pre\u003e\u003cp\u003eThe complete data set will be provided to you as input.  You should assume the general trend can be described by the following law:\u003c/p\u003e\u003cpre\u003e runtime = r0 - δ [1 - exp(-t/k)]\r\n runtime = r∞ + δ exp(-t/k)\u003c/pre\u003e\u003cp\u003ewhere \u003ctt\u003er0\u003c/tt\u003e is the runtime at the start of the period in seconds, \u003ctt\u003er∞\u003c/tt\u003e is the predicted runtime (in seconds) that will be approached far in the future, \u003ctt\u003eδ = r0 - r∞\u003c/tt\u003e, and \u003ctt\u003et\u003c/tt\u003e is the time in nominal years since the start of the period, and \u003ctt\u003ek\u003c/tt\u003e is a kinetic parameter (in nominal years).\u003c/p\u003e\u003cp\u003eThe start of the period is defined by the earliest date in the series.  Compute the number of days exactly, and assume that a nominal year comprises 365.24 days.\u003c/p\u003e\u003cp\u003eYour task is to \u003ca href = \"https://au.mathworks.com/help/matlab/math/example-curve-fitting-via-optimization.html\"\u003efit the curve\u003c/a\u003e and thereby predict the runtime for various future dates.  Your output should be rounded to four decimal places.\u003c/p\u003e\u003cp\u003eMETHOD:  You \u003ci\u003emust\u003c/i\u003e use \u003ca href = \"https://au.mathworks.com/help/matlab/ref/fminsearch.html\"\u003e\u003ctt\u003efminsearch\u003c/tt\u003e\u003c/a\u003e to perform the non-linear regression, and you \u003ci\u003emust\u003c/i\u003e set the options using \u003ca href = \"https://au.mathworks.com/help/matlab/ref/optimset.html\"\u003e\u003ctt\u003eoptimset\u003c/tt\u003e\u003c/a\u003e to ensure sufficient accuracy.  The 'best' fit is defined — \u003ci\u003efor this problem, as in the common convention\u003c/i\u003e — as that which minimises the sum of the squares of the residuals.\u003c/p\u003e\u003cp\u003eEXAMPLE:\u003c/p\u003e\u003cpre\u003e % Input\r\n futureDate = '21-Nov-2023';\r\n data.solutionNumber = [1036949, ..., 1272817];\r\n data.date = ['29-Oct-2016'; ...; '20-Sep-2017'];\r\n data.runtime = [1.2630, ..., 2.0402];\r\n % Output\r\n predictedRuntime = 0.3619;  % seconds\u003c/pre\u003e","function_template":"function predictedRuntime = predictor(futureDate, data)\r\n    % Preliminaries:\r\n    ...\r\n    \r\n    \r\n    % Use fminsearch to find the parameters that minimize the objective function.\r\n    myObjectiveFun = ...\r\n    myOptions = ...\r\n    ... = fminsearch\r\n\r\n    % Report your prediction of the runtime at the specified future date.\r\n    predictedRuntime = ...\r\nend\r\n\r\n% Function that accepts a trial set of parameters and the observed data, \r\n% and returns the sum of squared errors for the model . \r\n% https://au.mathworks.com/help/matlab/math/example-curve-fitting-via-optimization.html\r\nfunction SSE = sseVal(...)\r\n    % runtime = r0 - δ [1 - exp(-t/k)]\r\n    % runtime = r∞ + δ exp(-t/k)\r\n    \r\n    ...\r\n    \r\n    SSE = ...\r\nend\r\n","test_suite":"data.solutionNumber = [1036949\t1048171\t1051202\t1078061\t1078699\t1081561\t886761\t1165117\t1202850\t1211446\t1241478\t1241530\t1329044\t1323931\t1382633\t1345204\t1281524\t1272814\t1272815\t1272823\t1272845\t1287012\t1287386\t1262442\t1262445\t1251993\t1252843\t1407137\t1412687\t1448359\t1448368\t1410187\t1465616\t1472214\t1472225\t1490596\t1490602\t1541085\t1541132\t860114\t143879\t144156\t144393\t202249\t210894\t539555\t182974\t276887\t1133109\t654812\t1272817];\r\ndata.date = ['29-Oct-2016';\t'08-Nov-2016';\t'12-Nov-2016';\t'09-Dec-2016';\t'10-Dec-2016';\t'14-Dec-2016';\t'06-May-2016';\t'18-Apr-2017';\t'02-Jun-2017';\t'13-Jun-2017';\t'28-Jul-2017';\t'28-Jul-2017';\t'06-Nov-2017';\t'01-Nov-2017';\t'16-Dec-2017';\t'15-Nov-2017';\t'05-Oct-2017';\t'20-Sep-2017';\t'20-Sep-2017';\t'20-Sep-2017';\t'20-Sep-2017';\t'15-Oct-2017';\t'15-Oct-2017';\t'05-Sep-2017';\t'05-Sep-2017';\t'16-Aug-2017';\t'17-Aug-2017';\t'06-Jan-2018';\t'10-Jan-2018';\t'24-Feb-2018';\t'24-Feb-2018';\t'08-Jan-2018';\t'18-Mar-2018';\t'27-Mar-2018';\t'27-Mar-2018';\t'13-Apr-2018';\t'13-Apr-2018';\t'26-May-2018';\t'26-May-2018';\t'31-Mar-2016';\t'02-Oct-2012';\t'03-Oct-2012';\t'04-Oct-2012';\t'08-Feb-2013';\t'01-Mar-2013';\t'03-Dec-2014';\t'29-Dec-2012';\t'09-Jul-2013';\t'03-Mar-2017';\t'17-Apr-2015';\t'20-Sep-2017'];\r\ndata.runtime = [1.2630\t0.624\t1.3326\t1.2939\t0.5995\t1.1307\t1.3505\t1.2613\t1.2737\t1.3081\t1.2774\t1.252\t1.2179\t1.4431\t1.2637\t1.1614\t1.2542\t1.9135\t1.2996\t1.2595\t1.2664\t1.1858\t1.1501\t1.2516\t1.2786\t1.9461\t1.2492\t1.3654\t1.3263\t1.1484\t1.1728\t1.6177\t1.0538\t1.0571\t1.0454\t1.3873\t1.0600\t1.1154\t1.0635\t1.4359\t4.498\t4.198\t4.230\t5.264\t4.022\t3.153\t4.740\t4.112\t2.3409\t3.099\t2.0402];\r\n\r\n%% Anti-hacking provision\r\n% ADDED 2019-06-29.  \r\n% Ensure only builtin functions will be called.  \r\n! rm -v fileread.m\r\n! rm -v assert.m\r\n\r\n%% Required methodology\r\nassessFunctionPresence({'fminsearch', 'optimset'}, 'FileName','predictor.m', 'Feedback','You must use the specified methodology')\r\n\r\n%% Anti-hardcoding test\r\n% Adapted from a comment at https://www.mathworks.com/matlabcentral/cody/problems/44343 .\r\nassert(~any(cellfun(@(x)ismember(max([0,str2num(x)]),[2012,2019:2023,3000]),regexp(fileread('predictor.m'),'[\\d\\.\\+\\-\\*\\/\\(\\) \\[\\]\\,\\;]+','match'))), 'Please don''t hard-code your ''solution''.') \r\nassert(~any(cellfun(@(x)ismember(max([0,str2num(x)]),[2012,2019:2023,3000]),regexp(fileread('predictor.m'),'\\d+','match'))), 'Please do not hard-code your ''solution''.') \r\n\r\n%% January 2019\r\nfutureDate = '22-Jan-2019';\r\npredictedRuntime_correct = round(0.935043294718475, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '23-Jan-2019';\r\npredictedRuntime_correct = round(0.934448955935569, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '24-Jan-2019';\r\npredictedRuntime_correct = round(0.933855075296061, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% January 2020\r\nfutureDate = '09-Jan-2020';\r\npredictedRuntime_correct = round(0.751755426901932, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '13-Jan-2020';\r\npredictedRuntime_correct = round(0.749945312632780, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% January 2021\r\nfutureDate = '06-Jan-2021';\r\npredictedRuntime_correct = round(0.608254613313523, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '11-Jan-2021';\r\npredictedRuntime_correct = round(0.606545074994730, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% February 2022\r\nfutureDate = '06-Feb-2022';\r\npredictedRuntime_correct = round(0.491354117473879, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '10-Feb-2022';\r\npredictedRuntime_correct = round(0.490345992043568, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% November 2023\r\nfutureDate = '06-Nov-2023';\r\npredictedRuntime_correct = round(0.364155581026111, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '21-Nov-2023';\r\npredictedRuntime_correct = round(0.361853937995679, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '25-Nov-2023';\r\npredictedRuntime_correct = round(0.361244649648014, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\nfutureDate = '27-Nov-2023';\r\npredictedRuntime_correct = round(0.360940709522436, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% Let's all meet up in the year 3000\r\nfutureDate = '01-Jan-3000';\r\npredictedRuntime_correct = round(0.164021404839, 4);\r\nassert(isequal(predictor(futureDate, data), predictedRuntime_correct))\r\n\r\n%% Enforce antihacking\r\n% Assure no 'workarounds' that avoid appropriate use of optimset.\r\n% (Added 29 May 2018.)\r\nfutureDate = {'19-Dec-2019';\t'04-Oct-2020';\t'06-Nov-2020';\t'20-Nov-2020';\t'07-Dec-2020';\t'09-Dec-2020';\t'11-Dec-2020';\t'25-Feb-2021';\t'04-Mar-2021';\t'18-Apr-2021';\t'19-May-2021';\t'23-Jul-2021'};\r\npRuntime_c_unrounded = {0.761350673860113\t0.641651747331991\t0.629650497697623\t0.624650604424140\t0.618651428148657\t0.617950799242008\t0.617251250072375\t0.591452258418511\t0.589151203003048\t0.574651625923890\t0.564951737257372\t0.545350739105932};\r\nfor j = 1 : length(futureDate)\r\n    assert(isequal(predictor(futureDate{j}, data), round(pRuntime_c_unrounded{j}, 4)))\r\nend;\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":64439,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":2,"test_suite_updated_at":"2019-06-29T12:58:19.000Z","rescore_all_solutions":true,"group_id":1,"created_at":"2018-05-27T08:32:09.000Z","updated_at":"2019-06-29T12:58:19.000Z","published_at":"2018-05-27T12:35:16.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIt has been\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Moore%27s_law#History\\\"\u003e\u003cw:r\u003e\u003cw:t\u003epredicted\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e that the performance of integrated circuits would\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003edouble\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e every 18 months. That suggests the time to perform a given computation should\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ehalve\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e roughly every 18 months.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWhat about on Cody? Observational data is available from the final test case of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/963-we-love-vectorized-solutions-problem-1-remove-the-row-average\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 963\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e to help us quantify the improvement in performance! And thereby even make predictions for future computations.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Solution    Date     Runtime [s]\\n ...\\n 144393       04-Oct-12     4.230\\n ...\\n 654812       17-Apr-15     3.099\\n ...\\n 1272817     20-Sep-17     2.0402\\n ...]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe complete data set will be provided to you as input. You should assume the general trend can be described by the following law:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ runtime = r0 - δ [1 - exp(-t/k)]\\n runtime = r∞ + δ exp(-t/k)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewhere\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003er0\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is the runtime at the start of the period in seconds,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003er∞\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is the predicted runtime (in seconds) that will be approached far in the future,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eδ = r0 - r∞\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003et\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is the time in nominal years since the start of the period, and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ek\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a kinetic parameter (in nominal years).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe start of the period is defined by the earliest date in the series. Compute the number of days exactly, and assume that a nominal year comprises 365.24 days.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour task is to\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://au.mathworks.com/help/matlab/math/example-curve-fitting-via-optimization.html\\\"\u003e\u003cw:r\u003e\u003cw:t\u003efit the curve\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e and thereby predict the runtime for various future dates. Your output should be rounded to four decimal places.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMETHOD: You\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emust\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e use\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://au.mathworks.com/help/matlab/ref/fminsearch.html\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efminsearch\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e to perform the non-linear regression, and you\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emust\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e set the options using\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://au.mathworks.com/help/matlab/ref/optimset.html\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eoptimset\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e to ensure sufficient accuracy. The 'best' fit is defined —\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efor this problem, as in the common convention\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e — as that which minimises the sum of the squares of the residuals.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ % Input\\n futureDate = '21-Nov-2023';\\n data.solutionNumber = [1036949, ..., 1272817];\\n data.date = ['29-Oct-2016'; ...; '20-Sep-2017'];\\n data.runtime = [1.2630, ..., 2.0402];\\n % Output\\n predictedRuntime = 0.3619;  % seconds]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"tag:\"regression\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"regression\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"regression\"","","\"","regression","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f77260d6408\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f77260d6368\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f77260d58c8\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f77260d6728\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f77260d6688\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f77260d65e8\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f77260d64a8\u003e":"tag:\"regression\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f77260d64a8\u003e":"tag:\"regression\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"search","password":"J3bGPZzQ7asjJcCk","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"regression\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"regression\"","","\"","regression","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f77260d6408\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f77260d6368\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f77260d58c8\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f77260d6728\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f77260d6688\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f77260d65e8\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f77260d64a8\u003e":"tag:\"regression\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f77260d64a8\u003e":"tag:\"regression\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":44843,"difficulty_rating":"easy-medium"},{"id":44314,"difficulty_rating":"easy-medium"},{"id":45851,"difficulty_rating":"easy-medium"},{"id":45854,"difficulty_rating":"medium"},{"id":44655,"difficulty_rating":"unrated"}]}}