Effacer les filtres
Effacer les filtres

I get an error using fsolve but I don't understand why?

5 vues (au cours des 30 derniers jours)
Arvind
Arvind le 17 Avr 2024
Commenté : Arvind le 17 Avr 2024
I have get an error
Too many output arguments.
Error in @(r)calculate_f(r,alphai,phi,phi0,n,m,fraci) (line 21)
func = @(r) calculate_f(r, alphai, phi, phi0, n, m, fraci);
Failure in initial objective function evaluation. FSOLVE cannot continue.
function f = calculate_f(r, alphai, phi, phi0, n, m, fraci)
f = zeros(m, 1);
f(1) = log(alphai(1) / r(1)) + log(1.0 - phi0 / phi) - log(1 - ((1.0 - phi) / (1.0 - phi0))^(1.0 / n));
for j = 2:m
f(j) = f(j-1) + log(alphai(j) / r(j)) + log(r(j-1) / alphai(j-1) - fraci(j-1));
end
end
func = @(r) calculate_f(r, alphai, phi, phi0, n, m, fraci);
r0 = r * ones(m, 1);
options = optimoptions('fsolve', 'Algorithm', 'trust-region-dogleg', 'SpecifyObjectiveGradient', true, 'CheckGradients', false, 'ScaleProblem', 'jacobian', 'FunctionTolerance', phitol);
ri = fsolve(func, r0, options);

Réponse acceptée

Steven Lord
Steven Lord le 17 Avr 2024
Look at the description of the fun input argument on the documentation page for the fsolve function. The relevant part of that documentation:
"f the Jacobian can also be computed and the 'SpecifyObjectiveGradient' option is true, set by
options = optimoptions('fsolve','SpecifyObjectiveGradient',true)
the function fun must return, in a second output argument, the Jacobian value J, a matrix, at x."
Your function does not return a second output argument. Either have it return that second output argument or don't set 'SpecifyObjectiveGradient' to true.

Plus de réponses (0)

Catégories

En savoir plus sur Systems of Nonlinear Equations dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by