Maximize function with fminsearch

In my daily work, I have to maximize a specific function using fminsearch; the code:

clc
 clear all
 close all

 f = @(x,c,k) -(x(2)/c)^3*(((exp(-(x(1)/c)^k)-exp(-(x(2)/c)^k))/((x(2)/c)^k-(x(1)/c)^k))-exp(-(x(3)/c)^k))^2;
 c = 10.1;
 k = 2.3;
 X = fminsearch(@(x) f(x,c,k),[4,10,20]);

It works fine, as I expect, but not a problem: I need to bind x within certain limits, for example:

 4 < x(1) < 5
 10 < x(2) < 15
20 < x(3) < 30

To achieve the correct results, I have to use the optimization toolbar, which, unfortunately, I can’t convey.

Is there a way to get the same analysis using only fminsearch?

+3
source share
3 answers

, fminsearch , fminsearchbnd , . fminsearchbnd fminsearch. fminsearch , .

, , , , , . . fminsearchbnd , .

, rosenbrock [1,1] fminsearch. 2 , fminsearchbnd [2,4].

rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2;

fminsearch(rosen,[3 3])     % unconstrained
ans =
   1.0000    1.0000

fminsearchbnd(rosen,[3 3],[2 2],[])     % constrained
ans =
   2.0000    4.0000

, -inf inf .

fminsearchbnd(rosen,[3 3],[-inf 2],[])
ans =
       1.4137            2
+2

x, x, .

:

   function res = f(x,c,k)
        if x(1)>5 || x(1)<4
            penalty = 1000000000000;
        else
            penalty = 0;
        end
        res = penalty - (x(2)/c)^3*(((exp(-(x(1)/c)^k)-exp(-(x(2)/c)^k))/((x(2)/c)^k-(x(1)/c)^k))-exp(-(x(3)/c)^k))^2;
   end

, .

+1

, - : .

:

f = @(x,c,k, Xmin, Xmax) -(x(2)/c)^3*(((exp(-(x(1)/c)^k)-exp(-(x(2)/c)^k))/((x(2)/c)^k-(x(1)/c)^k))-exp(-(x(3)/c)^k))^2 ...
+ (x< Xmin)*(Xmin' - x' + 10000) + (x>Xmax)*(x' - Xmax' + 10000) ;
+1

All Articles