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

5 次查看(过去 30 天)
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);

采纳的回答

Steven Lord
Steven Lord 2024-4-17
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by