clc;
clear;
% Shows the points obtained by the Bisection Method when
% solving the equation func(x)=0. The two initial points
% are supplied, along with the number of required iterations.
% It is assumed that the initial interval is "good".
% Of course, in practice one should not set beforehand the
% number of iterations, but rather stop when reaching a
% sufficiently small neighbourhood of the root.
iterations=1000;
left=0;
right=10;
func=@(x) 4*(x-2)^3+4*x-12;
orbit=[];
for i=1:iterations
disp('======')
disp('itteration')
i
disp('left');
left
disp('right');
right
disp('feval left')
feval(func,left)
disp('feval right')
feval(func,right)
disp('middle')
middle=(left+right)/2
disp('feval middle')
feval(func,middle)
if feval(func,left)*feval(func,middle)>0 left=middle; disp('left*midd>0');
else right=middle; disp('left*midd<=0')
end
orbit=[orbit,middle];
end
orbit
0 Comments:
Post a Comment
<< Home