-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
outputs result of bisection method to file
- Loading branch information
Showing
4 changed files
with
64 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,70 @@ | ||
function root = bisection(xl,xu,epsilon, max_iterations, fx) | ||
|
||
% bisection method: | ||
% | ||
% Pors: | ||
% 1- Easy | ||
% 2- always converges | ||
% 3- the number of iterations required to attain an absolute error can be computer a priori. | ||
% | ||
% Cons: | ||
% 1- Slower compared to other methods. | ||
% 2- Need to find inital guess Xl and Xu. | ||
% 3- No account is taken of the fact that if f(xl) is closer to zero, it's likely that root is | ||
% closer to Xl. | ||
|
||
|
||
% | ||
% default value of epsilon = 0.00001 | ||
% default value of max_iterations = 50 | ||
% | ||
function root = bisection(xl, xu, epsilon, max_iterations, fx, output_file) | ||
addpath('../'); % to use displaytable function | ||
tic | ||
if(fx(xl)*fx(xu)>0) | ||
return; | ||
end | ||
|
||
root = implementation(xl, xu, xu, epsilon, max_iterations, fx, 0); | ||
|
||
|
||
end | ||
[root, iterations, data] = implementation(xl, xu, xu, epsilon, max_iterations, fx, 0, []); | ||
disp(iterations); | ||
disp(data); | ||
|
||
fileID = fopen(output_file,'w'); | ||
colheadings = {'Xr', 'Precision'}; | ||
rowheadings = {}; | ||
for i=1:iterations, | ||
rowheadings{end+1} = int2str(i); | ||
end | ||
|
||
fms = {'.4f','.5E'}; | ||
wid = 16; | ||
displaytable(data, colheadings, wid, fms, rowheadings, fileID, '|', '|'); | ||
|
||
function root2 = implementation(xl, xu, xr_old, epsilon, max_iterations, fx, iterations) | ||
timeElapsed = toc | ||
fprintf(fileID, '\nnumber of iterations: %d\n', iterations); | ||
fprintf(fileID, 'execution time: %f\n', timeElapsed); | ||
fclose(fileID); | ||
end | ||
|
||
xr = (xl+xu)/2; | ||
|
||
function [root, iterations, data] = implementation(xl, xu, xr_old, epsilon, max_iterations, fx, iterations, data) | ||
xr = (xl + xu) / 2; | ||
|
||
%01_STOP CONDITION************************* | ||
error=abs((xr-xr_old)/xr); | ||
iterations = iterations+1; | ||
if(error<=epsilon||iterations>max_iterations) | ||
root2 = xr; | ||
approximate_relative_error = abs((xr - xr_old) / xr); | ||
iterations = iterations + 1; | ||
data = [data; xr, approximate_relative_error]; | ||
if(approximate_relative_error <= epsilon || iterations >= max_iterations) | ||
root = xr; | ||
return; | ||
end | ||
|
||
%01_RECURSIVE STEP************************* | ||
if(fx(xr)*fx(xl)<0) | ||
root2 = implementation(xl, xr, xr, epsilon, max_iterations, fx, iterations); | ||
if(fx(xr) * fx(xl) < 0) | ||
[root, iterations, data] = implementation(xl, xr, xr, epsilon, max_iterations, fx, iterations, data); | ||
return; | ||
else | ||
root2 = implementation(xr, xu, xr, epsilon, max_iterations, fx, iterations); | ||
[root, iterations, data] = implementation(xr, xu, xr, epsilon, max_iterations, fx, iterations, data); | ||
return; | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
| Xr| Precision| | ||
1| 0.0550| 1.00000E+00| | ||
2| 0.0825| 3.33333E-01| | ||
3| 0.0688| 2.00000E-01| | ||
4| 0.0619| 1.11111E-01| | ||
5| 0.0653| 5.26316E-02| | ||
6| 0.0636| 2.70270E-02| | ||
7| 0.0627| 1.36986E-02| | ||
8| 0.0623| 6.89655E-03| | ||
9| 0.0625| 3.43643E-03| | ||
10| 0.0624| 1.72117E-03| | ||
|
||
number of iterations: 10 | ||
execution time: 0.065541 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters