find the least positive root of the equation y=cos(sqrt(abs(polyval(p,x))))-sin(x)/2 with the use of fzero
4 views (last 30 days)
Show older comments
andreas tziolas
on 8 Jan 2016
Answered: Walter Roberson
on 9 Jan 2016
what function should i create and what i should do after
0 Comments
Accepted Answer
Star Strider
on 8 Jan 2016
Edited: Star Strider
on 8 Jan 2016
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 Comments
More Answers (1)
Walter Roberson
on 9 Jan 2016
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 Comments
See Also
Categories
Find more on Whos 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!