From 78bd8ce98681ec1a5abc59d1bb7b0b5925fefd89 Mon Sep 17 00:00:00 2001 From: dimgrichr <32580033+dimgrichr@users.noreply.github.com> Date: Wed, 5 Jun 2019 13:41:26 +0300 Subject: [PATCH 1/4] Add files via upload --- arithmetic_analysis/secant_method.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 arithmetic_analysis/secant_method.py diff --git a/arithmetic_analysis/secant_method.py b/arithmetic_analysis/secant_method.py new file mode 100644 index 000000000000..4cfc39cfd696 --- /dev/null +++ b/arithmetic_analysis/secant_method.py @@ -0,0 +1,19 @@ +# Implementing Secant method in Python +# Author: dimgrichr + + +from math import cos, sin, exp +def f(x): + return 8*x-2*exp(-x); + + +def SecantMethod(lowerBound, upperBound, repeats): + x0 = lowerBound; + x1 = upperBound; + for i in range(0,repeats): + x2 = x1 - (f(x1)*(x1-x0))/(f(x1)-f(x0)); + x0=x1; + x1=x2; + return x2; + +print('The solution is: ' ,SecantMethod(1,3,2)); From f1be3bdd6ca856e31d05963106146bd099583dc9 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 28 Oct 2019 19:14:19 +0100 Subject: [PATCH 2/4] Update secant_method.py --- arithmetic_analysis/secant_method.py | 31 ++++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/arithmetic_analysis/secant_method.py b/arithmetic_analysis/secant_method.py index 4cfc39cfd696..0a24ed106390 100644 --- a/arithmetic_analysis/secant_method.py +++ b/arithmetic_analysis/secant_method.py @@ -2,18 +2,27 @@ # Author: dimgrichr -from math import cos, sin, exp +from math import cos, exp, sin + + def f(x): - return 8*x-2*exp(-x); + """ + >>> f(5) + 39.98652410600183 + """ + return 8 * x - 2 * exp(-x) + +def SecantMethod(lower_bound, upper_bound, repeats): + """ + >>> SecantMethod(1, 3, 2) + 0.2139409276214589 + """ + x0 = lower_bound + x1 = upper_bound + for i in range(0, repeats): + x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0)) + return x1 -def SecantMethod(lowerBound, upperBound, repeats): - x0 = lowerBound; - x1 = upperBound; - for i in range(0,repeats): - x2 = x1 - (f(x1)*(x1-x0))/(f(x1)-f(x0)); - x0=x1; - x1=x2; - return x2; -print('The solution is: ' ,SecantMethod(1,3,2)); +print(f"The solution is: {SecantMethod(1, 3, 2)}") From f01edda3c6bef038ba265ccdac90fe19e46c14de Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 28 Oct 2019 19:19:09 +0100 Subject: [PATCH 3/4] Remove unused import --- arithmetic_analysis/secant_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetic_analysis/secant_method.py b/arithmetic_analysis/secant_method.py index 0a24ed106390..37a714a74575 100644 --- a/arithmetic_analysis/secant_method.py +++ b/arithmetic_analysis/secant_method.py @@ -2,7 +2,7 @@ # Author: dimgrichr -from math import cos, exp, sin +from math import exp, sin def f(x): From a0ca7609340dc55a88fb31ebe8100d1cfa442e39 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 28 Oct 2019 19:23:44 +0100 Subject: [PATCH 4/4] Remove unused import --- arithmetic_analysis/secant_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetic_analysis/secant_method.py b/arithmetic_analysis/secant_method.py index 37a714a74575..b05d44c627d8 100644 --- a/arithmetic_analysis/secant_method.py +++ b/arithmetic_analysis/secant_method.py @@ -2,7 +2,7 @@ # Author: dimgrichr -from math import exp, sin +from math import exp def f(x):