Find maximum of a function 2 variables

5 次查看(过去 30 天)
how can I find the maximum of f4 where 0<=x<=1 and 0<=t<=pi/2 ?
syms x t
syms f(x,t)
f(x,t)=(2-x^2*(sin(t))^(1/2));
f1=diff(f(x,t),t);
f2=diff(f1,t);
f3=diff(f2,t);
f4(x,t)=diff(f3,t);
  3 个评论
Torsten
Torsten 2019-1-2
编辑:Torsten 2019-1-2
(sin(t)^2)^(1/2) = abs(sin(t)), and since 0<=t<=pi/2, f(x,t)=2-x^2*sin(t). Is this the correct f ?

请先登录,再进行评论。

回答(3 个)

Star Strider
Star Strider 2019-1-1
编辑:Star Strider 2019-1-2
Using the Symbolic Math Toolbox for an optimisation problem is probably not appropriate.
Try this:
f = @(x,t) (2-x^2*(sin(t))^(1/2));
XT = fmincon(@(b)-f(b(1),b(2)), [0.5; 0.5], [], [], [], [], [0 0], [1 pi/2])
producing:
XT =
0.000446642879507019
0.689946783740861
EDIT — (2 Jan 2019 at 18:33 UCT)
With the new function, and question clarification:
syms f(x,t)
f(x,t)=(2-x^2*(sin(t)^2))^(1/2);
f1=diff(f(x,t),t);
f2=diff(f1,t);
f3=diff(f2,t);
f4(x,t)=diff(f3,t);
f4 = simplify(f4, 'Steps',20);
f = matlabFunction(f4, 'Vars',{[x,t]});
XT = fmincon(@(b)-f(b), [0.5 0.5], [], [], [], [], [0 0], [1 pi/2])
XT =
0.999999986840462 0.656184655563777

John D'Errico
John D'Errico 2019-1-1
编辑:John D'Errico 2019-1-1
Have you been told to use the symbolic toolbox? Of course, you can do so. But sometimes it is easier just to use a numerical tool, anything from fminsearch to fmincon. But before you do that, lets look at the surface. Often this can give you an insight into the process, and mathematical insight is always better than brute force computation.
syms x t
syms f(x,t)
f(x,t)=(2-x^2*(sin(t))^(1/2));
ezsurf(f,[0 1],[0,pi/2])
In general, it is ALWAYS right to plot EVERYTHING you can plot.
Does this give you any ideas? You should recognize that this function does not seem to have a unique maximum on that domain.
So using an optimizer will give you A result, but it will not really tell the truth, because you may be confused, expecting there to be a unique solution.
In fact, if you look at the function, you would see that for any value of x==0, regardless of t, the function hasa value of 2, AND EXACTLY 2, and that the function can never exceed 2. As well, whenever t==0, it does not matter what x is, again, the function has a constant value of 2.
So your function has its maximum along the TWO lower edges of the domain. ANY point along those edges will result in the maximum.
What will happen if you try to use fmincon? Just for kicks, lets try it, with several different starting values. Remember, that in order to maximize using a minimizer tool, you need to negate the objective.
Fxt = @(xt) -(2 - xt(1).^2.*sqrt(sin(xt(2))));
[xt,fval] = fmincon(Fxt,[.5 .5],[],[],[],[],[0 0], [1 pi/2])
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
xt =
0.000446642879508753 0.689946783741699
fval =
-1.99999984084558
[xt,fval] = fmincon(Fxt,[.1 .9],[],[],[],[],[0 0], [1 pi/2])
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
xt =
0.000684543819254824 0.889798593575601
fval =
-1.99999958695475
[xt,fval] = fmincon(Fxt,[.99 .01],[],[],[],[],[0 0], [1 pi/2])
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
xt =
9.7192758663948e-05 0.785121771761396
fval =
-1.99999999205763
So, I tried it 3 different times, with 3 sets of starting values. Each time, I got a different minimizer of -F(x,t). All were equally valid. But none were any better than the others.
We can return to the symbolic solve to see if this could have been predicted, but even solve will get confused, not really understanding that there are infinitely many solutions.
xtsol = solve(dfdx,dfdt)
xtsol =
struct with fields:
t: [2×1 sym]
x: [2×1 sym]
>> xtsol.t
ans =
0
pi/2
>> xtsol.x
ans =
0
0
It found two solutions, but does not predict the infinite set of solutions that really exist. Sometimes what you really need is just a plot, and some mathematical thinking applied to the problem.
  1 个评论
Star Strider
Star Strider 2019-1-1
I considered using a Lagrangian multiplier for the symbolic optimisation. The approach in Constrained Optimization Using Lagrange Multipliers (link) may be appropriate. (It is however more effort than I want to go into today.)

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2019-1-1
f4 is unbounded. When x > 0, the limit of f4 as t approaches 0 is +infinity.
>> double(f4(1/10,1/100000))
ans =
2.96463530638315e+15

类别

Help CenterFile Exchange 中查找有关 Solver-Based Nonlinear Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by