Find minimum of double-variable function on fixed interval

Hi every body I have this function : y=(G*r)/4 + ((G^2*r^2)/16 + (G*r)/4 - 1/4)^(1/2) + 1/2 and I would calculated the minimum of this, in 0:1 interval for both of variable.
How I can write their code?
regards

 採用された回答

Walter Roberson
Walter Roberson 2015 年 6 月 15 日

0 投票

y = @(G,r) abs((G*r)/4 + ((G^2*r^2)/16 + (G*r)/4 - 1/4)^(1/2) + 1/2);
yx = @(x) y(x(1), x(2));
A = [];
Aeq = [];
b = [];
beq = [];
lb = [0 0];
ub = [1 1];
[x, fval] = fmincon(yx, [rand(), rand()], A, b, Aeq, Beq, lb, ub);
Note: that particular function's minimum value of 1/sqrt(2) occurs over almost all of the region, so the location of the minimum is not unique.

9 件のコメント

Mohammad
Mohammad 2015 年 6 月 15 日
thanks for your guidance, but can you explain your code? what is x(1) and x(2)? i run this code and answer is x = 0.2785 0.5469 fval = 0.7071 is 0.2785 for G and 0.5469 for r?
Walter Roberson
Walter Roberson 2015 年 6 月 15 日
y = @(G,r) abs((G.*r)/4 + ((G.^2.*r.^2)/16 + (G.*r)/4 - 1/4).^(1/2) + 1/2);
yG_r = @(G_r) y(G_r(1), G_r(2));
A = [];
Aeq = [];
b = [];
beq = [];
lb = [0 0];
ub = [1 1];
[G_r, fval] = fmincon(yG_r, [rand(), rand()], A, b, Aeq, Beq, lb, ub);
Your function is defined in terms of two named variables, but fmincon supplies values as a vector. I packed the two together in the order G and then r. The yG_r layer unpacks the vector into two distinct parameters to pass to the function. It could all have been done in one step by defining y as acting on a vector and referencing the vector elements as required, but having the two layers allows the code to be clearer.
The output will be in the order G and then r, so Yes, 0.2785 was for G and 0.5469 was for r, and the function value was the 1/sqrt(2) that I indicated before.
If you run again then you will get a different location for the minimum because the function is flat for most of its locations. You can see this with:
[G_grid, r_grid] = ndgrid(linspace(0,1,100), linspace(0,1,100));
surf(G_grid, r_grid, y(G_grid, r_grid), 'EdgeColor','none')
Mohammad
Mohammad 2015 年 6 月 15 日
thanks for your answer what is Aeq=[] ?
Walter Roberson
Walter Roberson 2015 年 6 月 16 日
Those parameters have to do with equality and inequality constraints. You do not have any so the empty array holds the place
Mohammad
Mohammad 2015 年 6 月 16 日
編集済み: Mohammad 2015 年 6 月 16 日
very thanks, for your answer. I would appreciate if you explain what is difference between fminunc and fmincon ? and why you used rand[] in your command?
I write this code
> y = @(G,r) abs((G*r)/4 + ((G^2*r^2)/16 + (G*r)/4 - 1/4)^(1/2) + 1/2);
>> yx=@(x)y(x(1),x(2));
>> b=[];
>> a=[];
>> aeq=[];
>> beq=[];
>> lb=[0 0];
>> ub=[2 2];
>> [x,fval]=fmincon(yx,[0,1],a,b,aeq,beq,lb,ub)
in ninth line i said [0,1], i want to know what is difference between it and lb,ub ?
regards
Walter Roberson
Walter Roberson 2015 年 6 月 16 日
fminunc is for unconstrained minimization. You have a constraint, your 0 to 1 range.
The second parameter, the one I used rand for, is the starting location for the search and does not have to do with bounds other than that the starting point should be somewhere that is within the boundaries
Mohammad
Mohammad 2015 年 6 月 16 日
I am very thankful, for your guidance.
Mohammad
Mohammad 2015 年 6 月 16 日
just one question if i want find the maximum of that function; what is this code? thanks
Walter Roberson
Walter Roberson 2015 年 6 月 16 日
yx = @(x) -y(x(1),x(2));
that is, minimize the negative.

サインインしてコメントする。

その他の回答 (1 件)

Titus Edelhofer
Titus Edelhofer 2015 年 6 月 15 日

0 投票

Hi,
just to be sure: your y is dependent both on G and r and you want to minimize on the square [0..1]x[0..1]?
In this case fmincon from optimization toolbox is your friend, although your function is not real-valued in the entire square. E.g. for G=0.5, r=0.5 the result is complex ...?
Titus

3 件のコメント

Mohammad
Mohammad 2015 年 6 月 15 日
answer of first question is yes. for second question;That's right my function is complex. However, I want to find minimum of abs of function.
Do you have example of fmincon? thanks
Walter Roberson
Walter Roberson 2015 年 6 月 15 日
the abs() of the function is 1/sqrt(2) for most of the area, and increases in a region near 0.81 to 1 in G and r. The minimum is therefor going to be 1/sqrt(2)
Mohammad
Mohammad 2015 年 6 月 15 日
After calculating the minimum, i want to find the value which minimum occurred. How i can find minimum of double-variable function?

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by