Monday, September 19, 2016

matlab code for numerical analysis=bi-section method

islami bank scholarship

Simulation in MATLAB Programming environment:

clc
clear
f=@(x) 30-15*(1/exp(.5*x));
e=0.001;
for k=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:20
     f1=f(x1);
  f2=f(x2);
    x3=(x1+x2)/2;
    f3=f(x3);
    disp([i     x1     f1     x2      f2     x3      f3])
    if f1*f3<0
        x2=x3;
    else
        x1=x3;
    end
end
root=x3

MATLAB Running:

x1=-1
x2=-2
    i            x1         f1        x2        f2        x3       f3
    1.0000   -1.0000    5.2692   -2.0000  -10.7742   -1.5000   -1.7550
    2.0000   -1.0000    5.2692   -1.5000   -1.7550   -1.2500    1.9763
    3.0000   -1.2500    1.9763   -1.5000   -1.7550   -1.3750    0.1689
    4.0000   -1.3750    0.1689   -1.5000   -1.7550   -1.4375   -0.7780
    5.0000   -1.3750    0.1689   -1.4375   -0.7780   -1.4063   -0.3008
    6.0000   -1.3750    0.1689   -1.4063   -0.3008   -1.3906   -0.0650
    7.0000   -1.3750    0.1689   -1.3906   -0.0650   -1.3828    0.0522
    8.0000   -1.3828    0.0522   -1.3906   -0.0650   -1.3867   -0.0064
    9.0000   -1.3828    0.0522   -1.3867   -0.0064   -1.3848    0.0229
   10.0000   -1.3848    0.0229   -1.3867   -0.0064   -1.3857    0.0083
   11.0000   -1.3857    0.0083   -1.3867   -0.0064   -1.3862    0.0010
   12.0000   -1.3862    0.0010   -1.3867   -0.0064   -1.3865   -0.0027
   13.0000   -1.3862    0.0010   -1.3865   -0.0027   -1.3864   -0.0009
   14.0000   -1.3862    0.0010   -1.3864   -0.0009   -1.3863    0.0000
   15.0000   -1.3863    0.0000   -1.3864   -0.0009   -1.3863   -0.0004
   16.0000   -1.3863    0.0000   -1.3863   -0.0004   -1.3863   -0.0002
   17.0000   -1.3863    0.0000   -1.3863   -0.0002   -1.3863   -0.0001
   18.0000   -1.3863    0.0000   -1.3863   -0.0001   -1.3863   -0.0000
   19.0000   -1.3863    0.0000   -1.3863   -0.0000   -1.3863    0.0000
   20.0000   -1.3863    0.0000   -1.3863   -0.0000   -1.3863   -0.0000
root =
 -1.3863

No comments:

Post a Comment