Wednesday, September 21, 2016

matlab code:false position method

false position method:


Simulation in MATLAB Programming environment:

clc
clear
f=@(x)x^2-4*x+1;
for i=1:10
    x1=input('x1=');
    x2=input('x2=');
    f1=f(x1);
    f2=f(x2);
    if f1*f2<0
        break;
    end
end
disp('        i          x1        f1      x2      f2        x3       f3')
for i=1:10
    f1=f(x1);
    f2=f(x2);
    x3=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1));
    f3=f(x3);
   disp([i     x1       f1       x2      f2      x3       f3])
   if f1*f3<0
       x2=x3;
   else
       x1=x3;
   end
end
x3

MATLAB Running:

x1=0
x2=1
        i          x1        f1      x2      f2        x3       f3
    1.0000         0    1.0000    1.0000   -2.0000    0.3333   -0.2222
    2.0000         0    1.0000    0.3333   -0.2222    0.2727   -0.0165
    3.0000         0    1.0000    0.2727   -0.0165    0.2683   -0.0012
    4.0000         0    1.0000    0.2683   -0.0012    0.2680   -0.0001
    5.0000         0    1.0000    0.2680   -0.0001    0.2680   -0.0000
    6.0000         0    1.0000    0.2680   -0.0000    0.2679   -0.0000
    7.0000         0    1.0000    0.2679   -0.0000    0.2679   -0.0000
    8.0000         0    1.0000    0.2679   -0.0000    0.2679   -0.0000
    9.0000         0    1.0000    0.2679   -0.0000    0.2679   -0.0000
    10.0000         0    1.0000    0.2679   -0.0000    0.2679   -0.0000
x3 =
 0.2679 >>    

No comments:

Post a Comment