find the least positive root of the equation y=cos(sqrt(abs(polyval(p,x))))-sin(x)/2 with the use of fzero
2 ビュー (過去 30 日間)
古いコメントを表示
what function should i create and what i should do after
0 件のコメント
採用された回答
Star Strider
2016 年 1 月 8 日
編集済み: Star Strider
2016 年 1 月 8 日
Similarly to your earlier Question:
p = polyfit(x,y,7); % Fit Polynomial
P = @(x,p) polyval(p,x);
f = @(x,p) cos(sqrt(abs(P(x,p)))) - sin(x)/2; % Function
f_root = fzero(@(x) f(x,p), 1); % Find Single Root
xv0 = linspace(0, 5, 100); % Vector Of Initial Estimates For Multiple Roots
for k1 = 1:length(xv0) % Find Multiple Roots
f_root_v(k1) = fzero(@(x) f(x,p), xv0(k1)); % Find Vector Of Roots
end
roundn = @(x,n) round(x .* 10.^n)./10.^n; % Round ‘x’ To ‘n’ Digits, Emulates Latest ‘round’ Function
function_roots = unique(roundn(f_root_v, 4)); % Find Unique Roots
EDIT — To find the least positive root, add this line:
LeastPositiveRoot = min(function_roots(function_roots > 0))
0 件のコメント
その他の回答 (1 件)
Walter Roberson
2016 年 1 月 9 日
A difficulty with the linspace approach is that when you ask fzero for a root and supply an initial positive guess, there is no guarantee that the resulting root will be positive even if there are positive roots. For any fixed spacing like linspace(0,5,100) you can come up with a function which will project all of those values to negative roots even though positive roots exist.
It is tempting to use fzero with an interval of eps(realmin) to realmax. The difficulty with that is that fzero requires that the endpoints have opposite signs, which we can certainly not guarantee in general.
You can fzero adjacent intervals over a range hoping for some root, if you check the exitflag. But if the function is sufficiently wiggly then you might still run into the sign-change problem.
Some other methods of finding roots:
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!