フィルターのクリア

Stationary point Code Error. Trying to find stationary points for the equation below. Was having a hard time doing it by hand so tried a code.. getting error for fsolve.

4 ビュー (過去 30 日間)
% Define the function f(x1, x2)
f = @(x) x(1)^2 + x(1)*x(2) + 3/2*x(2)^2 - 2*log(x(1)) - log(x(2));
% Define the gradient ∇f(x1, x2)
gradient = @(x) [2*x(1) + x(2) - 2/x(1); x(1) + 3*x(2) - 1/x(2)];
% Define a function that returns a vector for fsolve
stationary_points = fsolve(@(x) gradient(x), [0; 0], options);
% The variable stationary_points now contains the stationary points
disp('Stationary Points:');
disp(stationary_points);

採用された回答

Star Strider
Star Strider 2024 年 2 月 11 日
One problem is using zero for any initial parameter estimate, and especially if the parameter is the only element in the denominator, since that becomes Inf and the solver immediately stops.
Start with different initial estimates instead —
% Define the function f(x1, x2)
f = @(x) x(1)^2 + x(1)*x(2) + 3/2*x(2)^2 - 2*log(x(1)) - log(x(2));
% Define the gradient ∇f(x1, x2)
gradient = @(x) [2*x(1) + x(2) - 2/x(1); x(1) + 3*x(2) - 1/x(2)];
% Define a function that returns a vector for fsolve
stationary_points = fsolve(@(x) gradient(x), rand(2,1));%, options);
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
stationary_points = 2×1
0.8944 0.4472
% The variable stationary_points now contains the stationary points
disp('Stationary Points:');
Stationary Points:
disp(stationary_points);
0.8944 0.4472
You apparently defined an options structure, however did not include it, so I changed the fsolve call to exclude it.
.
  2 件のコメント
Rian Sullivan
Rian Sullivan 2024 年 2 月 11 日
Oh okay that makes sense. Thank you so much. I did have the options structure and I accidently took it out.. Thank you so much.

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

その他の回答 (1 件)

Matt J
Matt J 2024 年 2 月 11 日
編集済み: Matt J 2024 年 2 月 11 日
The problem is strictly convex, so obviously the staitonary point is unique and lies at the global minimum. So why not just use fminunc?
f = @(x) x(1)^2 + x(1)*x(2) + 3/2*x(2)^2 - 2*log(x(1)) - log(x(2));
stationary_point = fminunc(f,[1;1])
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
stationary_point = 2×1
0.8944 0.4472
  4 件のコメント
Matt J
Matt J 2024 年 2 月 11 日
編集済み: Matt J 2024 年 2 月 11 日
No, a convex function with an open domain (like your problem where the domain is x1>0, x2>0) will not have a finite global max. On a closed domain, there will be a global max, but the gradient won't be zero there.
Rian Sullivan
Rian Sullivan 2024 年 2 月 11 日
Ah okay I understand now thank you so much!

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

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by