Cody

Problem 45951. Function minimization - beat fminsearch!

Solution 2742223

Submitted on 26 Jul 2020 by Binbin Qi
  • Size: 17
  • This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
filetext = fileread('better_than_fminsearch.m'); filetext(find(filetext == '%', 1, 'last') : end) = []; assert(isempty(strfind(filetext, '>')), 'File handling is forbidden.'); assert(isempty(strfind(filetext, 'eval')), 'Eval forbidden.'); assert(isempty(strfind(filetext, 'regexp')), 'Regexp forbidden.'); assert(isempty(strfind(filetext, '!')), 'Shell commands are forbidden.'); assert(isempty(strfind(filetext, 'mlock')), 'mlock is forbidden.'); assert(isempty(strfind(filetext, 'munlock')), 'munlock is forbidden.'); assert(isempty(strfind(filetext, '@')), 'Anonymous functions are forbidden.');

2   Pass
fun=@(x)cos(x)-cos(pi*x)/pi; x0=0; xopt = better_than_fminsearch(fun,x0) xsubopt = fminsearch(fun,x0) yopt=fun(xopt) ysubopt=fun(xsubopt) assert(yopt < ysubopt)

xopt = 9.8611 xsubopt = 0 yopt = -1.1948 ysubopt = 0.6817

3   Pass
fun=@(x)sin(20./x).*sin(20*x) x0=1; xopt = better_than_fminsearch(fun,x0) xsubopt = fminsearch(fun,x0) yopt=fun(xopt) ysubopt=fun(xsubopt) assert(yopt < ysubopt)

fun = function_handle with value: @(x)sin(20./x).*sin(20*x) xopt = 11.2313 xsubopt = 1.0803 yopt = -0.9780 ysubopt = -0.1239

Suggested Problems

More from this Author10

Community Treasure Hunt

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

Start Hunting!