From 78d941b4509e04cf5fc031207b727eab0142daf5 Mon Sep 17 00:00:00 2001 From: billpaps <37051006+billpaps@users.noreply.github.com> Date: Sun, 9 Feb 2020 15:15:31 +0200 Subject: [PATCH 01/12] Create Bisection.py Find root of --- maths/Bisection.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 maths/Bisection.py diff --git a/maths/Bisection.py b/maths/Bisection.py new file mode 100644 index 000000000000..33d6c39471ee --- /dev/null +++ b/maths/Bisection.py @@ -0,0 +1,50 @@ +'''Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ +such that f(a)*f(b) < 0 and f(x) is continuous in [a, b]. Here f(x) represents algebraic or transcendental equation. +Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0) +''' + + +import math + +def equation(x): + return math.cos(x) + x + +def bisection(a, b): + #Bolzano theory in order to find if there is a root between a and b + if equation(a) * equation(b) >= 0: + print("There is not a root between a and b\n") + return + + half = a + while ((b - a) >= 0.01): + + # Find middle point + c = (a + b) / 2 + + # Check if middle point is root + if equation(c) == 0.0: + break + + # Decide the side to repeat the steps + if equation(c) * equation(a) < 0: + b = c + else: + a = c + + print("The value of root is : ", "%.4f" % c) + +#Testing +print("Testing for a = -3 and b = 2") +a = -3 +b = 2 +bisection(a, b) + +a = 1 +b = 3 +print("\nTesting for a = 1 and b = 3") +bisection(a, b) + +print("\nTesting for a = -5 and b = 0") +a = -5 +b=0 +bisection(a,b) From 95b39cbcdf0f9a33bfda33f96a74775f2ca0e81b Mon Sep 17 00:00:00 2001 From: billpaps <37051006+billpaps@users.noreply.github.com> Date: Sun, 9 Feb 2020 15:20:27 +0200 Subject: [PATCH 02/12] Update Bisection.py --- maths/Bisection.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/maths/Bisection.py b/maths/Bisection.py index 33d6c39471ee..b0cf914c43ae 100644 --- a/maths/Bisection.py +++ b/maths/Bisection.py @@ -1,14 +1,15 @@ -'''Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ -such that f(a)*f(b) < 0 and f(x) is continuous in [a, b]. Here f(x) represents algebraic or transcendental equation. -Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0) +'''Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ we test if there is +a root between a and b. The bisection theory can be applied for algebraic or transcendental equations. ''' import math +#Put here the equation you want def equation(x): return math.cos(x) + x +#Finds the root and prints it def bisection(a, b): #Bolzano theory in order to find if there is a root between a and b if equation(a) * equation(b) >= 0: From 984985f256d55be9470a33ee5b6c28df37c5a2bf Mon Sep 17 00:00:00 2001 From: billpaps <37051006+billpaps@users.noreply.github.com> Date: Mon, 10 Feb 2020 20:57:05 +0200 Subject: [PATCH 03/12] Update Bisection.py i changed the given function with one that i could make the doctests. --- maths/Bisection.py | 62 ++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/maths/Bisection.py b/maths/Bisection.py index b0cf914c43ae..da26fd904f91 100644 --- a/maths/Bisection.py +++ b/maths/Bisection.py @@ -1,23 +1,30 @@ -'''Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ we test if there is -a root between a and b. The bisection theory can be applied for algebraic or transcendental equations. -''' - - -import math - -#Put here the equation you want -def equation(x): - return math.cos(x) + x - -#Finds the root and prints it -def bisection(a, b): - #Bolzano theory in order to find if there is a root between a and b +"""Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ +such that f(a)*f(b) < 0 and f(x) is continuous in [a, b]. Here f(x) represents algebraic or transcendental equation. +Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0) +""" +import doctest + + +def equation(x: int) -> float: + return 10 - x * x + + +def bisection(a: int, b: int) -> float: + """ + >>> bisection(-2,5) + 3.1611328125 + >>> bisection(0, 6) + 3.158203125 + >>> bisection(2,3) + False + """ + c = False + # Bolzano theory in order to find if there is a root between a and b if equation(a) * equation(b) >= 0: - print("There is not a root between a and b\n") - return + return c half = a - while ((b - a) >= 0.01): + while (b - a) >= 0.01: # Find middle point c = (a + b) / 2 @@ -32,20 +39,11 @@ def bisection(a, b): else: a = c - print("The value of root is : ", "%.4f" % c) - -#Testing -print("Testing for a = -3 and b = 2") -a = -3 -b = 2 -bisection(a, b) + return c -a = 1 -b = 3 -print("\nTesting for a = 1 and b = 3") -bisection(a, b) -print("\nTesting for a = -5 and b = 0") -a = -5 -b=0 -bisection(a,b) +if __name__ == '__main__': + print(bisection(-2, 5)) + print(bisection(0, 6)) + print(bisection(2, 3)) + doctest.testmod() From a87330d7fdb3b3bca18eb0eaef4bb09b012f65ef Mon Sep 17 00:00:00 2001 From: billpaps <37051006+billpaps@users.noreply.github.com> Date: Mon, 10 Feb 2020 20:58:30 +0200 Subject: [PATCH 04/12] Rename Bisection.py to bisection.py --- maths/{Bisection.py => bisection.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename maths/{Bisection.py => bisection.py} (100%) diff --git a/maths/Bisection.py b/maths/bisection.py similarity index 100% rename from maths/Bisection.py rename to maths/bisection.py From b6f8bebb16bd55a6e345da8e88117eaee54a4834 Mon Sep 17 00:00:00 2001 From: billpaps <37051006+billpaps@users.noreply.github.com> Date: Mon, 10 Feb 2020 21:13:31 +0200 Subject: [PATCH 05/12] Update bisection.py --- maths/bisection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/bisection.py b/maths/bisection.py index da26fd904f91..eeb775c0bf68 100644 --- a/maths/bisection.py +++ b/maths/bisection.py @@ -5,7 +5,7 @@ import doctest -def equation(x: int) -> float: +def equation(x: int) -> int: return 10 - x * x From 9cdef1bc5127ae63fc4563e51dbb691359edddee Mon Sep 17 00:00:00 2001 From: billpaps <37051006+billpaps@users.noreply.github.com> Date: Mon, 10 Feb 2020 21:31:03 +0200 Subject: [PATCH 06/12] Update bisection.py --- maths/bisection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/bisection.py b/maths/bisection.py index eeb775c0bf68..d506f4e7472b 100644 --- a/maths/bisection.py +++ b/maths/bisection.py @@ -23,7 +23,7 @@ def bisection(a: int, b: int) -> float: if equation(a) * equation(b) >= 0: return c - half = a + c = a while (b - a) >= 0.01: # Find middle point From 0eea8a7caf7ba48167fbb2a913519906d9c61859 Mon Sep 17 00:00:00 2001 From: billpaps <37051006+billpaps@users.noreply.github.com> Date: Mon, 10 Feb 2020 21:51:18 +0200 Subject: [PATCH 07/12] Update bisection.py --- maths/bisection.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maths/bisection.py b/maths/bisection.py index d506f4e7472b..af750497da96 100644 --- a/maths/bisection.py +++ b/maths/bisection.py @@ -18,10 +18,9 @@ def bisection(a: int, b: int) -> float: >>> bisection(2,3) False """ - c = False # Bolzano theory in order to find if there is a root between a and b if equation(a) * equation(b) >= 0: - return c + return False c = a while (b - a) >= 0.01: From 8c6b55c72bedc977c2da49e2e9bd0992f1848e5d Mon Sep 17 00:00:00 2001 From: billpaps <37051006+billpaps@users.noreply.github.com> Date: Mon, 10 Feb 2020 22:07:30 +0200 Subject: [PATCH 08/12] Update bisection.py --- maths/bisection.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/maths/bisection.py b/maths/bisection.py index af750497da96..6bbb7145d90c 100644 --- a/maths/bisection.py +++ b/maths/bisection.py @@ -1,43 +1,43 @@ """Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ -such that f(a)*f(b) < 0 and f(x) is continuous in [a, b]. Here f(x) represents algebraic or transcendental equation. -Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0) +such that f(a)*f(b) < 0 and f(x) is continuous in [a, b]. Here f(x) represents + algebraic or transcendental equation. +Find root of function in interval [a, b] (Or find a value of x such +that f(x) is 0) """ import doctest - def equation(x: int) -> int: - return 10 - x * x + """ + >>> equation(0) + 10 + """ + return 10 - x * x def bisection(a: int, b: int) -> float: """ - >>> bisection(-2,5) + >>> bisection(-2, 5) 3.1611328125 >>> bisection(0, 6) 3.158203125 - >>> bisection(2,3) + >>> bisection(2, 3) False """ # Bolzano theory in order to find if there is a root between a and b if equation(a) * equation(b) >= 0: return False - c = a while (b - a) >= 0.01: - # Find middle point c = (a + b) / 2 - # Check if middle point is root if equation(c) == 0.0: break - # Decide the side to repeat the steps if equation(c) * equation(a) < 0: b = c else: a = c - return c From 8bbee80266c422bff4916b30ecc84ab834a0fc0e Mon Sep 17 00:00:00 2001 From: billpaps <37051006+billpaps@users.noreply.github.com> Date: Mon, 10 Feb 2020 22:31:36 +0200 Subject: [PATCH 09/12] Update bisection.py Made the changes that were requested --- maths/bisection.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/maths/bisection.py b/maths/bisection.py index 6bbb7145d90c..9fb333a2e403 100644 --- a/maths/bisection.py +++ b/maths/bisection.py @@ -21,11 +21,14 @@ def bisection(a: int, b: int) -> float: >>> bisection(0, 6) 3.158203125 >>> bisection(2, 3) - False + Traceback (most recent call last): + ... + ValueError: Wrong space! """ # Bolzano theory in order to find if there is a root between a and b if equation(a) * equation(b) >= 0: - return False + raise ValueError('Wrong space!') + c = a while (b - a) >= 0.01: # Find middle point @@ -44,5 +47,5 @@ def bisection(a: int, b: int) -> float: if __name__ == '__main__': print(bisection(-2, 5)) print(bisection(0, 6)) - print(bisection(2, 3)) + doctest.testmod() From be56af2109132be5dc357683cbf58f21a16d6b09 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 10 Feb 2020 21:48:08 +0100 Subject: [PATCH 10/12] Update bisection.py --- maths/bisection.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/maths/bisection.py b/maths/bisection.py index 9fb333a2e403..ad806a0beaa2 100644 --- a/maths/bisection.py +++ b/maths/bisection.py @@ -1,19 +1,23 @@ -"""Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ +"""Given a function on floating point number f(x) and two numbers ‘a’ and ‘b’ such that f(a)*f(b) < 0 and f(x) is continuous in [a, b]. Here f(x) represents - algebraic or transcendental equation. -Find root of function in interval [a, b] (Or find a value of x such +algebraic or transcendental equation. +Find root of the function in the interval [a, b] (Or find a value of x such that f(x) is 0) -""" -import doctest +https://en.wikipedia.org/wiki/Bisection_method +""" def equation(x: int) -> int: """ + >>> equation(5) + -15 >>> equation(0) 10 - + >>> equation(-5) + -15 """ return 10 - x * x + def bisection(a: int, b: int) -> float: """ >>> bisection(-2, 5) @@ -45,7 +49,8 @@ def bisection(a: int, b: int) -> float: if __name__ == '__main__': - print(bisection(-2, 5)) - print(bisection(0, 6)) + import doctest doctest.testmod() + print(bisection(-2, 5)) + print(bisection(0, 6)) From de6e494bfd8bee9ae598a340c2c332ac063a4703 Mon Sep 17 00:00:00 2001 From: billpaps <37051006+billpaps@users.noreply.github.com> Date: Mon, 10 Feb 2020 23:27:35 +0200 Subject: [PATCH 11/12] Update bisection.py --- maths/bisection.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/maths/bisection.py b/maths/bisection.py index ad806a0beaa2..484125bd478b 100644 --- a/maths/bisection.py +++ b/maths/bisection.py @@ -1,24 +1,20 @@ -"""Given a function on floating point number f(x) and two numbers ‘a’ and ‘b’ +"""Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ such that f(a)*f(b) < 0 and f(x) is continuous in [a, b]. Here f(x) represents -algebraic or transcendental equation. -Find root of the function in the interval [a, b] (Or find a value of x such + algebraic or transcendental equation. +Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0) - -https://en.wikipedia.org/wiki/Bisection_method """ -def equation(x: int) -> int: +import doctest + +def equation(x: float) -> float: """ - >>> equation(5) - -15 >>> equation(0) 10 - >>> equation(-5) - -15 + """ return 10 - x * x - -def bisection(a: int, b: int) -> float: +def bisection(a: float, b: float) -> float: """ >>> bisection(-2, 5) 3.1611328125 @@ -49,8 +45,7 @@ def bisection(a: int, b: int) -> float: if __name__ == '__main__': - import doctest - - doctest.testmod() print(bisection(-2, 5)) print(bisection(0, 6)) + + doctest.testmod() From 0f0928b367fa488972768b8a0cce36ae794e5f95 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 11 Feb 2020 09:20:09 +0100 Subject: [PATCH 12/12] Add wiki url --- maths/bisection.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/maths/bisection.py b/maths/bisection.py index 484125bd478b..a9df15b775b3 100644 --- a/maths/bisection.py +++ b/maths/bisection.py @@ -1,19 +1,27 @@ -"""Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ -such that f(a)*f(b) < 0 and f(x) is continuous in [a, b]. Here f(x) represents - algebraic or transcendental equation. -Find root of function in interval [a, b] (Or find a value of x such -that f(x) is 0) """ -import doctest +Given a function on floating number f(x) and two floating numbers ‘a’ and ‘b’ such that +f(a) * f(b) < 0 and f(x) is continuous in [a, b]. +Here f(x) represents algebraic or transcendental equation. +Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0) +https://en.wikipedia.org/wiki/Bisection_method +""" def equation(x: float) -> float: """ + >>> equation(5) + -15 >>> equation(0) 10 - + >>> equation(-5) + -15 + >>> equation(0.1) + 9.99 + >>> equation(-0.1) + 9.99 """ return 10 - x * x + def bisection(a: float, b: float) -> float: """ >>> bisection(-2, 5) @@ -27,7 +35,7 @@ def bisection(a: float, b: float) -> float: """ # Bolzano theory in order to find if there is a root between a and b if equation(a) * equation(b) >= 0: - raise ValueError('Wrong space!') + raise ValueError("Wrong space!") c = a while (b - a) >= 0.01: @@ -44,8 +52,10 @@ def bisection(a: float, b: float) -> float: return c -if __name__ == '__main__': - print(bisection(-2, 5)) - print(bisection(0, 6)) +if __name__ == "__main__": + import doctest doctest.testmod() + + print(bisection(-2, 5)) + print(bisection(0, 6))