fminbnd loop leads to same optimizer

I have a matrix of parameter A that is 2*2 matrix and other matrix B. The solution matrix C that is 2*1
Then I run
for i=1:1:2
C(i,1)=fminbnd(@(b) fun(A(i,1),A(i,2),b,B),0,1)
end
Then strangely, even when paramters A(1,:) and A(2,:) are very different, I got the same optimizer. For example, A is
0.870639815937867 0.845094090200317
0.748241117938819 0.400000548158531
C is
0.150263993746969
0.150256643269687
This happens multiple times. What is going on? Can it be that first run of fminbnd affects the second run of fminbnd?

4 个评论

Another example:
A=
0.746821809634498 0.487998406012061
0.867490537729536 0.435364272702853
C=
0.150224392330097
0.150224392193498
Another example
0.779791079076863 0.657515932204619
0.860697964305429 0.418860403291225
C=
0.150285249468118
0.150270524267576
The crystal ball is so foggy today. It simply will not show me the contents of your computer, to tell us what the function fun does. It just keeps on telling me that you will soon come into a large sum of money. Or maybe that means I will come into some money. Whatever. ONE of us will be getting righ soon. Maybe. Should I try my deck of MATLAB tarot cards instead? Tea leaves? :)
If you really want help, then you need to show us the function fun.
Yes you wrote fminsearchbnd so I will appreciate if you take a look at the attached mix file. If one run this script for, let me say, 15 times, then one will encounter a case where the solutions of the optimization problems are the same even though the paramters are different. I wlll appreciate comments.

请先登录,再进行评论。

 采纳的回答

Matt J
Matt J 2020-8-3
编辑:Matt J 2020-8-3
In addition to hwat John said, you could plot the function to confirm whether fminbnd is giving you a good approximation of the minimizer in each case. If it is, that will tell you that it is not fminbnd's fault. The minimizer is simply not as sensitive a function of A as you had believed.
I would note that there is clearly some impact that A is having on the output. The successive C(i) that you have showed are similar, but not identical.

15 个评论

I did
options = optimset('PlotFcns',@optimplotfval,'TolX',1e-3);
and checked the plot. As I explained in the previous question. it often happens that title of the plot says "Current Value Function: NaN" although it does show fval, exitflag, and output.
Another question is that how can one check the value of variables within the objective function? I do not see them in Workspace.
Also the question is that what would be the next step? Is it possible to specify some different algorithm for fminbnd? It says that it uses "golden secion search: parabolic interpolation."
As I explained in the previous question.
I don't know what previous question you're referring to.
Another question is that how can one check the value of variables within the objective function? I do not see them in Workspace.
You can pause code execution inside the objective function, by setting conditional or non-conditional breakpoints,
Once you've paused at a certain line in the code in debug mode, you can inspect the variable values directly. In debug mode, you can also visit other workspaces using the function call stack drop-down
Also the question is that what would be the next step?
The next step will depend on the outcome of the first step. The first step is to determine whether the problem is in fminbnd or in the function you have provided. The latter is the more common problem, and certainly is likely if, as you say, the function is generating lots of NaNs.
You can pause code execution inside the objective function, by setting conditional or non-conditional breakpoints,
And so, for my own curiosity, I have done this very thing, setting a break point at line 40 in your code,
This allows me to evaluate the function at lots of different points. I've yet to find a single point where the objective function is not NaN:
K>> fun=@(b12) functionT(A(i,1),A(i,2),b12, RB);
K>> fun(0)
ans =
NaN
K>> fun(A(i,2))
ans =
NaN
K>> fun(A(i,2)/2)
ans =
NaN
K>> C
C =
0.0349
K>> fun(C(i,1))
ans =
NaN
You are right. The function does not deal with the possibility that inidex1 and index3 can be zero thus return NaN.
It means that it looks like your function is returning NaNs everywhere, regardless of the input.
Here is the new version that fixed the above problem. I checked breakpoints and they return valid values (not NaN). But still returns the same optimizer for different parameters if one tries like 15 times.
For example, how can one look at the value of index1, index2, index3, and index4 for different value of b12?
I checked breakpoints and they return valid values (not NaN).... how can one look at the value of index1, index2, index3, and index4 for different value of b12?
You can put a breakpoint inside functionT() and explore the variables in that workspace just as you did with the breakpoint at line 40.
Would you explain how or provide a reference/example? Thank you.
I learned a lot. Thank you.
Well, if I break at line 79, for example, I can print the values to the screen like so,
K>> index1,index2,index3,index4
index1 =
0
index2 =
0
index3 =
61
index4 =
5.0681
or just look at them in the workspace window
This is the problem that I encounter. I set a breakpoint at line 79. Then I run. Once it rans into "pause". But then it turn out "Run". I do not see the debug mode. What should I have done?
If the break point is not reached, it may be because an if-else-end statement sent the code along a different path...
Let me work on it.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by