Skip to content

Commit

Permalink
outputs result of bisection method to file
Browse files Browse the repository at this point in the history
  • Loading branch information
yakout committed May 13, 2017
1 parent dcea721 commit 44acdb4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 22 deletions.
65 changes: 50 additions & 15 deletions src/root_finding/bisection.m
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
14 changes: 14 additions & 0 deletions src/root_finding/output.txt
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
5 changes: 0 additions & 5 deletions src/system_of_equations/lu_decomposition.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
%solve for X
solution=obtain_x_matrix(upper_matrix, y_matrix, num_of_unknowns);


end


Expand Down Expand Up @@ -99,7 +98,3 @@
system_matrix=result;

end




2 changes: 0 additions & 2 deletions src/system_of_equations/naive_gauss.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
solutions=forward_elimination(system_matrix, num_of_unknowns);
solutions=back_substitution(solutions, num_of_unknowns);



end

function new_system = forward_elimination(system_matrix, num_of_unknowns)
Expand Down

0 comments on commit 44acdb4

Please sign in to comment.