find the least positive root of the equation y=cos(sqrt(abs(polyval(p,x))))-sin(x)/2 with the use of fzero
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
andreas tziolas
el 8 de En. de 2016
Respondida: Walter Roberson
el 9 de En. de 2016
what function should i create and what i should do after
0 comentarios
Respuesta aceptada
Star Strider
el 8 de En. de 2016
Editada: Star Strider
el 8 de En. de 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 comentarios
Más respuestas (1)
Walter Roberson
el 9 de En. de 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 comentarios
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!