How to use fminsearch for vectors (2024)

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.

As you can see, they do work. If we plug in different possible values for ratio, we will get different results.

myfun1(1.2)

ans = 0.4976

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)

rmin1 = 1.2567

fval1 = 0.4661

exitflag = 1

[rmin3,fval3,exitflag] = fminsearch(myfun3,rstart)

rmin3 = 1.2424

fval3 = 1.7711

exitflag = 1

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')

Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.

hold on

fplot(myfun3,[0.25,4],'b')

Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.

hold off

grid on

legend('2-norm objective','1-norm objective')

How to use fminsearch for vectors (1)

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]

ratioRange = 1x2

1.0000 1.2000

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

[rminbounded,fvalbounded,exitflag] = fminbnd(myfun1,ratioRange(1),ratioRange(2))

rminbounded = 1.1999

fvalbounded = 0.4976

exitflag = 1

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')

How to use fminsearch for vectors (2)

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.

How to use fminsearch for vectors (2024)

References

Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6748

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.