[CENTER]لاظهار تطور الحل في الجدول
نبدل كود الدالة الى مايلي
function [p]=MyBiSection(f,a,b,Tol,N)
% Check if f(a)=0 "a is the root"
if f(a)==0,p=a;return;end
% Check if f(b)=0 "b is the root"
if f(b)==0,p=b;return;end
% Check if f(a)*f(b)<0
if (sign(f(a))==sign(f(b)))
disp('f(a) and f(b) have the same sign');
p=NaN;
return;
end
% Routine
str=10;
disp('N----------|An---------|Bn---------|Pn---------|Fn---------|');
i=1;
Fa=f(a);
p=(a + b)/2;
fprintf('%*.0f |%*.4f |%*.4f |%*.4f |%*.4f |
', str, i,str,a,str,b,str,p,str,f(p));
while i<N
i=i+1;
p=(a + b)/2;
if (f(p)==0 ||(b-a)*0.5<Tol)
fprintf('%*.0f |%*.4f |%*.4f |%*.4f |%*.4f |
', str, i,str,a,str,b,str,p,str,f(p));
return
end
if sign(f(a))==sign(f(p))
Fa=f(p);
a=p;
else
b=p;
end
fprintf('%*.0f |%*.4f |%*.4f |%*.4f |%*.4f |
', str, i,str,a,str,b,str,p,str,f(p));
end
fprintf('
Method failed after %d iterations
',N);
p=NaN;
لما نجرب مثلا
clear all; close all;clc
% Function
f=inline('exp(x)-3*x');
% Inputs
a=0; % Endpoint
b=1; % Endpoint
No=20; % Max iterations
Tol=0.001; % Tolerance
%
% % Call of our function
[p]=MyBiSection(f,a,b,Tol,No);
النتيجة تكون
![](http://www.almohandes.org/vb/attachment.php?attachmentid=9291&stc=1&d=1366593619)
لكن مثلا لو نحدد
clear all; close all;clc
% Function
f=inline('exp(x)-3*x');
% Inputs
a=0; % Endpoint
b=1; % Endpoint
No=9; % Max iterations
Tol=0.001; % Tolerance
%
% % Call of our function
[p]=MyBiSection(f,a,b,Tol,No);
يعني نسمح فقط ب 9 محاولات،
الطريقة ستفشل
لانها لم تصل للدقة المطلوبة وانتهى عدد محاولاتها
![](http://www.almohandes.org/vb/attachment.php?attachmentid=9292&stc=1&d=1366593745)
منه نقدر نرفع في قيم عدد المحاولات
مع المحافظة على نفس الدقة
لنصل الى عدد المحاولات الادنى اللازم لهذه الدقة
والذي ستنجح معه الطريقة،
اي محاولات بعده، تعتبر زائدة
[/center]