I am using Matlab fminsearch to minimize a equation with two variables sum((interval-5).^2, 2)*factorThe interval is a vector contains 5 values. They can be only picked sequentially from value 1 to 30 with step size is 1. The factor is a value from 0.1 to 0.9.
The code is below. I think the interval values are correct but factor value is wrong.
Interval value: [3 4 5 6 7]factor value: 0.6Final Output: 6
I think the factor value should be 0.1 and final output should be 1 as global minimum.
%% initialization of problem parametersminval = 1;maxval = 30;step = 1;count = 5;minFactor = 0.1;maxFactor = 0.9;%% the objective functionfun = @(interval, factor) sum((interval-5).^2, 2)*factor;%% a function that generates an interval from its initial valuegetinterval = @(start) floor(start) + (0:(count-1)) * step;getfactor =@(start2) floor(start2 * 10)/10;%% a modified objective function that handles constraintsobjective = @(start, start2) f(start, fun, getinterval, minval, maxval, getfactor, minFactor, maxFactor);%% finding the interval that minimizes the objective functionstart = [(minval+maxval)/2 (minFactor+maxFactor)/2];y = fminsearch(objective, start);bestvals = getinterval(y(1));bestfactor = getfactor(y(2));eval = fun(bestvals,bestfactor);disp(bestvals)disp(bestfactor)disp(eval)The code uses the following function f.
function y = f(start, fun, getinterval, minval, maxval, getfactor, minFactor, maxFactor) interval = getinterval(start(1)); factor = getfactor(start(2)); if (min(interval) < minval) || (max(interval) > maxval) || (factormaxFactor) y = Inf; else y = fun(interval, factor); end end
更多...