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 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Rian Sullivan
le 11 Fév 2024
Commenté : Star Strider
le 11 Fév 2024
% 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);
0 commentaires
Réponse acceptée
Star Strider
le 11 Fév 2024
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);
% The variable stationary_points now contains the stationary points
disp('Stationary Points:');
disp(stationary_points);
You apparently defined an options structure, however did not include it, so I changed the fsolve call to exclude it.
.
2 commentaires
Plus de réponses (1)
Matt J
le 11 Fév 2024
Modifié(e) : Matt J
le 11 Fév 2024
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])
4 commentaires
Voir également
Catégories
En savoir plus sur Systems of Nonlinear Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!