Sie sind auf Seite 1von 6

bisectExample

Contents
G G G G G G G

using bisection to determine zero For demonstration purposes set stopping criteria Calculate new root and update endpoints Calculate new root and update endpoints Calculate new root and update endpoints Calculate new root and update endpoints Calculate new root and update endpoints

f=@(c) (667.38 ./ c) .* (1-exp(-0.146843*c)) - 40; c = 4:.1:20; plot(c,f(c)) grid title('Determinng drag coefficient') xlabel('c') ylabel('f(c)')

file:///H|/00 Quick Links/00 Fall 2009/00 EGR 274/chapter 5 handouts/html/bisectExample.html (1 of 6)9/18/2009 8:25:24 AM

bisectExample

using bisection to determine zero xl=12; xu=16; i=0; true_value = 14.7802; For demonstration purposes set stopping criteria es = 1 % 1 percent

es = 1

Calculate new root and update endpoints


file:///H|/00 Quick Links/00 Fall 2009/00 EGR 274/chapter 5 handouts/html/bisectExample.html (2 of 6)9/18/2009 8:25:24 AM

bisectExample

i=i+1; display(['iteration ' num2str(i)]) xr = (xl + xu)/2; %calculate new root if f(xr)*f(xl) > 0, xl=xr; else xu=xr; end; et = 100*abs((true_value - xr)/true_value);

%update endpoint %determine true error

% print results fprintf('\nNew root guess xr = %f\n',xr) fprintf('Next bracketing points xl = %f\txu = %f\n',xl,xu) fprintf('et = %.1f%%\n',et) xrold = xr;

iteration 1 New root guess xr = 14.000000 Next bracketing points xl = 14.000000 et = 5.3% Calculate new root and update endpoints i=i+1; display(['iteration ' num2str(i)]) xr = (xl + xu)/2; %calculate new root if f(xr)*f(xl) > 0, xl=xr; else xu=xr; end;

xu = 16.000000

%update endpoint

et = 100*abs((true_value - xr)/true_value); %determine true error ea = 100*abs((xr-xrold)/xr); % determine approximate realtive error % print results fprintf('\nNew root guess xr = %f\n',xr) fprintf('Next bracketing points xl = %f\txu = %f\n',xl,xu) fprintf('et = %.1f%%\tea = %f%%\n',et,ea) %check stopping criteria if ea<es, fprintf('******* DONE: ea is less than es ***********'),end xrold = xr;

file:///H|/00 Quick Links/00 Fall 2009/00 EGR 274/chapter 5 handouts/html/bisectExample.html (3 of 6)9/18/2009 8:25:24 AM

bisectExample

iteration 2 New root guess xr = 15.000000 Next bracketing points xl = 14.000000 et = 1.5% ea = 6.666667% Calculate new root and update endpoints i=i+1; display(['iteration ' num2str(i)]) xr = (xl + xu)/2; %calculate new root if f(xr)*f(xl) > 0, xl=xr; else xu=xr; end;

xu = 15.000000

%update endpoint

et = 100*abs((true_value - xr)/true_value); %determine true error ea = 100*abs((xr-xrold)/xr); % determine approximate realtive error % print results fprintf('\nNew root guess xr = %f\n',xr) fprintf('Next bracketing points xl = %f\txu = %f\n',xl,xu) fprintf('et = %.1f%%\tea = %f%%\n',et,ea) %check stopping criteria if ea<es, fprintf('******* DONE: ea is less than es ***********'),end xrold = xr;

iteration 3 New root guess xr = 14.500000 Next bracketing points xl = 14.500000 et = 1.9% ea = 3.448276% Calculate new root and update endpoints i=i+1; display(['iteration ' num2str(i)]) xr = (xl + xu)/2; %calculate new root if f(xr)*f(xl) > 0, xl=xr; else xu=xr; end; et = 100*abs((true_value - xr)/true_value);

xu = 15.000000

%update endpoint %determine true error

file:///H|/00 Quick Links/00 Fall 2009/00 EGR 274/chapter 5 handouts/html/bisectExample.html (4 of 6)9/18/2009 8:25:24 AM

bisectExample

ea = 100*abs((xr-xrold)/xr);

% determine approximate realtive error

% print results fprintf('\nNew root guess xr = %f\n',xr) fprintf('Next bracketing points xl = %f\txu = %f\n',xl,xu) fprintf('et = %.1f%%\tea = %f%%\n',et,ea) %check stopping criteria if ea<es, fprintf('******* DONE: ea is less than es ***********'),end xrold = xr;

iteration 4 New root guess xr = 14.750000 Next bracketing points xl = 14.750000 et = 0.2% ea = 1.694915% Calculate new root and update endpoints i=i+1; display(['iteration ' num2str(i)]) xr = (xl + xu)/2; %calculate new root if f(xr)*f(xl) > 0, xl=xr; else xu=xr; end;

xu = 15.000000

%update endpoint

et = 100*abs((true_value - xr)/true_value); %determine true error ea = 100*abs((xr-xrold)/xr); % determine approximate realtive error % print results fprintf('\nNew root guess xr = %f\n',xr) fprintf('Next bracketing points xl = %f\txu = %f\n',xl,xu) fprintf('et = %.1f%%\tea = %f%%\n',et,ea) %check stopping criteria if ea<es, fprintf('******* DONE: ea is less than es ***********'),end xrold = xr;

iteration 5 New root guess xr = 14.875000 Next bracketing points xl = 14.750000

xu = 14.875000

file:///H|/00 Quick Links/00 Fall 2009/00 EGR 274/chapter 5 handouts/html/bisectExample.html (5 of 6)9/18/2009 8:25:24 AM

bisectExample

et = 0.6% ea = 0.840336% ******* DONE: ea is less than es ***********

Published with MATLAB 7.5

file:///H|/00 Quick Links/00 Fall 2009/00 EGR 274/chapter 5 handouts/html/bisectExample.html (6 of 6)9/18/2009 8:25:24 AM

Das könnte Ihnen auch gefallen