Sangat baik kalau saudara memodifikasi program (salah satunya adalah melibatkan unsur
error), sehingga menghasilkan tampilan yang lebih lengkap dan lebih mudah dipahami.
function rtn=bisec(fx,xa,xb,n)
x=xa; fa=fx(xa);
x=xb; fb=fx(xb);
for i=1:n;
xc=(xa+xb)/2; x=xc; fc=fx(xc);
X=[i,xa,xb,xc,fc];
disp(X);
if fc*fa<0
xb=xc;
else xa=xc;
end
end
>> fx=inline('3*x+sin(x)-exp(x)')
fx =
Inline function:
fx(x) = 3*x+sin(x)-exp(x)
>> fplot(fx,[0 2]); grid on
>> fx=inline('3*x+sin(x)-exp(x)');
>> bisec(fx,0,1,13)
1.0000
0
1.0000
0.5000
0.3307
2.0000
0.5000
0.2500
-0.2866
3.0000
0.2500
0.5000
0.3750
0.0363
4.0000
0.2500
0.3750
0.3125
-0.1219
5.0000
0.3125
0.3750
0.3438
-0.0420
6.0000
0.3438
0.3750
0.3594
-0.0026
7.0000
0.3594
0.3750
0.3672
0.0169
8.0000
0.3594
0.3672
0.3633
0.0071
9.0000
0.3594
0.3633
0.3613
0.0023
10.0000
0.3594
0.3613
0.3604
-0.0002
11.0000
0.3604
0.3613
0.3608
0.0010
12.0000
0.3604
0.3608
0.3606
0.0004
13.0000
0.3604
0.3606
0.3605
0.0001
function rtn=bisec1(fx,xa,xb,n)
x=xa; fa=fx(xa);
x=xb; fb=fx(xb);
for i=1:n;
xc=(xa+xb)/2; x=xc; fc=fx(xc);
X=[i,xa,xb,xc,fc];
disp(X);
if fc*fa<0
xb=xc;
else xa=xc;
end
end
xa
xb
n
r=(xa+xb)/2
>> bisec1(fx,0,1,13)
1.0000
0 1.0000
0.5000
0.5000
0.3307
2.0000
0.2500 -0.2866
3.0000
0.2500
0.5000
0.3750
4.0000
0.2500
0.3750
0.3125 -0.1219
5.0000
0.3125
0.3750
0.3438 -0.0420
6.0000
0.3438
0.3750
0.3594 -0.0026
7.0000
0.3594
0.3750
0.3672
0.0169
8.0000
0.3594
0.3672
0.3633
0.0071
0.0363
9.0000
0.3594
0.3633
0.3613
0.0023
10.0000
0.3594
0.3613
0.3604 -0.0002
11.0000
0.3604
0.3613
0.3608
0.0010
12.0000
0.3604
0.3608
0.3606
0.0004
13.0000
0.3604
0.3606
0.3605
0.0001
xa =
0.3604
xb =
0.3605
n=
13
r=
0.3604