Fastest way to minimize a function
显示 更早的评论
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 个评论
Sean de Wolski
2012-2-10
fmincon() or ga() could both be your friends.
K E
2012-2-10
Sean de Wolski
2012-2-10
ga is definitely not faster, fmincon might be but probably not. But they're significantly more powerful.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Linear Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!