Finding multiple roots of a function without using matlab's built-ins?

StackOverflow https://stackoverflow.com/questions/22161923

  •  19-10-2022
  •  | 
  •  

문제

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

올바른 솔루션이 없습니다

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top