MATLAB Answers

trying with bisection method to create a function that finds the root of an equation, approximated error, and numbers of iteration

377 ビュー (過去 30 日間)
XIN SHEN
XIN SHEN 2016 年 10 月 4 日
コメント済み: XIN SHEN 2016 年 10 月 7 日
So i did my code in the following way shown below; however, the way that my codes decided to stop the loop and output the root is by comparing if F(xr)==0 or not; however, i want to add another term which is the "approximated error" term that is to compare with a desired maximum error. So, once the Ea <= Emax, the loop is ended. The Ea should be calculated as (current approximation - previous approximation)/ current approximation; however, this is the part i do not know how to code. Could you help me with it? Any help would be really appreciated!! Thanks.
function root= bisection(fun, lb, ub,iter_max)
flb=fun(lb); % find the f(lb) value
fub=fun(ub); % find the f(ub) value
i=0; % the initial iteration
%now to test if there is a root within the uppper and lower limits
if flb*fub>0
disp('termination type 1: there is no root within bracket')
end
for i= 1: iter_max
i= i+1;
xr= (lb+ub)/2;
fxr=fun(xr);
if i >= iter_max
disp('termination type 0: algorithm terminated due to maximum interations')
break
elseif fxr==0 % test if this is the case where xr is directly the root
root= xr;
disp('this is the root')
break
end
% if none of above is met, then process the commands below
if fxr*flb>0
lb=xr;
else
ub=xr;
end
% after 1 loop, the program jumps back to the beginning
end
fprintf('The iterations taken is: %d', i)

  0 件のコメント

サインインしてコメントする。

採用された回答

elias GR
elias GR 2016 年 10 月 4 日
A first problem in your code is that the returned variable "root" does not always take a value. A second one is the "i= i+1;". i is the index of the loop so there is no need to increase it. Furthermore it would be good to test also if one of the initial limits is the root. An example of what you want do to is the following.
function root= bisection(fun, lb, ub,iter_max, Emax)
flb=fun(lb); % find the f(lb) value
fub=fun(ub); % find the f(ub) value
i=0; % the initial iteration
%Check if you have already found the solution
if flb==0
root=lb;
fprintf('The root is: %5.3f \n', root)
return
end
if fub==0
root=ub;
fprintf('The root is: %5.3f \n', root)
return
end
%now to test if there is a root within the uppper and lower limits
if flb*fub>0
disp('termination type 1: there is no root within bracket')
root=NaN;
return
end
%solution guess
root= (lb+ub)/2;
for i=1:iter_max
rootPrevious=root;
fxr=fun(root);
if i >= iter_max
disp('termination type 0: algorithm terminated due to maximum interations')
break
elseif fxr==0 % test if this is the case where root is directly the root
disp('this is the root')
break
end
% if none of above is met, then process the commands below
if fxr*flb>0
lb=root;
else
ub=root;
end
root= (lb+ub)/2;
error=abs((root-rootPrevious)/root);
if error<Emax
break
end
end
fprintf('The iterations taken is: %d \n', i)
fprintf('The root is: %5.3f \n', root)

  1 件のコメント

XIN SHEN
XIN SHEN 2016 年 10 月 7 日
Sorry for this late reply! And thank you! I really appreciate your help which literally gives me a lot of information!!!!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by