Question

I'm trying to figure out how to find a way to find all roots of a function. I know how to find one, depending on initial x's but am stuck as to how to find the all of them, as in the case in my practice problem. What I have is how to find one, but there are three in this case. How can I get to them? Thanks!

   f = @(x) x^3-6*x^2+11*x-6.1;

   xl = 1;
   xu = 3;
   cnt = 0;
   N = 4;
   es = .5*10^(2-N);
   ea = 1;
   n = 1000;



   for i = 1:n
      while ea > es 
         xm = xl(i) + (xu -xl(i)) / n;
         fxl = f(xl(i));
         fxm = f(xm);
         if fxl < 0 < fxm
             if f(xl(i))*f(xm) < 0
                 xu = xm;
             elseif f(xl(i))*f(xm) > 0
                 xl = xm;
             end
         else fxm < 0 < fxl
             if f(xl(i))*f(xm) < 0
                 xu = xm;
             elseif f(xl(i))*f(xm) > 0
                 xl = xm;
             end
       end     
       ea = abs((xu-xl)/xu)*100;
       cnt = cnt + 1;
     end
   end

No correct solution

OTHER TIPS

One of the simplest ways to do it is to split the region you're searching into subregions, find regions that have endpoints with opposite signs, and then do a more refined search (like you've already written) on the qualifying subregions. I'm not familiar with MATLAB specifically, but here's some Python-flavored pseudocode. a is the lower bound, b the upper bound, and f the function:

a = 1
b = 3
for i in range(1000):            # Loops 1000 times
    nextval = a + (b - a)/1000   # Increments by 1/1000 of the region
    fa = f(a)
    fnext = f(nextval)
    if fa < 0 < fnext:
        # do more refined search
    elif fnext < 0 < fa:
        # do more refined search
    a = nextval     # Moves <a> forward so a new region is checked on the next iteration

If you're into polynomials specifically, you may be able to solve them exactly. See this page http://en.wikipedia.org/wiki/Cubic_function for more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top