MATLAB: Find the least positive root of the equation y=cos(sqrt​(abs(polyv​al(p,x))))​-sin(x)/2 with the use of fzero

fzero

what function should i create and what i should do after

Best Answer

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))