# For LSQNONLIN is it okay if the number of elements in the vector-valued function varies per iteration?

2 views (last 30 days)
Kevin Moerman on 1 Mar 2012
For LSQNONLIN is it okay if the number of elements in the vector-valued function varies per iteration?
I have a vector-valued optimisation function producing say the variable Fopt. However for some parameter combinations some of the entries in the vector Fopt may yield NaN. At present I have this in the optimisation function:
Fopt=Fopt(~isnan(Fopt));
However that will result in the number of elements varying with iterations. Alternatively I can keep the same vector length but allow for NaN entries.
Which is the better approach?
Kevin
##### 1 CommentShowHide None
Kevin Moerman on 1 Mar 2012
Actually lets expand the question a bit. How "intelligent" is LSQNONLIN with respect to the vector-values function? Does it compare say entry 10 from iteration 1 with entry 10 from iteration 2? In that case the length would be important but also the 1-to-1 mapping is important.

Seth DeLand on 1 Mar 2012
Hi Kevin,
LSQNONLIN is using the values in the Fopt vector to determine the gradient, and it does this by calculating Fopt at different points. For example, it might take the difference of entry 10 from iteration 1 and entry 10 from iteration 2 and then use that to determine which way is downhill.
So you don't want to change the position of the values that the function is returning, as this could mess up the gradient calculation.
If LSQNONLIN encounters a NaN in Fopt, it is treated similarly to an Inf. Since we're trying to minimize, this point will most likely get rejected and LSQNONLIN will try to move back to a solution space where all of the values in Fopt were finite.
So the questions you need to ask yourself is: How do I want NaN's in Fopt to be interpreted? If having NaN's in Fopt is a bad solution for your problem, then it's fine for the Fopt vector to return NaN's since LSQNONLIN will treat those as really bad values of your objective function. If NaN has some other meaning, you may need to change your objective function to quantify what it means to have a NaN (i.e. if NaN is a good thing, then you would want to replace the NaN's in Fopt with something with a low penalty value).

### Community Treasure Hunt

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

Start Hunting!

Translated by