{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-05-26T00:16:20.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":"2026-05-26T00: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":42447,"title":"Define the operators of  function_handles","description":"Suppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\r\n     \r\n  e.g. \r\n     if   f = @(x)x and g = @(x)x+1\r\n     then\r\n          f+g = @(x)2*x+1\r\n          f-g = @(x)-1\r\n          f*g = @(x)x*(x+1)\r\n          f/g = @(x)x/(x+1)","description_html":"\u003cp\u003eSuppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ee.g. \r\n   if   f = @(x)x and g = @(x)x+1\r\n   then\r\n        f+g = @(x)2*x+1\r\n        f-g = @(x)-1\r\n        f*g = @(x)x*(x+1)\r\n        f/g = @(x)x/(x+1)\r\n\u003c/pre\u003e","function_template":"function y = fho(f,g,type)\r\n  % 'type' define which operation to be performed\r\n  % for simplicity, 1 represents '+', 2 represents '-', 3 represents '*', 4 represents '/'.\r\n  y = x;\r\nend","test_suite":"%%\r\nf = @(x)x;\r\ng = @(x)x+1;\r\ny = fho(f,g,1)\r\ny_correct = 3;\r\nassert(isequal(y(1),y_correct))\r\n\r\n%%\r\nf = @sqrt;\r\ng = @ceil;\r\ny = fho(f,g,2)\r\ny_correct = -2;\r\nassert(isequal(y(4),y_correct))\r\n\r\n%%\r\nf = @nextpow2;\r\ng = @log10;\r\ny = fho(f,g,3)\r\ny_correct = 4;\r\nassert(isequal(y(10),y_correct))\r\n\r\n%%\r\nf = @floor;\r\ng = @ceil;\r\ny = fho(f,g,4)\r\ny_correct = 0.5;\r\nassert(isequal(y(1.25),y_correct))","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":40597,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":60,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2015-07-05T14:27:43.000Z","updated_at":"2026-05-24T15:28:36.000Z","published_at":"2015-07-05T15:08: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\u003eSuppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\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[e.g. \\n   if   f = @(x)x and g = @(x)x+1\\n   then\\n        f+g = @(x)2*x+1\\n        f-g = @(x)-1\\n        f*g = @(x)x*(x+1)\\n        f/g = @(x)x/(x+1)]]\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":42817,"title":"Pipeline","description":"Do something that makes\r\n\r\nx\u003e f1 \u003e f2 ... \u003e fn\r\n\r\nequal to\r\n\r\nfn(...f2(f1(x))...)\r\n\r\n* x : MATLAB object\r\n* fi: function handle\r\n\r\nExample:\r\n\r\n  \u003e\u003e cos(sin(1))\r\n  ans =\r\n           0.666366745392881\r\n  \u003e\u003e 1 \u003e @sin \u003e @cos\r\n  ans =\r\n           0.666366745392881","description_html":"\u003cp\u003eDo something that makes\u003c/p\u003e\u003cp\u003ex\u0026gt; f1 \u0026gt; f2 ... \u0026gt; fn\u003c/p\u003e\u003cp\u003eequal to\u003c/p\u003e\u003cp\u003efn(...f2(f1(x))...)\u003c/p\u003e\u003cul\u003e\u003cli\u003ex : MATLAB object\u003c/li\u003e\u003cli\u003efi: function handle\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u0026gt;\u0026gt; cos(sin(1))\r\nans =\r\n         0.666366745392881\r\n\u0026gt;\u0026gt; 1 \u0026gt; @sin \u0026gt; @cos\r\nans =\r\n         0.666366745392881\r\n\u003c/pre\u003e","function_template":"function y = foo\r\n  y = x;\r\nend","test_suite":"%%\r\nfoo()\r\n\r\n%%\r\nassert(isequal(cos(sin(1)), 1\u003e@sin\u003e@cos))\r\n\r\n%%\r\nx = rand(10,'single');\r\nassert(isequal(sum(sum(x\u003e.2)), x\u003e.2\u003e@sum\u003e@sum))\r\n\r\n%%\r\na = rand(100);\r\n[x,y] = find(tan(a)\u003e.1);\r\n[x1,y1] = a\u003e @tan \u003e .1 \u003e @find;\r\nassert(isequal({x,y},{x1,y1}))\r\n\r\n%%\r\na = rand(1,100);\r\na\u003e @(x)x.^2+1 \u003e @cos \u003e @sin \u003e @sum \u003e @(x)isequal(x,sum(sin(cos(a.^2+1)))) \u003e @assert\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":2,"created_by":1434,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":4,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2016-04-22T03:54:50.000Z","updated_at":"2026-05-28T17:36:47.000Z","published_at":"2016-04-22T03:54: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\u003eDo something that makes\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\u003ex\u0026gt; f1 \u0026gt; f2 ... \u0026gt; fn\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\u003eequal to\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\u003efn(...f2(f1(x))...)\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\u003ex : MATLAB object\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\u003efi: function handle\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[\u003e\u003e cos(sin(1))\\nans =\\n         0.666366745392881\\n\u003e\u003e 1 \u003e @sin \u003e @cos\\nans =\\n         0.666366745392881]]\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":42248,"title":"Return fibonacci sequence do not use loop and condition version 3","description":"Calculate the nth Fibonacci number,return sequence\r\n\r\nGiven n, return f where f = fib(n) and f(1) = 1, f(2) = 1, f(3) = 2, ...\r\n\r\nin this time,return a function handle\r\n\r\n f  = fib(n)\r\n\r\ngenerate the sequence from the n-th item\r\n\r\nif n = 3\r\n\r\nfirst call:\r\n\r\n f() = 2\r\n\r\nsecond call\r\n\r\n f() = 3\r\n\r\nthird call\r\n\r\n f() = 5\r\n\r\nif n = 7\r\n\r\nfirst call:\r\n\r\n f() = 13\r\n\r\nsecond call\r\n\r\n f() = 21\r\n\r\nthird call\r\n\r\n f() = 34\r\n\r\nbut, loop and conditional statement is forbidden","description_html":"\u003cp\u003eCalculate the nth Fibonacci number,return sequence\u003c/p\u003e\u003cp\u003eGiven n, return f where f = fib(n) and f(1) = 1, f(2) = 1, f(3) = 2, ...\u003c/p\u003e\u003cp\u003ein this time,return a function handle\u003c/p\u003e\u003cpre\u003e f  = fib(n)\u003c/pre\u003e\u003cp\u003egenerate the sequence from the n-th item\u003c/p\u003e\u003cp\u003eif n = 3\u003c/p\u003e\u003cp\u003efirst call:\u003c/p\u003e\u003cpre\u003e f() = 2\u003c/pre\u003e\u003cp\u003esecond call\u003c/p\u003e\u003cpre\u003e f() = 3\u003c/pre\u003e\u003cp\u003ethird call\u003c/p\u003e\u003cpre\u003e f() = 5\u003c/pre\u003e\u003cp\u003eif n = 7\u003c/p\u003e\u003cp\u003efirst call:\u003c/p\u003e\u003cpre\u003e f() = 13\u003c/pre\u003e\u003cp\u003esecond call\u003c/p\u003e\u003cpre\u003e f() = 21\u003c/pre\u003e\u003cp\u003ethird call\u003c/p\u003e\u003cpre\u003e f() = 34\u003c/pre\u003e\u003cp\u003ebut, loop and conditional statement is forbidden\u003c/p\u003e","function_template":"function y = fib(x)\r\n  y = x;\r\nend","test_suite":"%%% Clean workspace\r\n% !/bin/cp fib.m safe\r\n% !/bin/rm *.*\r\n% !/bin/mv safe fib.m\r\n\r\n% Clean user's function from some known jailbreaking mechanisms\r\nfunctions={'!','feval','eval','str2func','str2num','regex','system','dos','unix','perl','assert','fopen','write','save','setenv','path','please','for','if','while','switch','global','figure'...\r\n    'round','roundn','fix','ceil','char','floor','\\.','^','pow','\\^','sscanf','persistent'};\r\nfid = fopen('fib.m');\r\n  st = char(fread(fid)');\r\n  for n = 1:numel(functions)\r\n    st = regexprep(st, functions{n}, 'error(''No fancy functions!''); %','ignorecase');\r\n  end\r\n \r\nfclose(fid);\r\nfid = fopen('fib.m' , 'w');\r\n  fwrite(fid,st);\r\nfclose(fid);\r\n\r\n%%\r\nn = 2;\r\nf = fib(n);\r\nassert(isequal(f() + f(),f()));\r\nassert(isequal(f(),5));\r\n\r\n\r\n\r\n%%\r\nn = 7;\r\nf = fib(n);\r\nassert(isequal(f() + f(),f()));\r\nassert(isequal(f() + f(),f()));\r\nassert(isequal(f(),233));\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":3668,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":"2015-04-22T17:14:27.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2015-04-22T17:05:37.000Z","updated_at":"2026-05-28T04:50:38.000Z","published_at":"2015-04-22T17:05:37.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\u003eCalculate the nth Fibonacci number,return sequence\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\u003eGiven n, return f where f = fib(n) and f(1) = 1, f(2) = 1, f(3) = 2, ...\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 time,return a function handle\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  = fib(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\u003egenerate the sequence from the n-th item\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\u003eif n = 3\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\u003efirst call:\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() = 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\u003esecond call\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() = 3]]\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\u003ethird call\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() = 5]]\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\u003eif n = 7\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\u003efirst call:\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() = 13]]\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\u003esecond call\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() = 21]]\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\u003ethird call\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() = 34]]\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\u003ebut, loop and conditional statement is forbidden\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":42588,"title":"Derivative function","description":"Given a function handle f, generate a function that evaluates the derivative of f\r\n\r\n\r\nExamples:\r\n    \r\n   f = @sin;\r\n   df = Derivative(f);\r\n   df([0 pi])\r\n   ans =\r\n        1    -1\r\n\r\nDerivative of sin is cos, cos([0 pi]) = [1 -1]\r\n\r\nHint(Added 2015/9/17):\r\n\u003chttps://en.wikipedia.org/wiki/Numerical_differentiation\u003e","description_html":"\u003cp\u003eGiven a function handle f, generate a function that evaluates the derivative of f\u003c/p\u003e\u003cp\u003eExamples:\u003c/p\u003e\u003cpre\u003e   f = @sin;\r\n   df = Derivative(f);\r\n   df([0 pi])\r\n   ans =\r\n        1    -1\u003c/pre\u003e\u003cp\u003eDerivative of sin is cos, cos([0 pi]) = [1 -1]\u003c/p\u003e\u003cp\u003eHint(Added 2015/9/17): \u003ca href = \"https://en.wikipedia.org/wiki/Numerical_differentiation\"\u003ehttps://en.wikipedia.org/wiki/Numerical_differentiation\u003c/a\u003e\u003c/p\u003e","function_template":"function df = Derivative(f)\r\n  df = f;\r\nend","test_suite":"%%\r\nf = @sin;\r\ndf = Derivative(f);\r\nx = 10*rand(10000,1);\r\ndy = cos(x);\r\nassert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\n%% \r\nf = @log;\r\ndf = Derivative(f);\r\nx = exp(500*rand(10000,1));\r\ndy = 1./x;\r\nassert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\n%%\r\nf = @exp;\r\ndf = Derivative(f);\r\nx = 50*(rand(10000,1)-0.5);\r\ndy = exp(x);\r\nassert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\n%%\r\nt = 10*rand;\r\nf = @(x)1./(x+t);\r\ndf = Derivative(f);\r\nx = 100*(rand(10000,1)-0.5); x(x==-t) = 1;\r\ndy = -1./(x+t).^2;\r\nassert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\n%%\r\nt = 10*rand-5;\r\nf = @(x)atan(t*x);\r\ndf = Derivative(f);\r\nx = 100*(rand(10000,1)-0.5);\r\ndy = t./(t^2*x.^2 + 1);\r\nassert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\n%%\r\nfor t = 2:6\r\n    f = @(x)t.^x./(sin(x).^t+cos(x).^t);\r\n    df = Derivative(f);\r\n    x = 3+rand(10000,1);\r\n    dy = (t.^x.*log(t))./(cos(x).^t+sin(x).^t)-...\r\n        (t.^x.*(t.*cos(x).*sin(x).^(t-1)-...\r\n        t.*cos(x).^(t-1).*sin(x)))./(cos(x).^t+sin(x).^t).^2;\r\n    assert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\nend","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":1434,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":"2015-09-08T18:13:33.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2015-09-07T05:46:00.000Z","updated_at":"2026-05-28T13:39:34.000Z","published_at":"2015-09-07T05:47:35.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 f, generate a function that evaluates the derivative of f\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\u003eExamples:\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 = @sin;\\n   df = Derivative(f);\\n   df([0 pi])\\n   ans =\\n        1    -1]]\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\u003eDerivative of sin is cos, cos([0 pi]) = [1 -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\u003eHint(Added 2015/9/17):\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/Numerical_differentiation\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026lt;https://en.wikipedia.org/wiki/Numerical_differentiation\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\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":{"problems":[{"id":42447,"title":"Define the operators of  function_handles","description":"Suppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\r\n     \r\n  e.g. \r\n     if   f = @(x)x and g = @(x)x+1\r\n     then\r\n          f+g = @(x)2*x+1\r\n          f-g = @(x)-1\r\n          f*g = @(x)x*(x+1)\r\n          f/g = @(x)x/(x+1)","description_html":"\u003cp\u003eSuppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ee.g. \r\n   if   f = @(x)x and g = @(x)x+1\r\n   then\r\n        f+g = @(x)2*x+1\r\n        f-g = @(x)-1\r\n        f*g = @(x)x*(x+1)\r\n        f/g = @(x)x/(x+1)\r\n\u003c/pre\u003e","function_template":"function y = fho(f,g,type)\r\n  % 'type' define which operation to be performed\r\n  % for simplicity, 1 represents '+', 2 represents '-', 3 represents '*', 4 represents '/'.\r\n  y = x;\r\nend","test_suite":"%%\r\nf = @(x)x;\r\ng = @(x)x+1;\r\ny = fho(f,g,1)\r\ny_correct = 3;\r\nassert(isequal(y(1),y_correct))\r\n\r\n%%\r\nf = @sqrt;\r\ng = @ceil;\r\ny = fho(f,g,2)\r\ny_correct = -2;\r\nassert(isequal(y(4),y_correct))\r\n\r\n%%\r\nf = @nextpow2;\r\ng = @log10;\r\ny = fho(f,g,3)\r\ny_correct = 4;\r\nassert(isequal(y(10),y_correct))\r\n\r\n%%\r\nf = @floor;\r\ng = @ceil;\r\ny = fho(f,g,4)\r\ny_correct = 0.5;\r\nassert(isequal(y(1.25),y_correct))","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":40597,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":60,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2015-07-05T14:27:43.000Z","updated_at":"2026-05-24T15:28:36.000Z","published_at":"2015-07-05T15:08: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\u003eSuppose f and g are function_handles, try to define f+g,f-g,f*g and f/g.\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[e.g. \\n   if   f = @(x)x and g = @(x)x+1\\n   then\\n        f+g = @(x)2*x+1\\n        f-g = @(x)-1\\n        f*g = @(x)x*(x+1)\\n        f/g = @(x)x/(x+1)]]\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":42817,"title":"Pipeline","description":"Do something that makes\r\n\r\nx\u003e f1 \u003e f2 ... \u003e fn\r\n\r\nequal to\r\n\r\nfn(...f2(f1(x))...)\r\n\r\n* x : MATLAB object\r\n* fi: function handle\r\n\r\nExample:\r\n\r\n  \u003e\u003e cos(sin(1))\r\n  ans =\r\n           0.666366745392881\r\n  \u003e\u003e 1 \u003e @sin \u003e @cos\r\n  ans =\r\n           0.666366745392881","description_html":"\u003cp\u003eDo something that makes\u003c/p\u003e\u003cp\u003ex\u0026gt; f1 \u0026gt; f2 ... \u0026gt; fn\u003c/p\u003e\u003cp\u003eequal to\u003c/p\u003e\u003cp\u003efn(...f2(f1(x))...)\u003c/p\u003e\u003cul\u003e\u003cli\u003ex : MATLAB object\u003c/li\u003e\u003cli\u003efi: function handle\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u0026gt;\u0026gt; cos(sin(1))\r\nans =\r\n         0.666366745392881\r\n\u0026gt;\u0026gt; 1 \u0026gt; @sin \u0026gt; @cos\r\nans =\r\n         0.666366745392881\r\n\u003c/pre\u003e","function_template":"function y = foo\r\n  y = x;\r\nend","test_suite":"%%\r\nfoo()\r\n\r\n%%\r\nassert(isequal(cos(sin(1)), 1\u003e@sin\u003e@cos))\r\n\r\n%%\r\nx = rand(10,'single');\r\nassert(isequal(sum(sum(x\u003e.2)), x\u003e.2\u003e@sum\u003e@sum))\r\n\r\n%%\r\na = rand(100);\r\n[x,y] = find(tan(a)\u003e.1);\r\n[x1,y1] = a\u003e @tan \u003e .1 \u003e @find;\r\nassert(isequal({x,y},{x1,y1}))\r\n\r\n%%\r\na = rand(1,100);\r\na\u003e @(x)x.^2+1 \u003e @cos \u003e @sin \u003e @sum \u003e @(x)isequal(x,sum(sin(cos(a.^2+1)))) \u003e @assert\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":2,"created_by":1434,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":4,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2016-04-22T03:54:50.000Z","updated_at":"2026-05-28T17:36:47.000Z","published_at":"2016-04-22T03:54: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\u003eDo something that makes\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\u003ex\u0026gt; f1 \u0026gt; f2 ... \u0026gt; fn\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\u003eequal to\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\u003efn(...f2(f1(x))...)\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\u003ex : MATLAB object\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\u003efi: function handle\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[\u003e\u003e cos(sin(1))\\nans =\\n         0.666366745392881\\n\u003e\u003e 1 \u003e @sin \u003e @cos\\nans =\\n         0.666366745392881]]\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":42248,"title":"Return fibonacci sequence do not use loop and condition version 3","description":"Calculate the nth Fibonacci number,return sequence\r\n\r\nGiven n, return f where f = fib(n) and f(1) = 1, f(2) = 1, f(3) = 2, ...\r\n\r\nin this time,return a function handle\r\n\r\n f  = fib(n)\r\n\r\ngenerate the sequence from the n-th item\r\n\r\nif n = 3\r\n\r\nfirst call:\r\n\r\n f() = 2\r\n\r\nsecond call\r\n\r\n f() = 3\r\n\r\nthird call\r\n\r\n f() = 5\r\n\r\nif n = 7\r\n\r\nfirst call:\r\n\r\n f() = 13\r\n\r\nsecond call\r\n\r\n f() = 21\r\n\r\nthird call\r\n\r\n f() = 34\r\n\r\nbut, loop and conditional statement is forbidden","description_html":"\u003cp\u003eCalculate the nth Fibonacci number,return sequence\u003c/p\u003e\u003cp\u003eGiven n, return f where f = fib(n) and f(1) = 1, f(2) = 1, f(3) = 2, ...\u003c/p\u003e\u003cp\u003ein this time,return a function handle\u003c/p\u003e\u003cpre\u003e f  = fib(n)\u003c/pre\u003e\u003cp\u003egenerate the sequence from the n-th item\u003c/p\u003e\u003cp\u003eif n = 3\u003c/p\u003e\u003cp\u003efirst call:\u003c/p\u003e\u003cpre\u003e f() = 2\u003c/pre\u003e\u003cp\u003esecond call\u003c/p\u003e\u003cpre\u003e f() = 3\u003c/pre\u003e\u003cp\u003ethird call\u003c/p\u003e\u003cpre\u003e f() = 5\u003c/pre\u003e\u003cp\u003eif n = 7\u003c/p\u003e\u003cp\u003efirst call:\u003c/p\u003e\u003cpre\u003e f() = 13\u003c/pre\u003e\u003cp\u003esecond call\u003c/p\u003e\u003cpre\u003e f() = 21\u003c/pre\u003e\u003cp\u003ethird call\u003c/p\u003e\u003cpre\u003e f() = 34\u003c/pre\u003e\u003cp\u003ebut, loop and conditional statement is forbidden\u003c/p\u003e","function_template":"function y = fib(x)\r\n  y = x;\r\nend","test_suite":"%%% Clean workspace\r\n% !/bin/cp fib.m safe\r\n% !/bin/rm *.*\r\n% !/bin/mv safe fib.m\r\n\r\n% Clean user's function from some known jailbreaking mechanisms\r\nfunctions={'!','feval','eval','str2func','str2num','regex','system','dos','unix','perl','assert','fopen','write','save','setenv','path','please','for','if','while','switch','global','figure'...\r\n    'round','roundn','fix','ceil','char','floor','\\.','^','pow','\\^','sscanf','persistent'};\r\nfid = fopen('fib.m');\r\n  st = char(fread(fid)');\r\n  for n = 1:numel(functions)\r\n    st = regexprep(st, functions{n}, 'error(''No fancy functions!''); %','ignorecase');\r\n  end\r\n \r\nfclose(fid);\r\nfid = fopen('fib.m' , 'w');\r\n  fwrite(fid,st);\r\nfclose(fid);\r\n\r\n%%\r\nn = 2;\r\nf = fib(n);\r\nassert(isequal(f() + f(),f()));\r\nassert(isequal(f(),5));\r\n\r\n\r\n\r\n%%\r\nn = 7;\r\nf = fib(n);\r\nassert(isequal(f() + f(),f()));\r\nassert(isequal(f() + f(),f()));\r\nassert(isequal(f(),233));\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":3668,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":"2015-04-22T17:14:27.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2015-04-22T17:05:37.000Z","updated_at":"2026-05-28T04:50:38.000Z","published_at":"2015-04-22T17:05:37.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\u003eCalculate the nth Fibonacci number,return sequence\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\u003eGiven n, return f where f = fib(n) and f(1) = 1, f(2) = 1, f(3) = 2, ...\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 time,return a function handle\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  = fib(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\u003egenerate the sequence from the n-th item\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\u003eif n = 3\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\u003efirst call:\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() = 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\u003esecond call\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() = 3]]\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\u003ethird call\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() = 5]]\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\u003eif n = 7\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\u003efirst call:\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() = 13]]\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\u003esecond call\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() = 21]]\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\u003ethird call\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() = 34]]\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\u003ebut, loop and conditional statement is forbidden\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":42588,"title":"Derivative function","description":"Given a function handle f, generate a function that evaluates the derivative of f\r\n\r\n\r\nExamples:\r\n    \r\n   f = @sin;\r\n   df = Derivative(f);\r\n   df([0 pi])\r\n   ans =\r\n        1    -1\r\n\r\nDerivative of sin is cos, cos([0 pi]) = [1 -1]\r\n\r\nHint(Added 2015/9/17):\r\n\u003chttps://en.wikipedia.org/wiki/Numerical_differentiation\u003e","description_html":"\u003cp\u003eGiven a function handle f, generate a function that evaluates the derivative of f\u003c/p\u003e\u003cp\u003eExamples:\u003c/p\u003e\u003cpre\u003e   f = @sin;\r\n   df = Derivative(f);\r\n   df([0 pi])\r\n   ans =\r\n        1    -1\u003c/pre\u003e\u003cp\u003eDerivative of sin is cos, cos([0 pi]) = [1 -1]\u003c/p\u003e\u003cp\u003eHint(Added 2015/9/17): \u003ca href = \"https://en.wikipedia.org/wiki/Numerical_differentiation\"\u003ehttps://en.wikipedia.org/wiki/Numerical_differentiation\u003c/a\u003e\u003c/p\u003e","function_template":"function df = Derivative(f)\r\n  df = f;\r\nend","test_suite":"%%\r\nf = @sin;\r\ndf = Derivative(f);\r\nx = 10*rand(10000,1);\r\ndy = cos(x);\r\nassert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\n%% \r\nf = @log;\r\ndf = Derivative(f);\r\nx = exp(500*rand(10000,1));\r\ndy = 1./x;\r\nassert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\n%%\r\nf = @exp;\r\ndf = Derivative(f);\r\nx = 50*(rand(10000,1)-0.5);\r\ndy = exp(x);\r\nassert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\n%%\r\nt = 10*rand;\r\nf = @(x)1./(x+t);\r\ndf = Derivative(f);\r\nx = 100*(rand(10000,1)-0.5); x(x==-t) = 1;\r\ndy = -1./(x+t).^2;\r\nassert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\n%%\r\nt = 10*rand-5;\r\nf = @(x)atan(t*x);\r\ndf = Derivative(f);\r\nx = 100*(rand(10000,1)-0.5);\r\ndy = t./(t^2*x.^2 + 1);\r\nassert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\n%%\r\nfor t = 2:6\r\n    f = @(x)t.^x./(sin(x).^t+cos(x).^t);\r\n    df = Derivative(f);\r\n    x = 3+rand(10000,1);\r\n    dy = (t.^x.*log(t))./(cos(x).^t+sin(x).^t)-...\r\n        (t.^x.*(t.*cos(x).*sin(x).^(t-1)-...\r\n        t.*cos(x).^(t-1).*sin(x)))./(cos(x).^t+sin(x).^t).^2;\r\n    assert(max(abs((dy-df(x))./dy))\u003c1e-13)\r\nend","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":1434,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":"2015-09-08T18:13:33.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2015-09-07T05:46:00.000Z","updated_at":"2026-05-28T13:39:34.000Z","published_at":"2015-09-07T05:47:35.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 f, generate a function that evaluates the derivative of f\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\u003eExamples:\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 = @sin;\\n   df = Derivative(f);\\n   df([0 pi])\\n   ans =\\n        1    -1]]\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\u003eDerivative of sin is cos, cos([0 pi]) = [1 -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\u003eHint(Added 2015/9/17):\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/Numerical_differentiation\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026lt;https://en.wikipedia.org/wiki/Numerical_differentiation\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\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\"}]}"}],"errors":[],"facets":[[{"value":"Handling Functions","count":1,"selected":false}],[{"value":"medium","count":2,"selected":false},{"value":"easy","count":1,"selected":false},{"value":"hard","count":1,"selected":false}]],"term":"tag:\"function_handle\"","page":1,"per_page":50,"sort":"map(difficulty_value,0,0,999) asc"}}