Skip to content

Commit c905169

Browse files
authored
Merge pull request #23 from MatteoRaso/master
Added a new section and a few algorithms
2 parents e2d6f46 + d44a3bf commit c905169

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ These repository is a collection of useful algorithms and data structures built
2626
* Palindrome
2727
* maths
2828
* to_polar
29+
* arithmetic_analysis
30+
* bisection
31+
* newton
32+
* secant
33+
* false position
2934
* other
3035
* tic_tac_toe
36+
3137

3238
* machine-learning
3339
* nearest neighbor
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
%The bisection method for finding the root of a function.
2+
%f(a) is < 0 and f(b) > 0. By the intermediate value theorem,
3+
%there exists a number c in between a and b such that f(c) = 0.
4+
%In other words, there is always a root in between f(a) and f(b).
5+
%The bisection method takes the midpoint between a and b and evaluates
6+
%the value of the function at the midpoint. If it is less than 0,
7+
%a is assigned the midpoint. If it is greater than 0, b is assigned the
8+
%midpoint. With each iteration, the interval the root lies in is halved,
9+
%guaranteeing that the algorithm will converge towards the root.
10+
11+
%INPUTS:
12+
%Function handle f
13+
%endpoint a
14+
%endpoint b
15+
%maximum tolerated error
16+
17+
%OUTPUTS:
18+
%An approximated value for the root of f within the defined interval.
19+
20+
%Written by MatteoRaso
21+
22+
function y = bisection(f, a, b, error)
23+
%Making sure that the user didn't input invalid endpoints.
24+
if ~(f(a) < 0)
25+
disp("f(a) must be less than 0")
26+
elseif ~(f(b) > 0)
27+
disp("f(b) must be greater than 0")
28+
else
29+
c = 1e9;
30+
%Loops until we reach an acceptable approximation.
31+
while abs(f(c)) > error
32+
c = (a + b) / 2;
33+
if f(c) < 0
34+
a = c;
35+
else
36+
b = c;
37+
endif
38+
disp(f(c))
39+
endwhile
40+
x = ["The root is approximately located at ", num2str(c)];
41+
disp(x)
42+
y = c;
43+
endif
44+
endfunction
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
%The basic idea behind the false position method is similar to the bisection
2+
%method in that we continuously shrink the interval the root lies on until
3+
%the algorithm converges on the root. Unlike the bisection method, the false
4+
%position method does not halve the interval with each iteration. Instead of
5+
%using the midpoint of a and b to create the new interval, the false position
6+
%method uses the x-intercept of the line connecting f(a) and f(b). This
7+
%algorithm converges faster than the bisection method.
8+
9+
%INPUTS:
10+
%Function handle f
11+
%endpoint a
12+
%endpoint b
13+
%maximum tolerated error
14+
15+
%OUTPUTS:
16+
%An approximated value for the root of f within the defined interval.
17+
18+
%Written by MatteoRaso
19+
20+
function y = false_position(f, a, b, error)
21+
if ~(f(a) < 0)
22+
disp("f(a) must be less than 0")
23+
elseif ~(f(b) > 0)
24+
disp("f(b) must be greater than zero")
25+
else
26+
c = 100000;
27+
while abs(f(c)) > error
28+
%Formula for the x-intercept
29+
c = -f(b) * (b - a) / (f(b) - f(a)) + b;
30+
if f(c) < 0
31+
a = c;
32+
else
33+
b = c;
34+
endif
35+
disp(f(c))
36+
endwhile
37+
x = ["The root is approximately located at ", num2str(c)];
38+
disp(x)
39+
y = c;
40+
endif
41+
endfunction
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
%Newton's method is one of the fastest algorithms to converge on a root.
2+
%It does not require you to provide any endpoints, but it does require for
3+
%you to provide the derivative of the function. It is faster than the secant
4+
%method, but is also not guaranteed to converge.
5+
6+
%INPUTS:
7+
%function handle f
8+
%function handle df for the derivative of f
9+
%initial x-value
10+
%maximum tolerated error
11+
12+
%OUTPUTS:
13+
%An approximated value for the root of f.
14+
15+
%Written by MatteoRaso
16+
17+
function y = newton(f, df, x, error)
18+
while abs(f(x)) > error
19+
x = x - f(x) / df(x);
20+
disp(f(x))
21+
endwhile
22+
A = ["The root is approximately located at ", num2str(x)];
23+
disp(A)
24+
y = x;
25+
endfunction
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
%Extremely similar to the false position method. The main difference is
2+
%that the secant method does not actually have a defined interval where
3+
%the root lies on. It converges faster than the false position method,
4+
%but it is not always guaranteed to converge.
5+
6+
%INPUTS:
7+
%Function handle f
8+
%x1 = a
9+
%x2 = b
10+
%maximum tolerated error
11+
12+
%OUTPUTS:
13+
%An approximated value for the root of f.
14+
15+
%Written by MatteoRaso
16+
17+
function y = secant(f, a, b, error)
18+
x = [a, b];
19+
n = 2;
20+
while abs(f(x(n))) > error
21+
x(n + 1) = -f(x(n)) * (x(n) - x(n - 1)) / (f(x(n)) - f(x(n - 1))) + x(n);
22+
n = n + 1;
23+
disp(f(x(n)))
24+
endwhile
25+
A = ["The root is approximately ", num2str(x(n))];
26+
disp(A)
27+
y = x(n);
28+
endfunction

0 commit comments

Comments
 (0)