How is f(x) evaluated for each iteration using fsolve?

I'm using fsolve for an unconstrained non-linear function. And I notice that fval is much different than f(x) for the last iteration. How exactly is f(x) calculated?

13 Comments

Did you compare the x-vectors that led to fval and f(x) ?
TS
TS on 14 Jan 2019
Edited: TS on 14 Jan 2019
How can I see the x-vectors?
The value for fval corresponding to x is correct. But each iteration has some number of function evaluations (Func-count), which led me to think that f(x) is not the value of function but something else.
f(x) is always the function value of f, evaluated at x. If "fsolve" calls the function several times for each iteration, it is because it has to build the Jacobian for the Newton step. But the x-values also change slightly during these calls.
"And I notice that fval is much different than f(x) for the last iteration."
Why should the last function call necessarily use the fsolve output x value? I don't see why this would have to be the case, nor do I see anything in the documentation to support this.
"How exactly is f(x) calculated?"
Using the function that you provide, and whatever algorithm that fsolve uses. There are many algorithms:
So can I see the vaule of x at each iteration?
Of course. If you write "f" as a MATLAB function, you can output x every time the function is called.
Example:
function main
fun = @root2d;
x0 = [0,0];
x = fsolve(fun,x0,options)
end
function F = root2d(x)
F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2);
F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5;
x
end
"So can I see the vaule of x at each iteration?"
Of course, here are some options:
  • inside your function either print/display the current x value to the command window or to a file (simple).
  • add persistent and an input flag to your function to allow you to reset, collect the values into an array, and return them afterwards (complex).
  • plot the values using PlotFcn and any of the inbuilt plot functions, or write your own custom plot function:
  • define an OutputFcn, which can do anything you want and is called on each iteration:
  • display the f(x) values using the inbuilt Display option:
Which would you prefer?
Okay that was too easy. My bad.
But it still doesnt solve my problem. This is the output for the first iteration :
x=R and fun(x)=remaining_amount(R)
R = 0.1000
remaining_amount = 0.2324
R = 0.2000
remaining_amount = 0.2213
Norm of First-order Trust-region
Iteration Func-count f(x) step optimality radius
0 2 0.0540317 0.026 1
You see the value for f(x) displayed is in no way related to the function value.
Here is an option how to show x together with f(x):
I guess that 0.0540317 is the function value after the first Newton step, not when the function has been called two times.
TS
TS on 14 Jan 2019
Edited: Stephen23 on 14 Jan 2019
This is the plot showing the function value at each iteration
But the f(x) displayed are different (see below) :
Norm of First-order Trust-region
Iteration Func-count f(x) step optimality radius
0 2 0.0540317 0.026 1
1 4 0.0360172 1 0.0184 1
2 5 0.0360172 1 0.0184 1
3 7 0.0339781 0.25 0.0045 0.25
4 8 0.0339781 0.25 0.0045 0.25
5 9 0.0339781 0.0625 0.0045 0.0625
6 11 0.0321479 0.015625 0.00432 0.0156
Square the function values, and you arrive at the f(x) values. Don't know why f(x)^2 is shown in the convergence monitor.
So it's the squared error? I got confused because it says f(x) even though it's the sum of squared function values.
Thanks so much!
"I got confused because it says f(x) even though it's the sum of squared function values."
I agree that the inconsistent labeling is confusing at best, and at worst incorrect. You should make a bug report (and include a link to this thread).

Sign in to comment.

 Accepted Answer

The values shown under f(x) in the iteration table are indeed not literally the function values at that given iteration, you are seeing the square of the norm of the function value vector here; as documented, see:
The reason for this is that "fsolve" can actually solve whole systems of equations where f(x) would then be a whole vector of values. Displaying this whole vector in the table is not really feasible, there is not enough space for that. Hence, instead, a measure is shown which basically summarizes the whole f(x) vector in a single measure which also actually is representative for how good the current solution is.
We could look into whether we can perhaps change the header to ||f(x)||^2 in future releases to better illustrate this.

More Answers (1)

Tags

Asked:

TS
on 14 Jan 2019

Commented:

on 15 Jan 2019

Community Treasure Hunt

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

Start Hunting!