Can you help me to use function fsolve to calculate costs?

1 view (last 30 days)
I HAVE A 2X2 NON LINEAR SYSTEM AND I WANT TO CALCULATE THE ITERATIONS AND ESPECIALLY THE FUNCTION AND JACOBIAN COSTS . CAN YOU HELP ME ?
I HAVE THIS CODE AND I DONT KNOW WHAT TO DO NEXT
% Define the function handle for the system of equations
F = @(x) [ (x(1)-4)^2 + (x(2)-4)^2 - 5; x(1)^2 + x(2)^2 - 16 ];
% Set the initial guess
x0 = [1.8; 3.7];
% Set the options for the solver
options = optimoptions('fsolve','Display','iter');
% Call the solver
[x, fval] = fsolve(F, x0, options);
format longeng
disp('The solution of the system is');
disp( x )

Answers (1)

Walter Roberson
Walter Roberson on 4 Apr 2023
[x, fval, exitflag, info] = fsolve(F, x0, options);
number_of_info.iterations
However, nothing is recorded about the "cost" of the function, or the "cost" of the Jacobian estimatation.
  1 Comment
Georgios
Georgios on 4 Apr 2023
Thank you about the iterations . I also tried this code for "cost" of the function and the "cost" of the Jacobian estimatation but it has errors . I will appreciate your help.
CODE:
% Define the function handle for the system of equations
F = @(x) [ (x(1)-4)^2 + (x(2)-4)^2 - 5;
x(1)^2 + x(2)^2 - 16 ];
%Set the initial guess
x0 = [1.8; 3.7];
%Set the options for the solver
options = optimoptions('fsolve','Display','iter');
% Define the output function
outfun = @(x, optimValues, state) myoutput(x, optimValues, state);
%Call the solver
[x, fval, output] = fsolve(F, x0, options, optimset('OutputFcn', outfun));
%Display the solution and the number of iterations and cost
fprintf('\n\nThe roots are: x1 = %f, x2 = %f\n\n', x(1), x(2));
fprintf('Total cost: %d function evaluations and %d Jacobian evaluations\n',output.funcCount, output.jacobianEvals);
fprintf('Number of iterations: %d\n', output.iterations);

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!