필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
Arvind
Arvind 2024년 4월 17일
댓글: Arvind 2024년 4월 17일
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