Fastest way to minimize a function
14 views (last 30 days)
Show older comments
I have to perform many minimizations of a function, so I want to do it quickly. Here is the problem: given different values of T, the minimization should find L = ((g*T^2)/(2*pi))*tanh(2*pi*D/L) where g and D are constants. Below is how I am doing it, but is there a faster way?
g = 9.8066 ;
D = 50 ;
periodList = linspace(3, 15, 100) ;
fh = @(L,T) (L-((g*T.^2)/(2*pi))*tanh(2*pi*D/L)) ; % Handle to L as a function of T
for iPeriod = 1:length(periodList)
T = periodList(iPeriod) ;
guessL = g*(T^2)/(2*pi) ; % Initial guess at L
L(iPeriod) = fzero(@(L) fh(L, T), guessL) ;
end
Since this problem is finding the wavelength L of water waves given the wave period T and water depth D, there are some constraints on the problem: L must always be positive with a nominal solution accuracy of +/-1 meter, the period T will lie between 0 and ~20 seconds, and the water depth D ranges from ~10 to 1000 meters. g is the acceleration due to gravity, so it will always be 9.8066 m/s^2.
3 Comments
Sean de Wolski
on 10 Feb 2012
ga is definitely not faster, fmincon might be but probably not. But they're significantly more powerful.
Accepted Answer
Walter Roberson
on 10 Feb 2012
Unfortunately the solutions to your function suffer from sufficient arithmetic problems as to make minimization not of value: there are upper and lower bounds beyond which all L values produce the same result (different for the upper and lower bound though.)
The number of real roots of L vary considerably; with the test values I was using for g and D, I found up to 45 real roots. Unfortunately due to precision issues, the roots were indistinguishable except that one of them was the positive value corresponding to the other (negative) values.
With the constants you show above, all of the really interesting behavior occurs between T=0 and T=2, with some semi-interesting behavior up to T=5, and boring behavior after that. The positive L values increase as T increases -- and contrawise, the negative L values corresponding get more negative, so if fsolve() happens to hit the negative root, you will find the numbers becoming more negative (more minimum) as T increases. If you want the T that has the minimum L, then you might as well just choose the upper bound of your T range once T has gotten large enough that your results are dominated by floating point round-off. By the way, if you work matters through from the point of view of round-off then the best initial guess is the exact negative of the guess calculation you show.
I am having my system chase some obscure behavior near T=2; I do not know yet whether it will be meaningful or not.
6 Comments
Walter Roberson
on 13 Feb 2012
The missed roots show up most clearly in about the area of T=Pi .
The number of roots identified varies wildly with very small changes in T, from 1 up to (at least) 72. Nasty precision problems.
More Answers (0)
See Also
Categories
Find more on Linear Least Squares in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!