Finding all roots of nonlinear function
Show older comments
How would I find all the roots of the function 

I know all sine functions have multiple roots and so this function may also have multiple roots but how would I find all these roots? Using matlab fzero which uses the brackect criteria for the existence of solution only gives one root to the function.
So plotting the function might help but I think I have problems understanding what the graph is telling me. Can someone help to interpret the graph for me? How would I know the root(s) of the function? Atleast, I know one root is 0.25957 using fzero.
fplot( @(x) sin(10*x)-2*x, [-.5,.5])
%ploting the function with interval [-5, 5]
grid on

Answers (1)
Bjorn Gustavsson
on 4 Mar 2021
1 vote
Read the help for the fzero function and you will see that it returns a solution, not all. It accepts a range for x0 that restricts the region where it searches for the solution. Use that to work in a systematic way.
8 Comments
Hmm!
on 5 Mar 2021
Bjorn Gustavsson
on 5 Mar 2021
You take a look at the function. You have a think about how many regions you need to use to be guaranteed to find all the solutions, then you specify those regions, then you run fzero with all those regions.
Hmm!
on 5 Mar 2021
Bjorn Gustavsson
on 5 Mar 2021
Simply select regions that snuggly brackets the zero-crossings of the function - that way you'll get the zero inside each region. For this function it is simple since -2*x is a constantly decreasing function of x and sin(10*x) is bounded between -1 and 1 - therefore we know that once |x| is large enough we wont get any more zeros. However, fzero will only ever give us one at a time:
regs = [-.3,-.2;-.1,.1;.1,.3]; % therefore make a set of regions where f is crossing zero only once
for i1 = 3:-1:1,
sol(i1) = fzero(@(x) sin(10*x)-2*x,regs(i1,:));
end
disp(sol)
% -0.2596 0 0.2596
This will obviously be tedious for functions with many zeros. But it's possible to make something that tediously searches for larger number of solutions - note not all solutions in the general case (for example sin(1./x) in the region [0 1]).
HTH
John D'Errico
on 5 Mar 2021
Is that a technical term: "snuggly brackets"? The thought of it makes me feel all warm and cuddly. :) :) A great phrase.
Hmm!
on 5 Mar 2021
Bjorn Gustavsson
on 8 Mar 2021
Yes, possibly. If you specify a region with 2 components then the function needs to have different signs at the 2 end-points, if you select regions snuggly around a zero you will get that root. If you have an odd number of solutions in that region you will get one of them, I don't know which that will be in a general case.
You an also draw three additional lines in your plot:
fplot( @(x) sin(10*x)-2*x, [-2,2])
%ploting the function with interval [-5, 5]
grid on
hold on
fplot(@(x) -2*x+1,[-2 2])
fplot(@(x) -2*x-1,[-2 2])
fplot(@(x) sin(10*x+sqrt(2))-2*x,[-2 2])
From those three curves you can also conclude that this function only has those 3 zeros.
@John D'Errico: If you help me then maybe we can make it a technical term! (From my name and lack of spelling skills you must have guessed that "the language of the Bard" is not my native tounge - but I derive great joy from cobbling up terms that you natives find quirky...)
Categories
Find more on Get Started with MATLAB 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!
