Cody

Problem 45948. Function minimization - beat fminbnd!

Solution 2742241

Submitted on 26 Jul 2020 by Binbin Qi
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_fminbnd.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; xmin=0; xmax=10*pi; xopt = better_than_fminbnd(fun,xmin,xmax); xsubopt = fminbnd(fun,xmin,xmax); yopt=fun(xopt) ysubopt=fun(xsubopt) assert(xopt>=xmin & xopt<=xmax & fun(xopt) < fun(xsubopt))

yopt = -1.3183 ysubopt = -1.1948

3   Pass
fun=@(x)sin(20./x).*sin(20*x) xmin=1; xmax=5; xopt = better_than_fminbnd(fun,xmin,xmax); xsubopt = fminbnd(fun,xmin,xmax); yopt=fun(xopt) ysubopt=fun(xsubopt) assert(xopt>=xmin & xopt<=xmax & fun(xopt) < fun(xsubopt))

fun = function_handle with value: @(x)sin(20./x).*sin(20*x) yopt = -0.9958 ysubopt = -0.5881

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!