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

1 visualización (últimos 30 días)
Arvind
Arvind el 17 de Abr. de 2024
Comentada: Arvind el 17 de Abr. de 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);

Respuesta aceptada

Steven Lord
Steven Lord el 17 de Abr. de 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.

Más respuestas (0)

Categorías

Más información sobre Optimization Toolbox en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by