How would I sum a function and use fzero?
4 次查看(过去 30 天)
显示 更早的评论
I want a function to look like this y=sin(t1-T)+sin(t2-T)+sin(t3-T)+...+sin(tn-T) and use the fzero to find T. How would I go about this? Thank you in advance.
0 个评论
回答(3 个)
Roger Stafford
2017-10-22
编辑:Roger Stafford
2017-10-22
Using ‘fzero’ on that particular problem is needlessly inefficient. You can use ‘atan2’ and ‘asin’ instead.
cn = cos(t1)+cos(t2)+...+cos(tn);
sn = sin(t1)+sin(t2)+...+sin(tn);
p = atan2(sn,cn);
as = asin(y/sqrt(sn^2+cn^2));
T1 = p-as; % One solution (in radians)
T2 = p+as-pi; % Another solution (in radians)
Also any multiple of 2*pi added or subtracted from T1 or T2 is a solution. (Note that the inequality y^2<=sn^2+cn^2 must be true for a solution to exist.)
Andrei Bobrov
2017-10-22
f = @(T)sum(sin(t(:) - T))
fzero(f,.5)
1 个评论
J. Nash
2017-10-22
Wow this is even shorter. Helps a lot since I have around 1000 lines of code. Many thanks for making my code easier.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!