I have an equation with 6 variables. I loop through and substitue for 5 of these variables. Each time I would like to find the value of the sixth variable that makes this equation minimum.
I am using fminsearch which means that I need to assign a function handle to the equation mentioned above. So I use matlabFunction or inline and the fminsearch works fine. However this takes a long time. Running the profiler I find that these functions take 25-35% of the total time. This is an issue because I am using the results from the fminsearch further in a cost function using fmincon. This makes the entire program quite slow.
My question is:
Is there any other function that one can use instead of inline or matlabFunction?
Is there another way to do this?
Thanks in advance
Aruna
That said, it sounds to me as if you are doing a one-dimensional
minimization. I believe that fminbnd is more efficient than fminsearch
at one-dimensional minimizations, if you can supply an interval where
the minimum is sure to reside.
Even more, it sounds as if you have the Optimization Toolbox. Why not
use fminunc? You can even use matlabFunction in conjunction with
jacobian to supply fminunc with the gradient of your objective function
as shown here:
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1
I further believe that matlabFunction is an efficient way to evaluate
symbolic expressions numerically. You might be able to code the
expressions more efficiently by hand, but that is time-consuming and
error-prone.
Alan Weiss
MATLAB mathematical toolbox documentation
For example,
syms a b c d f g
myhandle = matlabFunction(a^2 - b*c + cos(d-f/(g^2+1)))
myhandle =
@(a,b,c,d,f,g)cos(d-f./(g.^2+1.0))-b.*c+a.^2
xmin = fminbnd(@(x)myhandle(x,2,3,4,5,6),-5,3)
xmin =
7.6328e-017
xmin2 = fminbnd(@(x)myhandle(1,2,3,x,2,1),-5,7)
xmin2 =
-2.1416
I called matlabFunction just once. Then I minimized over the first
variable in the expression, setting b,c,d,f,g to 2,3,4,5,6 respectively.
Then I minimized over the fourth variable (d) setting a,b,c,f,g to
1,2,3,2,1 respectively.
I hope this helps,
Alan Weiss
MATLAB mathematical toolbox documentation
thanks for the really prompt response.I see what you are saying. I should apologise as i am a new Matlab user.
I need to better explain what I am doing. I use the fminsearch to find the valu of the variable tha minimizes the function and then substitue it back into the function to calculate "Estimated" values. These are then used in a cost function. The cost function is optimized for a set of 6 variables two of which have upper bounds and lower bounds (which is why I am using the fmincon function). For every call of the fmincon function, the fminsearch is called and thus the matlabFunction before it. My goal is to get the fastest result (time wise) from the program and it seems like the matlabFunction is taking a long time. I was wondering if there was any way to get a faster result.
thanks much
aruna
Thanks again
Aruna
I believe that you will get better results if you do not use fminsearch,
but instead use fminunc, which is a more efficient solver.
I do not understand why you use both fmincon and fminsearch. If you are
doing a minimization, why not combine everything into one minimization,
why do it in sequence?
I also do not understand why you say you "...use the fminsearch to find
the value of the variable that minimizes the function and then
substitute it back into the function to calculate 'Estimated' values."
fminsearch will supply you with the value, no need to substitute:
[x fval] = fminsearch(...)
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Thanks for your help. I had the matlabFunction call defined in a function that was being optimised resulting in it (matlabFunction) being called more than once. That's sorted now and the total time has reduced considerably. Thanks again.
Having worked for a number of software companies, I have to say that your response was extremely prompt and helpful. I am really impressed.
Aruna
Alan Weiss <awe...@mathworks.com> wrote in message <gu1kpq$t1a$1...@fred.mathworks.com>...
Alan Weiss
MATLAB mathematical toolbox documentation
"Aruna V" <arun...@gmail.com> wrote in message <gu943q$cva$1...@fred.mathworks.com>...