วันอังคารที่ 6 กรกฎาคม พ.ศ. 2553

BiSection Method By MathLAB

อันนี้เป็นการหาrootของfunction โดยใช้วิธี BiSection Methodในการหาroot และทำการตรวจสอบfunctionที่ไม่เป็น continuous function ด้วย ซึ่งnot continuous function ก็จะมีลักษณะอย่างในภาพตัวอย่าง ซึ่งจะทำให้เราหาrootของfunctionไม่ได้นะคับ

image1 image2

ลักษณะของ function ที่เราสามารถหาrootได้จะต้องเป็น continuous function เท่านั้น โดยเราจะต้องกำหนด boundary เอาไว้ด้วยตัวอย่างเช่น

image3

เราสามารถหา root ของfunction ได้ โดยทำการกำหนด boundary เอาไว้ด้วย มาดูโปรแกรมตรวจสอบกันคับ (อันนี้ผมเขียนกับ MathLAB)

clear all;
close all;
clc;
f = inline('x^3+x^2+x-1');
a = -0.5;
b = 1;
tol1 = 0.0001;
tol2 = 0.0001;
flags = false;
max1=1+round(log(a-b)-log(tol1)/log(2));
fm = zeros(1,abs(max1));
if (f(a)*f(b) == 0)
    if (f(a) == 0)
        root = a;
    else
        root = b;
    end
else
    lamda = abs((a+b)/20);
    if f(a)<f(b)
       if f(a)<f(a+lamda)
          disp ('Continuous function')
          flags = true;
       else
          disp ('Not continuous function')
       end
    elseif f(b)<f(a)
       if f(b)<f(b-lamda)
          disp ('Continuous function')
          flags = true;
       else
           disp ('Not continuous function')
       end
    end
    count = 0;
end
if(flags)
   for j = 1:max1
      m = (a+b)/2;
      fm(j) = f(m);
      [j a b m f(m)]
      if(f(a)*f(m) > 0)
          a = m;
      else
          b = m;
      end
      if (b-a)/2<tol1
          flags = false;
          root = m;
      end
   end
   fm_greater_than_zero = [fm(find(fm>0))]
   fm_less_than_zero = fm(find(fm<0))
end

ไม่มีความคิดเห็น:

แสดงความคิดเห็น