How to use fminsearch for vectors
23 ビュー (過去 30 日間)
古いコメントを表示
I searched for prior discussions on fminsearch, including one on "How to use fminsearch for a vector" and did not find what I am looking for.
I have two somewhat independent vectors of measured values, A and B. Is the optimisation a linear domain or a plane? I have had a quick look at optimvar, fminsearch, fmincon, and fminbnd and have not secured a result. Error on fminsearch is " Maximum number of function evaluations has been exceeded". Is there a more appropriate function than fminsearch? Thank you.
rng('shuffle');
A = rand(1,10); % proxy for measured values
B = rand(1,10); % proxy for independent measured values
fun = @(ratio) min(sum(A.*ratio - B));
ratioRange = [1.0,1.2];
ratio = fminsearch(fun,ratioRange);
2 件のコメント
Matt J
2024 年 6 月 13 日
編集済み: Matt J
2024 年 6 月 13 日
It is not clear what your optimization problem is intended to be. Do you have a single optimization problem where ratio is supposed to be a scalar unknown constrained to the interval [1.0,1.2] and which depends on all A(i),B(i) simultaneously? If so, then sum(A.*ratio - B) is also a scalar quantity, and it is not clear why you would be applying min() to it.
If instead you intended to have a different ratio(i) for i=1,...,10 and solve 10 separate optimization problems, then the approach is entirely wrong, but a clearer description of the optimization problem is needed first.
回答 (1 件)
John D'Errico
2024 年 6 月 13 日
編集済み: John D'Errico
2024 年 6 月 13 日
I think your problem is you are confused. And your confusion turns into a highly confusing question. Questions like is the optimization a linear domain or a plane are meaningless. I'm sorry, but that is just a word salad, a juxtaposition of vaguely related words, but with no mathematical content in it. Lets look at the code you wrote.
The line of code:
fun = @(ratio) min(sum(A.*ratio - B));
has ratio as either a scalar, or a vector of length the same as A. Worse, that line itself shows a misunderstanding of what the function min does. I could talk about that later on. But I think because you wanted to compute the minimum of something, you threw in the function min for no valid reason.
Then you write:
ratioRange = [1.0,1.2];
ratio = fminsearch(fun,ratioRange);
And I think here, you seem to think that ratioRange represents the LIMITs or bounds on the ratio, so a lower and upper bound of 1 and 1.2. I interpret it that way because of the name of that variable. But that is NOT how fminsearch will interpret what you wrote. Fminsearch does not accept bounds.
This is not a question of finding a more appropriate function than fminsearch, but of understanding what you are looking to do.
I THINK your problem here is to find the SCALAR variable ratio, that makes the vector A look as much like the vector B as possible. (What some might describe as a classic calibration problem.) And you have provided limits on what you think that ratio might be. This is the only thing that makes sense of what you have said. We have two vectors A and B. I'll choose something that may be a little easier to visualize.
A = sort(rand(1,25)); % proxy for measured values
B = 1.25*A + randn(size(A))/10; % proxy for independent measured values
So my expectation is, the desired ratio would be approximately 1.25. But the vector B is pretty noisy.
Next, write a function that computes the difference. Note that fun was itself garbage. Sorry, but it was. You need to form the difference of the vectors A*ratio and B, then compute some SCALAR variable that measures the size of that difference. Here are a few alternatives:
myfun1 = @(ratio) norm(A*ratio - B); % The 2-norm of the difference between the vectors
myfun2 = @(ratio) sqrt(sum((A*ratio - B).^2)); % another way to compute the 2-norm
myfun3 = @(ratio) norm(A*ratio - B,1); % The 1-norm of the difference between the vectors
myfun4 = @(ratio) sum(abs(A*ratio - B)); % another way to compute the 1-norm
Note that NONE of these function handles ever uses the function min. Min is meaningless at th1s point.
Next, do you see that myfun2 is the sum of the squares of the differences between vectors? norm does that for you. as a self contained function. Lets try each of those function handles to see if they make sense. We expect that for any value of ratio, myfun1 and myfun2 should be the same result. And that also applies to myfun3 and myfun4.
myfun1(1.1)
myfun2(1.1)
myfun3(1.1)
myfun4(1.1)
As you can see, they do work. If we plug in different possible values for ratio, we will get different results.
myfun1(1.2)
Now how does an optimizer work? It just tries out different values of the unknown parameter ratio, until it finds a value that minimizes the objective. Yes, it uses some intelligent scheme to send in those test values, but it is just a search tool at heart. And we should expect slightly different results for the 1-norm objective versus the 2-norm objective. Also, there is no need to worry that the size of the objective function for the 1-norm and the 2-norm are greatly different. That is irrelevant.
As well, fminsearch does NOT allow you to supply bounds. But we can try these function out now.
rstart = 1.1;
[rmin1,fval1,exitflag] = fminsearch(myfun1,rstart)
[rmin3,fval3,exitflag] = fminsearch(myfun3,rstart)
So we get slightly different solutions for the 1-norm objective, versus the 2-norm objective. What do those objective functions look like?
fplot(myfun1,[0.25,4],'r')
hold on
fplot(myfun3,[0.25,4],'b')
hold off
grid on
legend('2-norm objective','1-norm objective')
Ignore the warnings, since I did not provide vectorized versions of these functions, and fplot wants to pass in many different ratios all at once. You can see both objectives have different minimum points. You might also see the 1-norm objective function is actually a piecewise linear thing, whereas the 2-norm objective is a smooth function. This is expected.
Finally, again, fminsearch does NOT allow you to provide bounds on where to look. fminbnd does allow that, but remember that fminbnd is only a 1-variable optimizer, whereas fminsearch allows you to solve for multiple unknowns at once.
ratioRange = [1,1.2]
[rminbounded,fvalbounded,exitflag] = fminbnd(myfun1,ratioRange(1),ratioRange(2))
And of course, on this pair of vectors, fminseach found a better solution, with a smaller difference than did fminbnd on that objective, because I made the bounds too tight. You can see from that red curve in the plot, that fminsearch allowed the ratio to go above 1.2, whereas fminbnd was limited. It actually gave up at 1.1999, which is as close as it got.
Anyway, how well did these various solutions work? I'll plot the vectors A and B, then the other potential "solutions" on top, to see how well they worked.
x = 1:numel(A);
plot(x,B,'or',x,A,'og',x,A*rmin1,'sk',x,A*rmin3,'xm',x,A*rminbounded,'pb')
legend('B','A','2-norm','1-norm','bounded 2-norm',location = 'northwest')
They all seem to have adjusted A as well as possible, so that it tries to overlay on top of B. Of course, B was pretty noisy.
Finally, I could add that there are other tools we could have used to estimate the value of ratio. In fact, this problem is a sufficiently simple one that I can think of many alternative ways to solve it. Overall, I think you would benefit from doing some reading online about various optimization tools, and how they would be employed to solve problems, however I am not at all sure where to point you since your question is too confusing. So I cannot gauge what you know.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!