{"group":{"group":{"id":676,"name":"Numerical Methods","lockable":false,"created_at":"2020-01-09T13:37:12.000Z","updated_at":"2025-12-14T01:33:56.000Z","description":"The utility of MATLAB knows no bounds.","is_default":false,"created_by":26769,"badge_id":62,"featured":false,"trending":false,"solution_count_in_trending_period":33,"trending_last_calculated":"2025-12-14T00:00:00.000Z","image_id":585,"published":true,"community_created":true,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"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\u003eThe utility of MATLAB knows no bounds.\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\"}]}","description_html":"\u003cdiv style = \"text-align: start; line-height: normal; 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=\"display: block; min-width: 0px; padding-top: 0px; perspective-origin: 289.5px 10.5px; transform-origin: 289.5px 10.5px; \"\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; perspective-origin: 266.5px 10.5px; transform-origin: 266.5px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThe utility of MATLAB knows no bounds.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","published_at":"2020-01-16T15:33:47.000Z"},"current_player":null},"problems":[{"id":179,"title":"Monte-Carlo integration","description":"Write a function that estimates a d-dimensional integral to at least 1% relative precision.\r\n\r\nInputs:\r\n\r\n* d: positive integer. The dimension of the integral.\r\n* fun: function handle. The function accepts a row-vector of length d as an argument and returns a real scalar as a result.\r\n\r\nOutput:\r\n\r\n* I: is the integral over fun from 0 to 1 in each direction.\r\n\r\n     1     1        1            \r\n     /     /        /           \r\n I = |dx_1 |dx_2 ...| dx_d  fun([x_1,x_2,...,x_d])\r\n     /     /        /            \r\n     0     0        0            \r\n\r\nExample:\r\n\r\n  fun = @(x) x(1)*x(2)\r\n  d = 2\r\n\r\nThe result should be 0.25. An output I=0.2501 would be acceptable, because the relative deviation would be abs(0.25-0.2501)/0.25 which is smaller than 1%.\r\n\r\nThe functions in the test-suite are all positive and generally 'well behaved', i.e. not fluctuating too much. Some of the tests hav a relatively large d.","description_html":"\u003cp\u003eWrite a function that estimates a d-dimensional integral to at least 1% relative precision.\u003c/p\u003e\u003cp\u003eInputs:\u003c/p\u003e\u003cul\u003e\u003cli\u003ed: positive integer. The dimension of the integral.\u003c/li\u003e\u003cli\u003efun: function handle. The function accepts a row-vector of length d as an argument and returns a real scalar as a result.\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eOutput:\u003c/p\u003e\u003cul\u003e\u003cli\u003eI: is the integral over fun from 0 to 1 in each direction.\u003c/li\u003e\u003c/ul\u003e\u003cpre\u003e     1     1        1            \r\n     /     /        /           \r\n I = |dx_1 |dx_2 ...| dx_d  fun([x_1,x_2,...,x_d])\r\n     /     /        /            \r\n     0     0        0            \u003c/pre\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003efun = @(x) x(1)*x(2)\r\nd = 2\r\n\u003c/pre\u003e\u003cp\u003eThe result should be 0.25. An output I=0.2501 would be acceptable, because the relative deviation would be abs(0.25-0.2501)/0.25 which is smaller than 1%.\u003c/p\u003e\u003cp\u003eThe functions in the test-suite are all positive and generally 'well behaved', i.e. not fluctuating too much. Some of the tests hav a relatively large d.\u003c/p\u003e","function_template":"function I = dquad(d,fun)\r\n  I=fun(0.5*ones(1,d));\r\nend","test_suite":"%% 1d integral: integrate x^2 from 0 to 1\r\nfun = @(x) x^2;\r\nassert(abs((dquad(1,fun) - 1/3)*3)\u003c0.01)\r\n\r\n%% 2d integral from the example\r\nfun = @(x) x(1)*x(2);\r\nassert(abs((dquad(2,fun) - 0.25)*4)\u003c0.01)\r\n\r\n%% constant in most dimensions\r\nfun = @(x) 1+sin(x(1));\r\nassert(abs((dquad(50,fun) -  1.45969769)/1.45969769)\u003c0.01)\r\n\r\n%% volume of d-dimensional 2^d box with a spherical hole, d between 5 and 10\r\nd = randi([5 10],1)\r\nr = rand*0.8\r\nfun = @(x) 2^d*(norm(x)\u003er);\r\ndball = exp(d/2*log(pi)+d*log(r)-gammaln(d/2+1));\r\nassert(abs((dquad(d,fun) - 2^d+dball)/(2^d-dball))\u003c0.01)\r\n","published":true,"deleted":false,"likes_count":7,"comments_count":8,"created_by":203,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":153,"test_suite_updated_at":"2012-01-31T21:50:54.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-01-30T15:04:16.000Z","updated_at":"2026-04-03T20:58:12.000Z","published_at":"2012-02-01T19:02:46.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\u003eWrite a function that estimates a d-dimensional integral to at least 1% relative precision.\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\u003eInputs:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ed: positive integer. The dimension of the integral.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003efun: function handle. The function accepts a row-vector of length d as an argument and returns a real scalar as a result.\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\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eI: is the integral over fun from 0 to 1 in each direction.\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[     1     1        1            \\n     /     /        /           \\n I = |dx_1 |dx_2 ...| dx_d  fun([x_1,x_2,...,x_d])\\n     /     /        /            \\n     0     0        0]]\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\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[fun = @(x) x(1)*x(2)\\nd = 2]]\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 result should be 0.25. An output I=0.2501 would be acceptable, because the relative deviation would be abs(0.25-0.2501)/0.25 which is smaller than 1%.\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 functions in the test-suite are all positive and generally 'well behaved', i.e. not fluctuating too much. Some of the tests hav a relatively large d.\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":231,"title":"Differential equations I","description":"Given a function handle |f| an initial condition |y0| and a final time |tf|, solve numerically the differential equation\r\n\r\n  dy/dt = f(y)\r\n\r\nfor the function |y(t)| between |t=0| and |t=tf|. Give as a result |res=y(tf)|.\r\n\r\nExample:\r\n\r\n   f = @(x) -x;\r\n   tf= 1;\r\n   y0= 1;\r\n\r\n =\u003e y(tf) = 1/e = 0.367879441171442\r\n\r\nRemarks: aim at a relative precision of around 1e-6. The function is analytic in the interval [0,1].","description_html":"\u003cp\u003eGiven a function handle \u003ctt\u003ef\u003c/tt\u003e an initial condition \u003ctt\u003ey0\u003c/tt\u003e and a final time \u003ctt\u003etf\u003c/tt\u003e, solve numerically the differential equation\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003edy/dt = f(y)\r\n\u003c/pre\u003e\u003cp\u003efor the function \u003ctt\u003ey(t)\u003c/tt\u003e between \u003ctt\u003et=0\u003c/tt\u003e and \u003ctt\u003et=tf\u003c/tt\u003e. Give as a result \u003ctt\u003eres=y(tf)\u003c/tt\u003e.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre\u003e   f = @(x) -x;\r\n   tf= 1;\r\n   y0= 1;\u003c/pre\u003e\u003cpre\u003e =\u003e y(tf) = 1/e = 0.367879441171442\u003c/pre\u003e\u003cp\u003eRemarks: aim at a relative precision of around 1e-6. The function is analytic in the interval [0,1].\u003c/p\u003e","function_template":"function res = deqnsolve(f,y0,tf)\r\n  res = 0;\r\nend","test_suite":"%% \r\nf = @(x) -x;\r\ntf =1;\r\ny0 =1;\r\nassert(abs(deqnsolve(f,y0,tf)-exp(-1)) \u003c 1e-5)\r\n\r\n%% \r\nf = @sin;\r\ntf =1;\r\ny0 =1/2;\r\nassert(abs(deqnsolve(f,y0,tf)-2*acot(exp(-1)*cot(1/4))) \u003c 1e-5)\r\n\r\n%% \r\nf = @(x) 1/(x+1);\r\ntf =6;\r\ny0 =1;\r\nassert(abs(deqnsolve(f,y0,tf)-3) \u003c 1e-5)\r\n\r\n%% a randomized one\r\na = rand*0.9;\r\nf = @(x) x-a*x^2;\r\ntf = rand+1.5;\r\ny0=1;\r\nassert(abs(deqnsolve(f,y0,tf)-exp(tf)/(1-a+a*exp(tf))) \u003c 1e-5)","published":true,"deleted":false,"likes_count":3,"comments_count":5,"created_by":203,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":190,"test_suite_updated_at":"2012-02-02T15:10:17.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-02-02T14:57:14.000Z","updated_at":"2026-03-13T19:43:52.000Z","published_at":"2012-02-02T15:20:18.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\u003eGiven a function handle\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e an initial condition\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\u003ey0\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and a final time\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\u003etf\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, solve numerically the differential equation\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[dy/dt = f(y)]]\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\u003efor the function\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\u003ey(t)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e between\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=0\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=tf\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Give as a result\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\u003eres=y(tf)\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\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[   f = @(x) -x;\\n   tf= 1;\\n   y0= 1;\\n\\n =\u003e y(tf) = 1/e = 0.367879441171442]]\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\u003eRemarks: aim at a relative precision of around 1e-6. The function is analytic in the interval [0,1].\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":484,"title":"Steepest Descent Method","description":"Write a function to find the values of a design variable vector, _x_, that minimizes an unconstrained scalar objective function, _f_, given a function handle to _f_ and its gradient, a starting guess, _x0_, a gradient tolerance, _TolGrad_, and a maximum number of iterations, _MaxIter_, using the Steepest Descent Method.","description_html":"\u003cp\u003eWrite a function to find the values of a design variable vector, \u003ci\u003ex\u003c/i\u003e, that minimizes an unconstrained scalar objective function, \u003ci\u003ef\u003c/i\u003e, given a function handle to \u003ci\u003ef\u003c/i\u003e and its gradient, a starting guess, \u003ci\u003ex0\u003c/i\u003e, a gradient tolerance, \u003ci\u003eTolGrad\u003c/i\u003e, and a maximum number of iterations, \u003ci\u003eMaxIter\u003c/i\u003e, using the Steepest Descent Method.\u003c/p\u003e","function_template":"function [xmin,fmin]=SteepestDescent(F,gradF,x0,TolGrad,MaxIter)\r\n%\r\n%  Input\r\n%  F........ function handle for objective function F(x) with input argument, x\r\n%  gradF... function handle for gradient of objective function with input argument, x\r\n%  x0....... design variable vector initial guess for starting point\r\n%  TolGrad.. Tolerance for norm of objective gradient to be zero\r\n%  MaxIter.. Maximum number of iterations\r\n%\r\n%  Output\r\n%  x........ final design variable vector found to minimize objective function, F(x)\r\n%  f1....... final objective function minimum value\r\n\r\nif nargin\u003c4 || isempty(TolGrad), TolGrad=1e-2; end\r\nif nargin\u003c5 || isempty(MaxIter), MaxIter=20; end\r\n\r\n%Initialize loop parameters\r\niter = 0;\r\nf0   = F(x0);\r\nc    = gradF(x0);\r\nConverged = norm(c) \u003c TolGrad;\r\n\r\n%% Search direction and line search iterations\r\nwhile iter\u003cMaxIter \u0026\u0026 ~Converged\r\n   iter = iter + 1;\r\n   xmin = 0;\r\n   fmin = 0;\r\nend\r\nend","test_suite":"%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nx0 = [-1.9; 2.0];\r\nx1=[\r\n   -1.4478\r\n    2.1184];\r\nx2=[\r\n    1.7064\r\n    2.9446];\r\nf1=6.0419;\r\nf2=0.6068;\r\n[xmin,fmin]=SteepestDescent(F,gradF,x0,0.01,1)\r\nassert(norm(xmin-x1)\u003c0.2||norm(xmin-x2)\u003c0.2)\r\nassert( abs(fmin-f1)\u003c0.5|| abs(fmin-f2)\u003c0.5) % 2 local min\r\n\r\n%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nx0 = [0; 0];\r\nxcorrect=[1;1];\r\nfcorrect=0;\r\n[xmin,fmin]=SteepestDescent(F,gradF,x0) % 20 iterations default\r\nassert(norm((xmin-xcorrect),inf)\u003c1)\r\nassert(abs(fmin-fcorrect)\u003c0.8);\r\n\r\n%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nx0 = [1.1; 0.9];\r\nxcorrect=[1;1];\r\nfcorrect=0;\r\n[xmin,fmin]=SteepestDescent(F,gradF,x0,1e-2,2000)\r\nassert(isequal(round(xmin),xcorrect))\r\nassert(isequal(round(fmin),fcorrect))","published":true,"deleted":false,"likes_count":2,"comments_count":5,"created_by":279,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":32,"test_suite_updated_at":"2012-03-17T00:51:37.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-03-12T07:04:30.000Z","updated_at":"2025-12-10T14:07:35.000Z","published_at":"2012-03-17T01:01:14.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\u003eWrite a function to find the values of a design variable 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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ex\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, that minimizes an unconstrained scalar objective function,\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, given a function handle to\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and its gradient, a starting guess,\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\u003ex0\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, a gradient tolerance,\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\u003eTolGrad\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, and a maximum number of iterations,\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\u003eMaxIter\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, using the Steepest Descent Method.\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":485,"title":"Fletcher-Reeves Conjugate Gradient Method","description":"Write a function to find the values of a design variable vector, _x_, that minimizes an unconstrained scalar objective function, _f_, given a function handle to _f_ and its gradient, a starting guess, _x0_, a gradient tolerance, _TolGrad_, and a maximum number of iterations, _MaxIter_, using Fletcher-Reeves Conjugate Gradient Method.","description_html":"\u003cp\u003eWrite a function to find the values of a design variable vector, \u003ci\u003ex\u003c/i\u003e, that minimizes an unconstrained scalar objective function, \u003ci\u003ef\u003c/i\u003e, given a function handle to \u003ci\u003ef\u003c/i\u003e and its gradient, a starting guess, \u003ci\u003ex0\u003c/i\u003e, a gradient tolerance, \u003ci\u003eTolGrad\u003c/i\u003e, and a maximum number of iterations, \u003ci\u003eMaxIter\u003c/i\u003e, using Fletcher-Reeves Conjugate Gradient Method.\u003c/p\u003e","function_template":"function [x,f1]=ConjGrad(F,gradF,x0,TolGrad,MaxIter)\r\n%\r\n%  Input\r\n%  F........ function handle for objective function F(x) with input argument, x\r\n%  gradF... function handle for gradient of objective function with input argument, x\r\n%  x0....... design variable vector initial guess for starting point\r\n%  TolGrad.. Tolerance for norm of objective gradient to be zero\r\n%  MaxIter.. Maximum number of iterations\r\n%\r\n%  Output\r\n%  x........ final design variable vector found to minimize objective function, F(x)\r\n%  f1....... final objective function minimum value\r\n\r\nif nargin\u003c4 || isempty(TolGrad), TolGrad=1e-2; end\r\nif nargin\u003c5 || isempty(MaxIter), MaxIter=20; end\r\n\r\n%% Change following steepest descent algorithm to Conjugate Gradient Method\r\n\r\n% Initialize loop parameters\r\niter = 0;\r\nf0   = F(x0);\r\nc    = gradF(x0);\r\nConverged = norm(c) \u003c TolGrad;\r\ndisp('   iter alpha f(alpha)  norm(c)')\r\nfprintf(' %6.0f %5.3f %8.4f %8.4f\\n',[iter, 0, f0, norm(c)])\r\nalpha = 1e-6*norm(c);\r\n\r\n%% Search direction and line search iterations\r\nwhile iter\u003cMaxIter \u0026\u0026 ~Converged\r\n\titer = iter + 1;\r\n   d = -c;\r\n   f = @(alpha) F(x0+alpha*d);\r\n   alphaUpper = bracket( f, 0, 0.1*alpha );\r\n   [alpha,f1] = fminbnd( f, 0, alphaUpper );\r\n   x = x0 + alpha*d;\r\n   c = gradF(x);\r\n   Converged = (norm(c) \u003c TolGrad);\r\n   x0 = x;\r\nend\r\nfprintf(' %6.0f %5.3f %8.4f %8.4f\\n',[iter, alpha, f1, norm(c)])\r\nend\r\n\r\n%% Bracket interval for 1-D line search\r\n   function [alphaUpper,alphaLower,nfunc] = bracket( f, alpha0, delta, MaxIter )\r\n\t% usage: [alphaUpper,alphaLower,nfunc] = bracket( f, alpha0, delta, MaxIter )\r\n\t%  Golden section search to bracket unimodal, univariate minimum\r\n\t%--input\r\n\t%  f = function handle to univariate function of alpha\r\n\t%  alpha0... Starting point lower bound on bracket\r\n\t%  delta.... Guess for upper bound on bracket on unimodal min\r\n\t%  MaxIter.. Maximum number of iterations\r\n\t%--output\r\n\t%  alphaUpper... Upper bound on alpha to bracket min of f(alpha)\r\n\t%  alphaLower... Lower bound on alpha to bracket min of f(alpha)\r\n\t%  nfunc........ Number of function evaluations\r\n\r\n      if nargin\u003c3, delta = 1.e-3; end\r\n      if nargin\u003c4, MaxIter=1e3; end\r\n      \r\n      %--Local variables\r\n      %  expand... Expansion factor for extending Upper Bound\r\n      %  phi...... Golden section ratio = 1.681...\r\n      phi=(1+sqrt(5))/2;\r\n      expand = phi; % Set=1 to use equal interval search\r\n      \r\n      % Initialize variables\r\n      alphaLower=alpha0;\r\n      alphaLast=alpha0;\r\n      alphaNext=delta;\r\n      fLast=f(alphaLast);\r\n      fNext=f(alphaNext);\r\n      iter=1;\r\n      while fNext\u003cfLast \u0026\u0026 iter\u003c=MaxIter\r\n         iter = iter + 1;\r\n         delta = expand*delta;\r\n         alphaLower = alphaLast;\r\n         alphaLast = alphaNext;\r\n         alphaNext = alphaNext + delta;\r\n         fLast = fNext;\r\n         fNext = f(alphaNext);\r\n      end\r\n      alphaUpper=alphaNext;\r\n      nfunc=iter+1;\r\n   end","test_suite":"%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nx0 = [-1.9; 2.0];\r\nx1=[\r\n   -1.4478\r\n    2.1184];\r\nx2=[\r\n    1.7064\r\n    2.9446];\r\nf1=6.0419;\r\nf2=0.6068;\r\n[xmin,fmin]=ConjGrad(F,gradF,x0,0.01,1) % single steepest descent\r\nassert(norm(xmin-x1)\u003c0.2||norm(xmin-x2)\u003c0.2)\r\nassert( abs(fmin-f1)\u003c0.5|| abs(fmin-f2)\u003c0.5) % 2 local min\r\n\r\n%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nx0 = [0; 0];\r\nxcorrect=[\r\n    0.2926\r\n    0.0505];\r\nfcorrect=0.6238;\r\n[xmin,fmin]=ConjGrad(F,gradF,x0,1e-2,2) % two iterations\r\nassert(norm(xmin-xcorrect)\u003c0.1)\r\nassert( abs(fmin-fcorrect)\u003c0.01)\r\n\r\n%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nx0 = [1.1;0.9];\r\nxcorrect = [1;1];\r\nfcorrect = 0;\r\n[xmin,fmin]=ConjGrad(F,gradF,x0) % default 20 iterations\r\nassert(norm(xmin-xcorrect)\u003c0.1)\r\nassert(abs(fmin-fcorrect)\u003c0.01);\r\n\r\n%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nx0 = [0; 0];\r\nxcorrect = [1;1];\r\nfcorrect = 0;\r\n[xmin,fmin]=ConjGrad(F,gradF,x0,0.01,100) % Convergence before 100 iterations\r\nassert(norm(xmin-xcorrect)\u003c0.1)\r\nassert(abs(fmin-fcorrect)\u003c0.01);\r\n\r\n%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nx0 = [-1.9; 2];\r\nxcorrect = [1;1];\r\nfcorrect = 0;\r\n[xmin,fmin]=ConjGrad(F,gradF,x0,1e-3,200)\r\nassert(isequal(round(xmin),xcorrect))\r\nassert(isequal(round(fmin),fcorrect))","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":279,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":31,"test_suite_updated_at":"2012-03-19T22:08:55.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-03-12T08:38:19.000Z","updated_at":"2025-12-10T14:17:15.000Z","published_at":"2012-03-19T22:15: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\u003eWrite a function to find the values of a design variable 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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ex\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, that minimizes an unconstrained scalar objective function,\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, given a function handle to\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and its gradient, a starting guess,\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\u003ex0\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, a gradient tolerance,\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\u003eTolGrad\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, and a maximum number of iterations,\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\u003eMaxIter\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, using Fletcher-Reeves Conjugate Gradient Method.\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":493,"title":"Quasi-Newton Method for Unconstrained Minimization using BFGS Update","description":"Write a function to find the values of a design variable vector, _x_, that minimizes an unconstrained scalar objective function, _f_, given a function handle to _f_ and its gradient, a starting guess, _x0_, a gradient tolerance, _TolGrad_, and a maximum number of iterations, _MaxIter_, using the Quasi-Newton (Secant) Method. Initialize the Hessian approximation as an identity matrix. Update the Hessian matrix approximation using the BFGS update formula.","description_html":"\u003cp\u003eWrite a function to find the values of a design variable vector, \u003ci\u003ex\u003c/i\u003e, that minimizes an unconstrained scalar objective function, \u003ci\u003ef\u003c/i\u003e, given a function handle to \u003ci\u003ef\u003c/i\u003e and its gradient, a starting guess, \u003ci\u003ex0\u003c/i\u003e, a gradient tolerance, \u003ci\u003eTolGrad\u003c/i\u003e, and a maximum number of iterations, \u003ci\u003eMaxIter\u003c/i\u003e, using the Quasi-Newton (Secant) Method. Initialize the Hessian approximation as an identity matrix. Update the Hessian matrix approximation using the BFGS update formula.\u003c/p\u003e","function_template":"function [x,f1]=BFGS_Quasi_Newton(F,gradF,x0,TolGrad,MaxIter)\r\n%\r\n%  Input\r\n%  F........ function handle for objective function F(x) with input argument, x\r\n%  gradF... function handle for gradient of objective function with input argument, x\r\n%  x0....... design variable vector initial guess for starting point\r\n%  TolGrad.. Tolerance for norm of objective gradient to be zero\r\n%  MaxIter.. Maximum number of iterations\r\n%\r\n%  Output\r\n%  x........ final design variable vector found to minimize objective function, F(x)\r\n%  f1....... final objective function minimum value\r\n\r\nif nargin\u003c4 || isempty(TolGrad), TolGrad=1e-3; end\r\nif nargin\u003c5 || isempty(MaxIter), MaxIter=100;  end\r\n\r\n% Change following steepest descent algorithm to Quasi-Newton Method \r\n% using BFGS Hessian updates.\r\n\r\n%% Initialize loop parameters\r\niter = 0;\r\nf0 = F(x0);\r\nc0 = gradF(x0);\r\nc  = c0;\r\nConverged = norm(c) \u003c TolGrad;\r\n\r\n%% Search direction and line search iterations\r\ndisp('iter alpha f(alpha)  norm(c)')\r\nfprintf('%4.0f %6.6f %8.4f %8.4f\\n',[iter, 0, f0, norm(c)])\r\nwhile iter\u003cMaxIter \u0026\u0026 ~Converged\r\n\titer = iter + 1;\r\n\td = -c;\r\n\tf = @(alpha) F(x0+alpha*d);\r\n\t[alpha,f1] = fminsearch( f, 0 );\r\n\tx = x0 + alpha*d;\r\n\r\n   Converged = norm(c) \u003c TolGrad;\r\n   x0 = x;\r\n   c0 = c;\r\nend\r\nfprintf('%4.0f %6.4f %8.4f %8.4f\\n',[iter, alpha, f1, norm(c)])\r\nend","test_suite":"%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nx0 = [-1.9; 2.0];\r\nx1=[\r\n   -1.4478\r\n    2.1184];\r\nx2=[\r\n    1.7064\r\n    2.9446];\r\nf1=6.0419;\r\nf2=0.6068;\r\n[xmin,fmin]=BFGS_Quasi_Newton(F,gradF,x0,0.01,1) % single steepest descent\r\nassert(norm(xmin-x1)\u003c0.2||norm(xmin-x2)\u003c0.2)\r\nassert( abs(fmin-f1)\u003c0.5|| abs(fmin-f2)\u003c0.5) % 2 local min\r\n\r\n%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nx0 = [0; 0];\r\nxcorrect=[\r\n    0.2927\r\n    0.0506];\r\nfcorrect=0.63;\r\n[xmin,fmin]=BFGS_Quasi_Newton(F,gradF,x0,1e-2,2) % two iterations\r\nassert(norm(xmin-xcorrect)\u003c0.1)\r\nassert( abs(fmin-fcorrect)\u003c0.01)\r\n\r\n%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nx0 = [0;0];\r\nxcorrect = [1;1;];\r\nfcorrect = 0;\r\n[xmin,fmin]=BFGS_Quasi_Newton(F,gradF,x0)\r\nassert(norm(xmin-xcorrect)\u003c0.01)\r\nassert(abs(fmin-fcorrect)\u003c0.01);\r\n\r\n%%\r\n% Rosenbrock's banana function\r\nF=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;\r\ngradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];\r\nHessF=@(x) 200*[4*x(1).^2-2*(x(2)-x(1).^2)+1/100, -2*x(1);\r\n                -2*x(1), 1];\r\nxcorrect = [1;1];\r\nfcorrect = 0;\r\nx0 = [-1.9; 2];\r\n[xmin,fmin]=BFGS_Quasi_Newton(F,gradF,x0,1e-4)\r\nassert(isequal(round(xmin),xcorrect))\r\nassert(isequal(round(fmin),fcorrect))","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":279,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":22,"test_suite_updated_at":"2012-03-20T03:17:25.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-03-13T17:04:07.000Z","updated_at":"2025-12-10T14:21:49.000Z","published_at":"2012-03-20T03:21:19.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\u003eWrite a function to find the values of a design variable 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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ex\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, that minimizes an unconstrained scalar objective function,\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, given a function handle to\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and its gradient, a starting guess,\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\u003ex0\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, a gradient tolerance,\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\u003eTolGrad\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, and a maximum number of iterations,\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\u003eMaxIter\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, using the Quasi-Newton (Secant) Method. Initialize the Hessian approximation as an identity matrix. Update the Hessian matrix approximation using the BFGS update formula.\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":523,"title":"Sequential Unconstrained Minimization (SUMT) using Exterior Penalty","description":"Write a function to find the values of a design variable vector, _x_, that minimizes a scalar objective function, _f_, given a function handle to _f_, a starting guess, _x0_, subject to inequality and equality constraints with function handles _g_\u003c=0 and _h_=0, respectively. Use a quadratic exterior penalty for the sequential unconstrained minimization technique (SUMT) with an optional input vector of penalty parameter values that become increasingly larger.","description_html":"\u003cp\u003eWrite a function to find the values of a design variable vector, \u003ci\u003ex\u003c/i\u003e, that minimizes a scalar objective function, \u003ci\u003ef\u003c/i\u003e, given a function handle to \u003ci\u003ef\u003c/i\u003e, a starting guess, \u003ci\u003ex0\u003c/i\u003e, subject to inequality and equality constraints with function handles \u003ci\u003eg\u003c/i\u003e\u0026lt;=0 and \u003ci\u003eh\u003c/i\u003e=0, respectively. Use a quadratic exterior penalty for the sequential unconstrained minimization technique (SUMT) with an optional input vector of penalty parameter values that become increasingly larger.\u003c/p\u003e","function_template":"function [x,fmin]=sumt_exterior(f,g,h,x0,penalty_parameter)\r\nif isempty(g), g=@(x)[]; end\r\nif isempty(h), h=@(x)[]; end\r\nif nargin\u003c5 || isempty(penalty_parameter)\r\n   penalty_parameter=? % initialize penalty parameter values for SUMT loop\r\nend\r\n% You may use fminsearch for the unconstrained minimization","test_suite":"%% Haftka \u0026 Gurdal, Figure 5.7.1 example\r\nf = @(x) 0.5*x;\r\ng = @(x) 2-x;\r\nx0 = 0;\r\n[xmin,fmin]=sumt_exterior(f,g,[],x0) %#ok\u003c*NOPTS\u003e\r\nxcorrect=2;\r\nassert(norm(xmin-xcorrect)\u003c1e-3)\r\nassert(abs(fmin-f(xcorrect))\u003c1e-3)\r\n\r\n%%\r\nf = @(x) 0.5*x;\r\ng = @(x) 2-x;\r\nx0 = 0;\r\n[xmin,fmin]=sumt_exterior(f,g,[],x0,1) % 1 iteration for unit penalty value\r\nxr1=1.75;\r\nassert(norm(xmin-xr1)\u003c1e-4)\r\nassert(abs(fmin-f(xr1))\u003c1e-4)\r\n\r\n%% Haftka \u0026 Gurdal, Example 5.7.1\r\nf = @(x) x(1).^2 + 10*x(2).^2;\r\nh = @(x) sum(x)-4;\r\nx0 = [0; 0];\r\n[xmin,fmin]=sumt_exterior(f,[],h,x0)\r\nxcorrect=[40; 4]/11;\r\nassert(norm(xmin-xcorrect)\u003c1e-3)\r\nassert(abs(fmin-f(xcorrect))\u003c1e-4)\r\n\r\n%%\r\nf = @(x) x(1).^2 + 10*x(2).^2;\r\nh = @(x) sum(x)-4;\r\nx0 = [0; 0];\r\nr  = [1, 5];\r\n[xmin,fmin]=sumt_exterior(f,[],h,x0,r) % 2 iterations\r\nxr2=[3.0769\r\n     0.3077];\r\nassert(norm(xmin-xr2,inf)\u003c1e-4)\r\nassert(abs(fmin-f(xr2))\u003c1e-4)\r\n\r\n%% Vanderplaats, Figure 5-4 example\r\nf = @(x) sum(x);\r\ng = @(x) [x(1) - 2*x(2) - 2\r\n          8 - 6*x(1) + x(1).^2 - x(2)];\r\nx0 = [0; 0];\r\n[xmin,fmin]=sumt_exterior(f,g,[],x0)\r\nxcorrect=[2; 0];\r\nassert(norm(xmin-xcorrect)\u003c1e-4)\r\nassert(abs(fmin-f(xcorrect))\u003c1e-4)\r\n\r\n%%\r\nf = @(x) sum(x);\r\ng = @(x) [x(1) - 2*x(2) - 2\r\n          8 - 6*x(1) + x(1).^2 - x(2)];\r\nx0 = [5; 5];\r\nr  = [1, 2];\r\n[xmin,fmin]=sumt_exterior(f,g,[],x0,r) % 2 iterations\r\nxr2=[1.9536\r\n    -0.0496];\r\nassert(norm(xmin-xr2)\u003c1e-4)\r\nassert(abs(fmin-f(xr2))\u003c1e-4)","published":true,"deleted":false,"likes_count":1,"comments_count":3,"created_by":279,"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":"2012-03-24T03:18:31.000Z","updated_at":"2025-12-10T14:27:22.000Z","published_at":"2012-03-24T03:18:36.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\u003eWrite a function to find the values of a design variable 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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ex\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, that minimizes a scalar objective function,\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, given a function handle to\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, a starting guess,\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\u003ex0\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, subject to inequality and equality constraints with function handles\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\u003eg\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026lt;=0 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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eh\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e=0, respectively. Use a quadratic exterior penalty for the sequential unconstrained minimization technique (SUMT) with an optional input vector of penalty parameter values that become increasingly larger.\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":524,"title":"Sequential Unconstrained Minimization (SUMT) using Interior Penalty","description":"Write a function to find the values of a design variable vector, _x_, that minimizes a scalar objective function, _f_ ( _x_ ), given a function handle to _f_, and a starting guess, _x0_, subject to inequality constraints _g_ ( _x_ )\u003c=0 with function handle _g_. Use a logarithmic interior penalty for the sequential unconstrained minimization technique (SUMT) with an optional input vector of increasing penalty parameter values. That is, the penalty (barrier) function, _P_, is\r\n\r\n P(x,r) = -sum(log(-g(x)))/r\r\n\r\nwhere _r_ is the penalty parameter.","description_html":"\u003cp\u003eWrite a function to find the values of a design variable vector, \u003ci\u003ex\u003c/i\u003e, that minimizes a scalar objective function, \u003ci\u003ef\u003c/i\u003e ( \u003ci\u003ex\u003c/i\u003e ), given a function handle to \u003ci\u003ef\u003c/i\u003e, and a starting guess, \u003ci\u003ex0\u003c/i\u003e, subject to inequality constraints \u003ci\u003eg\u003c/i\u003e ( \u003ci\u003ex\u003c/i\u003e )\u0026lt;=0 with function handle \u003ci\u003eg\u003c/i\u003e. Use a logarithmic interior penalty for the sequential unconstrained minimization technique (SUMT) with an optional input vector of increasing penalty parameter values. That is, the penalty (barrier) function, \u003ci\u003eP\u003c/i\u003e, is\u003c/p\u003e\u003cpre\u003e P(x,r) = -sum(log(-g(x)))/r\u003c/pre\u003e\u003cp\u003ewhere \u003ci\u003er\u003c/i\u003e is the penalty parameter.\u003c/p\u003e","function_template":"function [x,fmin]=sumt_interior(f,g,x0,penalty_parameter)\r\nif nargin\u003c4 || isempty(penalty_parameter)\r\n   penalty_parameter=? % initialize penalty parameter values for SUMT loop\r\nend\r\n% You may find that fminsearch is not accurate enough for the unconstrained minimization","test_suite":"%% Haftka \u0026 Gurdal, Figure 5.7.1 example\r\nf = @(x) 0.5*x;\r\ng = @(x) 2-x;\r\nx0 = 5;\r\n[xmin,fmin]=sumt_interior(f,g,x0) %#ok\u003c*NOPTS\u003e\r\nxcorrect=2;\r\nassert(norm(xmin-xcorrect)\u003c2e-3)\r\nassert(abs(fmin-f(xcorrect))\u003c1e-3)\r\n\r\n%%\r\nf = @(x) 0.5*x;\r\ng = @(x) 2-x;\r\nx0 = 5;\r\n[xmin,fmin]=sumt_interior(f,g,x0,1) % 1 iteration for unit penalty value\r\nxr1=4;\r\nassert(norm(xmin-xr1)\u003c1e-4)\r\nassert(abs(fmin-f(xr1))\u003c1e-4)\r\n\r\n%% Vanderplaats, Figure 5-4 example\r\nf = @(x) sum(x);\r\ng = @(x) [x(1) - 2*x(2) - 2\r\n          8 - 6*x(1) + x(1).^2 - x(2)];\r\nx0 = [3; 3];\r\n[xmin,fmin]=sumt_interior(f,g,x0,1) % 1 iteration\r\nxr2=[1.8686\r\n     2.1221];\r\nassert(norm(xmin-xr2)\u003c1e-2)\r\nassert(abs(fmin-f(xr2))\u003c1e-2)\r\n\r\n%%\r\nf = @(x) sum(x);\r\ng = @(x) [x(1) - 2*x(2) - 2\r\n          8 - 6*x(1) + x(1).^2 - x(2)];\r\nx0 = [2.1; 0.1];\r\nr  = 2^12;\r\n[xmin,fmin]=sumt_interior(f,g,x0,r) % Final iteration\r\nxcorrect=[2; 0];\r\nassert(norm(xmin-xcorrect)\u003c5e-3)\r\nassert(abs(fmin-f(xcorrect))\u003c2e-3)","published":true,"deleted":false,"likes_count":1,"comments_count":1,"created_by":279,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":10,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-03-24T03:23:26.000Z","updated_at":"2025-12-10T16:18:46.000Z","published_at":"2012-03-24T18:41:49.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\u003eWrite a function to find the values of a design variable 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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ex\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, that minimizes a scalar objective function,\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\u003ef\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: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\u003ex\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e ), given a function handle to\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\u003ef\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, and a starting guess,\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\u003ex0\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, subject to inequality constraints\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\u003eg\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: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\u003ex\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e )\u0026lt;=0 with function handle\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\u003eg\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Use a logarithmic interior penalty for the sequential unconstrained minimization technique (SUMT) with an optional input vector of increasing penalty parameter values. That is, the penalty (barrier) function,\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\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, is\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[ P(x,r) = -sum(log(-g(x)))/r]]\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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003er\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is the penalty parameter.\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":1031,"title":"Composite Trapezoidal Rule for Numeric Integration","description":"Use the trapezoidal rule to numerically integrate a function, _f(x)_, passed as the first argument, between upper and lower limits of integration, _x=a_ and _x=b_,passed as the second and third arguments, respectively. Use _n_ equal intervals, the optional fourth argument, for composite trapezoidal rule.","description_html":"\u003cp\u003eUse the trapezoidal rule to numerically integrate a function, \u003ci\u003ef(x)\u003c/i\u003e, passed as the first argument, between upper and lower limits of integration, \u003ci\u003ex=a\u003c/i\u003e and \u003ci\u003ex=b\u003c/i\u003e,passed as the second and third arguments, respectively. Use \u003ci\u003en\u003c/i\u003e equal intervals, the optional fourth argument, for composite trapezoidal rule.\u003c/p\u003e","function_template":"function I = trapezoidal_rule(f,a,b,n)\r\n% trap: composite trapezoidal rule quadrature\r\n%   I = trap(f,a,b,n):\r\n%       composite trapezoidal rule\r\n% input:\r\n%   f = name of vectorized function to be integrated\r\n%   a, b = integration limits\r\n%   n = number of segments (default = 100)\r\n% output:\r\n%   I = integral estimate\r\n\r\nend","test_suite":"%% Simple Trapezoidal Rule\r\np=[2 0 -4 0 -1 1];\r\na=-2;\r\nb=4;\r\nf = @(x) polyval(p,x);\r\nassert(isequal(trapezoidal_rule(f,a,b,1),5280))\r\n\r\n%% Composite Trapezoidal Rule for 2 intervals\r\np=[2 0 -4 0 -1 1];\r\na=-2;\r\nb=4;\r\nf = @(x) polyval(p,x);\r\nassert(isequal(trapezoidal_rule(f,a,b,2),2634))\r\n\r\n%% Composite Trapezoidal Rule for 4 intervals\r\np=[2 0 -4 0 -1 1];\r\na=-2;\r\nb=4;\r\nf = @(x) polyval(p,x);\r\nassert(isequal(trapezoidal_rule(f,a,b,4),1516.875))\r\n\r\n%% Exact analytical comparison\r\np=[2 0 -4 0 -1 1];\r\na=-2;\r\nb=4;\r\nf = @(x) polyval(p,x);\r\nP=polyint(p);\r\nI_correct=polyval(P,b)-polyval(P,a);\r\nI=trapezoidal_rule(f,a,b);\r\nassert(abs(I-I_correct)\u003c1)\r\n\r\n%% Exact analytical comparison--higher tolerance\r\np=[2 0 -4 0 -1 1];\r\na=-2;\r\nb=4;\r\nf = @(x) polyval(p,x);\r\nI = trapezoidal_rule(f,a,b,1000);\r\nP=polyint(p);\r\nI_correct=polyval(P,b)-polyval(P,a);\r\nassert(abs(trapezoidal_rule(f,a,b,1000)-I_correct)\u003c1e-1)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":279,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":113,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-11-13T19:14:48.000Z","updated_at":"2025-11-20T22:28:17.000Z","published_at":"2012-11-13T20:00:02.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\u003eUse the trapezoidal rule to numerically integrate a function,\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\u003ef(x)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, passed as the first argument, between upper and lower limits of integration,\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\u003ex=a\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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ex=b\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e,passed as the second and third arguments, respectively. Use\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\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e equal intervals, the optional fourth argument, for composite trapezoidal rule.\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":1197,"title":"Numerical Integration","description":"Input\r\n\r\n* |x0|, a real number greater than 0\r\n\r\nOutput\r\n\r\n* |I|, a numerical estimate of the integral\r\n\r\n      x0\r\n      /\r\n  I = |  cosh(x) / sqrt(cosh(x0) - cosh(x)) dx\r\n      /\r\n      0\r\n\r\nExample:\r\n\r\n   x0=1.0  --\u003e  I = 2.6405789412796\r\n\r\n\r\nRemarks:\r\n\r\n* Aim at a relative precision better than 1e-10\r\n* The problem arises studying the frictionless movement of a mass point on a hanging wire, which follows the curve cosh(x).","description_html":"\u003cp\u003eInput\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003ctt\u003ex0\u003c/tt\u003e, a real number greater than 0\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eOutput\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003ctt\u003eI\u003c/tt\u003e, a numerical estimate of the integral\u003c/li\u003e\u003c/ul\u003e\u003cpre\u003e      x0\r\n      /\r\n  I = |  cosh(x) / sqrt(cosh(x0) - cosh(x)) dx\r\n      /\r\n      0\u003c/pre\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre\u003e   x0=1.0  --\u003e  I = 2.6405789412796\u003c/pre\u003e\u003cp\u003eRemarks:\u003c/p\u003e\u003cul\u003e\u003cli\u003eAim at a relative precision better than 1e-10\u003c/li\u003e\u003cli\u003eThe problem arises studying the frictionless movement of a mass point on a hanging wire, which follows the curve cosh(x).\u003c/li\u003e\u003c/ul\u003e","function_template":"function I = coshint(x0)\r\n  I = x0;\r\nend","test_suite":"%%\r\nx0 = 1;\r\nI_correct = 2.6405789412796;\r\nfprintf('Relative difference to reference solution: %e\\n',norm(coshint(x0)-I_correct)/I_correct)\r\nassert(norm(coshint(x0)-I_correct)/I_correct \u003c= 1e-10)\r\n\r\n%%\r\nx0 = 2;\r\nI_correct = 3.9464053536380;\r\nfprintf('Relative difference to reference solution: %e\\n',norm(coshint(x0)-I_correct)/I_correct)\r\nassert(norm(coshint(x0)-I_correct)/I_correct \u003c= 1e-10)\r\n\r\n%%\r\nx0 = 13;\r\nI_correct = 9.4065231838369e+02;\r\nfprintf('Relative difference to reference solution: %e\\n',norm(coshint(x0)-I_correct)/I_correct)\r\nassert(norm(coshint(x0)-I_correct)/I_correct \u003c= 1e-10)\r\n\r\n%% randomized test for small values of x0 where \r\n % cosh(x) ~ 1 + x^2/2 + ...\r\n % and up to x0=1e-5 Integrating (analytically) the approximation is\r\n % accurate enough\r\nfor l=1:5\r\n   x0 = 1e-6 * (1+rand);\r\n   I_correct = pi*(4+x0^2)/4/sqrt(2);\r\n   fprintf('Relative difference to reference solution: %e\\n',norm(coshint(x0)-I_correct)/I_correct)\r\n   assert(norm(coshint(x0)-I_correct)/I_correct \u003c= 1e-10)\r\nend","published":true,"deleted":false,"likes_count":1,"comments_count":9,"created_by":203,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":106,"test_suite_updated_at":"2013-01-11T15:08:44.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-11T14:11:17.000Z","updated_at":"2026-03-13T22:59:58.000Z","published_at":"2013-01-11T15:08:44.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\u003eInput\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ex0\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, a real number greater than 0\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\u003eOutput\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eI\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, a numerical estimate of the integral\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[      x0\\n      /\\n  I = |  cosh(x) / sqrt(cosh(x0) - cosh(x)) dx\\n      /\\n      0]]\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\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[   x0=1.0  --\u003e  I = 2.6405789412796]]\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\u003eRemarks:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAim at a relative precision better than 1e-10\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe problem arises studying the frictionless movement of a mass point on a hanging wire, which follows the curve cosh(x).\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":42891,"title":"Differential equation and events","description":"You have to solve the non-linear time variant differential equation:\r\n\r\n  (cos(y)+2)*y''+atan(y)*sin(0.01*t)^2+y*sin(0.5*t)=0\r\n\r\nwith initial condition as input of the function\r\n\r\n  [y(0);y'(0)]=[a,b]\r\n\r\nand return the time 'te' when 'y' crosses zero from the negatives values to the positive values.\r\n\r\n  te=zero_crossing(a,b)\r\n\r\n\r\ntip: use the 'Events' option in odeset to detect the crossing, stop integration and get the time 'te'. See how to use it here :\r\nhttp://www.mathworks.com/help/matlab/math/ode-event-location.html?searchHighlight=ballode\r\n\r\nAdditional info:\r\n\r\n* in the test case, the solver ode45 is used, with options 'RelTol'=1e-8 and 'AbsTol'=1e-10.\r\n* because you don't know in advance the final time of the crossing, you can solve again further in time if no crossing is detected, starting from the point of the final time of previous integration.","description_html":"\u003cp\u003eYou have to solve the non-linear time variant differential equation:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e(cos(y)+2)*y''+atan(y)*sin(0.01*t)^2+y*sin(0.5*t)=0\r\n\u003c/pre\u003e\u003cp\u003ewith initial condition as input of the function\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e[y(0);y'(0)]=[a,b]\r\n\u003c/pre\u003e\u003cp\u003eand return the time 'te' when 'y' crosses zero from the negatives values to the positive values.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ete=zero_crossing(a,b)\r\n\u003c/pre\u003e\u003cp\u003etip: use the 'Events' option in odeset to detect the crossing, stop integration and get the time 'te'. See how to use it here : \u003ca href = \"http://www.mathworks.com/help/matlab/math/ode-event-location.html?searchHighlight=ballode\"\u003ehttp://www.mathworks.com/help/matlab/math/ode-event-location.html?searchHighlight=ballode\u003c/a\u003e\u003c/p\u003e\u003cp\u003eAdditional info:\u003c/p\u003e\u003cul\u003e\u003cli\u003ein the test case, the solver ode45 is used, with options 'RelTol'=1e-8 and 'AbsTol'=1e-10.\u003c/li\u003e\u003cli\u003ebecause you don't know in advance the final time of the crossing, you can solve again further in time if no crossing is detected, starting from the point of the final time of previous integration.\u003c/li\u003e\u003c/ul\u003e","function_template":"function te=zero_crossing(a,b)\r\nte=[];\r\nend","test_suite":"%%\r\na = 1; b = 1;\r\nte = 16.3214;\r\nassert(abs(zero_crossing(a,b)-te)\u003c0.01)\r\n\r\n%%\r\na = 10; b = 10;\r\nte = 16.3070;\r\nassert(abs(zero_crossing(a,b)-te)\u003c0.01)\r\n\r\n%%\r\na = 0.1; b = 100;\r\nte = 16.3298;\r\nassert(abs(zero_crossing(a,b)-te)\u003c0.01)\r\n\r\n%%\r\na = -1;b = -0.5;\r\nte = 4.1668;\r\nassert(abs(zero_crossing(a,b)-te)\u003c0.01)\r\n\r\n%%\r\na = -0.01;b = -15;\r\nte = 4.8781;\r\nassert(abs(zero_crossing(a,b)-te)\u003c0.01)\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":2,"created_by":61291,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":24,"test_suite_updated_at":"2016-06-14T02:45:21.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2016-06-14T02:05:14.000Z","updated_at":"2025-12-19T12:18:25.000Z","published_at":"2016-06-14T02:45:21.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\u003eYou have to solve the non-linear time variant differential equation:\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[(cos(y)+2)*y''+atan(y)*sin(0.01*t)^2+y*sin(0.5*t)=0]]\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\u003ewith initial condition as input of the function\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[[y(0);y'(0)]=[a,b]]]\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\u003eand return the time 'te' when 'y' crosses zero from the negatives values to the positive values.\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[te=zero_crossing(a,b)]]\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\u003etip: use the 'Events' option in odeset to detect the crossing, stop integration and get the time 'te'. See how to use it here :\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=\\\"http://www.mathworks.com/help/matlab/math/ode-event-location.html?searchHighlight=ballode\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ehttp://www.mathworks.com/help/matlab/math/ode-event-location.html?searchHighlight=ballode\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAdditional info:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ein the test case, the solver ode45 is used, with options 'RelTol'=1e-8 and 'AbsTol'=1e-10.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ebecause you don't know in advance the final time of the crossing, you can solve again further in time if no crossing is detected, starting from the point of the final time of previous integration.\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":43003,"title":"Simpsons's rule (but not Homer Simpson)","description":"I wonder what Homer Simpson would have thought of Simpson's rule? Somehow I doubt his thoughts would have included the phrase Newton-Cotes, or even numerical integration.\r\n\r\nIn this problem, I want you to use \u003chttps://en.wikipedia.org/wiki/Simpson%27s_rule Simpson's rule\u003e to integrate a function, provided as a list of points in a vector. The points must be equally spaced in x, so all that need be provided is the spacing between points in x. As well, I'll be nice and always give you an odd number of points, since that is important for Simpson's rule to work smoothly.\r\n\r\nAs a test case, we should know that the integral of cos(x) over the interval [0,pi/2] is 1, at least analytically that is true. Your Simpson's rule code, for only 7 points spanning that interval will do pretty well:\r\n\r\n  deltax = pi/12;\r\n  Fx = cos(linspace(0,pi/2,7));\r\n  simpsInt(Fx,deltax)\r\n  ans =\r\n            1.00002631217059\r\n\r\nThus, Fx is a vector of supplied function values, and deltax is the step size taken on the x-axis.\r\n\r\nHomer would be proud.","description_html":"\u003cp\u003eI wonder what Homer Simpson would have thought of Simpson's rule? Somehow I doubt his thoughts would have included the phrase Newton-Cotes, or even numerical integration.\u003c/p\u003e\u003cp\u003eIn this problem, I want you to use \u003ca href = \"https://en.wikipedia.org/wiki/Simpson%27s_rule\"\u003eSimpson's rule\u003c/a\u003e to integrate a function, provided as a list of points in a vector. The points must be equally spaced in x, so all that need be provided is the spacing between points in x. As well, I'll be nice and always give you an odd number of points, since that is important for Simpson's rule to work smoothly.\u003c/p\u003e\u003cp\u003eAs a test case, we should know that the integral of cos(x) over the interval [0,pi/2] is 1, at least analytically that is true. Your Simpson's rule code, for only 7 points spanning that interval will do pretty well:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003edeltax = pi/12;\r\nFx = cos(linspace(0,pi/2,7));\r\nsimpsInt(Fx,deltax)\r\nans =\r\n          1.00002631217059\r\n\u003c/pre\u003e\u003cp\u003eThus, Fx is a vector of supplied function values, and deltax is the step size taken on the x-axis.\u003c/p\u003e\u003cp\u003eHomer would be proud.\u003c/p\u003e","function_template":"function y = simpsInt(Fx,deltax)\r\n  % simple Simpson's rule integration.\r\n  y = Fx;\r\nend\r\n","test_suite":"%%\r\ndeltax = pi/12;\r\nFx = cos(linspace(0,pi/2,7));\r\ny_correct = 1.00002631217059;\r\ntol = 1e-14;\r\nassert(abs(simpsInt(Fx,deltax) - y_correct) \u003c tol)\r\n\r\n%%\r\ndeltax = 0.125;\r\nFx = exp((0:8)/8);\r\ny_correct = 1.7182841546999;\r\ntol = 1e-14;\r\nassert(abs(simpsInt(Fx,deltax) - y_correct) \u003c tol)\r\n\r\n%%\r\ndeltax = 1;\r\nFx = (0:10).^2;\r\ny_correct = 1000/3;\r\ntol = 1e-11;\r\nassert(abs(simpsInt(Fx,deltax) - y_correct) \u003c tol)\r\n\r\n%%\r\ndeltax = 1;\r\nFx = (0:10).^4;\r\ny_correct = 20001.3333333333333;\r\ntol = 1e-9;\r\nassert(abs(simpsInt(Fx,deltax) - y_correct) \u003c tol)\r\n\r\n%%\r\ndeltax = 1;\r\nFx = sin(-5:5);\r\ny_correct = 0;\r\ntol = 1e-15;\r\nassert(abs(simpsInt(Fx,deltax) - y_correct) \u003c tol)\r\n\r\n%%\r\ndeltax = 0.25;\r\nFx = sin(0:.25:100);\r\ny_correct = 0.13768413796203;\r\ntol = 1e-15;\r\nassert(abs(simpsInt(Fx,deltax) - y_correct) \u003c tol)\r\n\r\n%%\r\ndeltax = 1;\r\nFx = double((0:10) \u003e= 5);\r\ny_correct = 5.66666666666667;\r\ntol = 1e-14;\r\nabs(simpsInt(Fx,deltax) - y_correct) \u003c tol\r\n\r\n","published":true,"deleted":false,"likes_count":4,"comments_count":9,"created_by":544,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":48,"test_suite_updated_at":"2016-10-02T01:31:18.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2016-10-01T23:16:52.000Z","updated_at":"2026-02-25T15:06:57.000Z","published_at":"2016-10-01T23:16:52.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\u003eI wonder what Homer Simpson would have thought of Simpson's rule? Somehow I doubt his thoughts would have included the phrase Newton-Cotes, or even numerical integration.\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\u003eIn this problem, I want you to 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://en.wikipedia.org/wiki/Simpson%27s_rule\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eSimpson's rule\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e to integrate a function, provided as a list of points in a vector. The points must be equally spaced in x, so all that need be provided is the spacing between points in x. As well, I'll be nice and always give you an odd number of points, since that is important for Simpson's rule to work smoothly.\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\u003eAs a test case, we should know that the integral of cos(x) over the interval [0,pi/2] is 1, at least analytically that is true. Your Simpson's rule code, for only 7 points spanning that interval will do pretty well:\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[deltax = pi/12;\\nFx = cos(linspace(0,pi/2,7));\\nsimpsInt(Fx,deltax)\\nans =\\n          1.00002631217059]]\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\u003eThus, Fx is a vector of supplied function values, and deltax is the step size taken on the x-axis.\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\u003eHomer would be proud.\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":43494,"title":"Weighted Convolution","description":"Given two input vectors x = [x_1, x_2, ..., x_K] and y = [y_1, y_2, ..., y_K] of equal length, compute the weighted convolution output z = [z_1, z_2, ..., z_K], where \r\n\r\n     z_k = sum(nchoosek(k-1,j-1)*x_{k-j+1}*y_j, j = 1..k),  k = 1, 2, ..., K\r\n\r\nExample: x = [1, 2, 3]; y = [4, 5, 6]. Then z = [z_1, z_2, z_3] where \r\n\r\n  z_1 = nchoosek(0,0)*1*4 = 4\r\n  z_2 = nchoosek(1,0)*2*4 + nchoosek(1,1)*1*5 = 13\r\n  z_3 = nchoosek(2,0)*3*4 + nchoosek(2,1)*2*5 + nchoosek(2,2)*1*6 = 38\r\n \r\nHint: This can be seen as the linear convolution weighted by the binomial coefficient. It is straightforward to solve this problem using a for loop. I am wondering if there exists some more elegant way (e.g., vectorization) to do this. \r\n\r\n","description_html":"\u003cp\u003eGiven two input vectors x = [x_1, x_2, ..., x_K] and y = [y_1, y_2, ..., y_K] of equal length, compute the weighted convolution output z = [z_1, z_2, ..., z_K], where\u003c/p\u003e\u003cpre\u003e     z_k = sum(nchoosek(k-1,j-1)*x_{k-j+1}*y_j, j = 1..k),  k = 1, 2, ..., K\u003c/pre\u003e\u003cp\u003eExample: x = [1, 2, 3]; y = [4, 5, 6]. Then z = [z_1, z_2, z_3] where\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ez_1 = nchoosek(0,0)*1*4 = 4\r\nz_2 = nchoosek(1,0)*2*4 + nchoosek(1,1)*1*5 = 13\r\nz_3 = nchoosek(2,0)*3*4 + nchoosek(2,1)*2*5 + nchoosek(2,2)*1*6 = 38\r\n\u003c/pre\u003e\u003cp\u003eHint: This can be seen as the linear convolution weighted by the binomial coefficient. It is straightforward to solve this problem using a for loop. I am wondering if there exists some more elegant way (e.g., vectorization) to do this.\u003c/p\u003e","function_template":"function z = weightedConv(x,y)\r\n  z = x;\r\nend","test_suite":"%%\r\nx = 1;\r\ny = 1;\r\nz = 1;\r\nassert(isequal(weightedConv(x,y),z))\r\n\r\n%%\r\nx = 1:2;\r\ny = 3:4;\r\nz = [3 10];\r\nassert(isequal(weightedConv(x,y),z))\r\n\r\n%%\r\nx = 1:3;\r\ny = 4:6;\r\nz = [4 13 38];\r\nassert(isequal(weightedConv(x,y),z))\r\n\r\n\r\n%%\r\nx = 1:4;\r\ny = 5:8;\r\nz = [5 16 46 124];\r\nassert(isequal(weightedConv(x,y),z))\r\n\r\n%%\r\nx = 1:10;\r\ny = 2:11;\r\nz = [2,7,22,64,176,464,1184,2944,7168,17152];\r\nassert(isequal(weightedConv(x,y),z))","published":true,"deleted":false,"likes_count":6,"comments_count":7,"created_by":12569,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":41,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2016-10-13T05:39:48.000Z","updated_at":"2025-12-15T03:59:38.000Z","published_at":"2016-10-13T06:12:52.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\u003eGiven two input vectors x = [x_1, x_2, ..., x_K] and y = [y_1, y_2, ..., y_K] of equal length, compute the weighted convolution output z = [z_1, z_2, ..., z_K], where\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[     z_k = sum(nchoosek(k-1,j-1)*x_{k-j+1}*y_j, j = 1..k),  k = 1, 2, ..., 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\u003eExample: x = [1, 2, 3]; y = [4, 5, 6]. Then z = [z_1, z_2, z_3] where\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[z_1 = nchoosek(0,0)*1*4 = 4\\nz_2 = nchoosek(1,0)*2*4 + nchoosek(1,1)*1*5 = 13\\nz_3 = nchoosek(2,0)*3*4 + nchoosek(2,1)*2*5 + nchoosek(2,2)*1*6 = 38]]\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\u003eHint: This can be seen as the linear convolution weighted by the binomial coefficient. It is straightforward to solve this problem using a for loop. I am wondering if there exists some more elegant way (e.g., vectorization) to do this.\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\"}]}"}],"no_progress_badge":{"id":53,"name":"Unknown","symbol":"unknown","description":"Partially completed groups","description_html":null,"image_location":"/images/responsive/supporting/matlabcentral/cody/badges/problem_groups_unknown_2.png","bonus":null,"players_count":0,"active":false,"created_by":null,"updated_by":null,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"created_at":"2018-01-10T23:20:29.000Z","updated_at":"2018-01-10T23:20:29.000Z","community_badge_id":null,"award_multiples":false}}