Writing a function that returns all the roots of any function within a certain range?

19 views (last 30 days)
Hi all,
I'm just getting started with MATLAB and coding in general so don't understnad many of the basics.
My task here is to to have function handle "f" and two-element range vector "r" return all the the roots of f(x) = 0 in the range r=[x0,x1], and also plot them. I know using the fzero() function will probably be useful here, there should be a for/while loop somewhere, and that nargin should be used somewhere, but I have no idea where to start... Any help would be much appreciated, thanks!!
(A good example function would be x^4 – 16x^3 + 86x^2 – 176x + 105 = 0 with roots at 1, 3, 5, and 7)
  1 Comment
James Tursa
James Tursa on 21 Sep 2019
Look at sin(1/x) between 0 and 1 and you will see that your requirements are impossible. Maybe you should restrict yourself to polynomials?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 20 Sep 2019
"Writing a function that returns all the roots of any function within a certain range?"
That is known to be impossible in continuous domains. It is trivial to define a function whose roots cannot all be returned:
f(x) = { 1 if x is rational
0 if x is irrational }
There are an infinite number of irrational numbers between any two rational numbers, so finding them all would take an infinite amount of time.
It can also be shown through Goedelization or Turing arguments that there are some functions which have roots which cannot be predicted. With an infinitely long binary fraction, you can encode any logic proposition, including propositions equivalent to "This number is unprovable", which will be a true proposition (a root) but it is not provable that the number is a root within the mathematics of the system, and further more that such numbers will always exist no matter how you augment the system.
If you are willing to restrict yourself to finite discrete domains, then you cannot solve easy tasks such as x^2 - 2 : there is no finite discrete number x such that x^2 == 2.
Sometimes you can define precisions and demonstrate that for all x in the range a-eps(a) to a+eps(a) that abs(f(x)-f(a)) <= tolerance and abs(f(a) - target) < tolerance -- that all the values within the numeric precision of f(a) are within tolerance of being roots of the equation f(x) == target . In such a case, you can say in one sense that a is a root of the equation, but really all you are demonstrating is that there is at least one root in that range: you are not proving that there are no other roots in that range.
But if you want to work numerically, what you can do is:
LB = typecast(LowerBound, 'uint64');
UB = typecast(UpperBound, 'uint64');
for X = LB : UB
Xd = typecast(X, 'double');
if abs(f(Xd)) <= tolerance
add Xd to the list of roots
end
end
What this does is test EVERY representable number in the range LowerBound to UpperBound . This is necessary because there are functions for which it can be proven that no amount of knowledge about where the other roots are can tell you whether a given number is a root before testing that number.

Categories

Find more on Polynomials in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!