Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
baillif_exercices
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ISC2
maths
baillif_exercices
Commits
7274bdda
Commit
7274bdda
authored
1 year ago
by
iliya
Browse files
Options
Downloads
Patches
Plain Diff
feat: added iterative solutions for both root-finding methods
parent
d45c7554
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
derive_and_min-max.py
+13
-3
13 additions, 3 deletions
derive_and_min-max.py
rf_methods.py
+108
-0
108 additions, 0 deletions
rf_methods.py
with
121 additions
and
3 deletions
derive_and_min-max.py
+
13
−
3
View file @
7274bdda
from
zeroes_func
import
bissect_method
,
regula_falsi
from
rf_methods
import
rec_bisection
,
rec_regula_falsi
,
iter_bisection
,
\
iter_regula_falsi
from
Dual_Numbers
import
Dual_Number
...
...
@@ -11,5 +12,14 @@ def dfdx(x: float):
if
__name__
==
"
__main__
"
:
print
(
f
"
Bissect method =
{
bissect_method
(
-
6
,
-
1
,
dfdx
)
}
"
)
print
(
f
"
Regula falsi =
{
regula_falsi
(
-
6
,
-
1
,
dfdx
)
}
"
)
start
=
-
6
stop
=
-
1
print
(
"
----------------------------------------------
"
)
print
(
f
"
Rec bisection =
{
rec_bisection
(
start
,
stop
,
dfdx
,
False
)
}
"
)
print
(
"
----------------------------------------------
"
)
print
(
f
"
Rec regula falsi =
{
rec_regula_falsi
(
start
,
stop
,
dfdx
,
False
)
}
"
)
print
(
"
----------------------------------------------
"
)
print
(
f
"
Iter bisection =
{
iter_bisection
(
start
,
stop
,
dfdx
,
False
)
}
"
)
print
(
"
----------------------------------------------
"
)
print
(
f
"
Iter regula falsi =
{
iter_regula_falsi
(
start
,
stop
,
dfdx
,
False
)
}
"
)
This diff is collapsed.
Click to expand it.
zeroes_func
.py
→
rf_methods
.py
+
108
−
0
View file @
7274bdda
...
...
@@ -17,7 +17,7 @@ def z(x: float) -> float:
return
x
**
3
-
x
**
2
-
1
def
regula_falsi
(
start
:
float
,
stop
:
float
,
func
)
->
float
:
def
rec_
regula_falsi
(
start
:
float
,
stop
:
float
,
func
,
debug
:
bool
)
->
float
:
cN
=
(
start
*
func
(
stop
)
-
stop
*
func
(
start
))
/
(
func
(
stop
)
-
func
(
start
))
if
np
.
sign
(
func
(
cN
))
!=
np
.
sign
(
func
(
start
)):
...
...
@@ -26,16 +26,40 @@ def regula_falsi(start: float, stop: float, func) -> float:
start
=
cN
if
np
.
abs
(
func
(
stop
)
-
func
(
start
))
>
1e-9
:
print
(
f
"
Intermediate value of Cn =
{
cN
}
\t
f(Cn) =
{
func
(
cN
)
}
"
)
if
debug
:
print
(
f
"
x =
{
cN
}
\t
f(x) =
{
func
(
cN
)
}
"
)
# Testing the y-axis
if
np
.
abs
(
func
(
cN
))
<
1e-9
:
return
cN
return
regula_falsi
(
start
,
stop
,
func
)
return
rec_
regula_falsi
(
start
,
stop
,
func
,
debug
)
else
:
return
cN
def
bissect_method
(
start
:
float
,
stop
:
float
,
func
)
->
float
:
def
iter_regula_falsi
(
init_start
:
float
,
init_stop
:
float
,
func
,
debug
:
bool
)
\
->
float
:
cN
=
(
init_start
*
func
(
init_stop
)
-
init_stop
*
func
(
init_start
))
/
(
func
(
init_stop
)
-
func
(
init_start
))
start
=
init_start
stop
=
init_stop
iter
=
0
while
np
.
abs
(
func
(
stop
)
-
func
(
start
))
>
1e-9
or
np
.
abs
(
func
(
cN
))
>
1e-9
:
if
debug
:
print
(
f
"
Iter =
{
iter
}
\t
x =
{
cN
}
\t
f(x) =
{
func
(
cN
)
}
"
)
cN
=
(
start
*
func
(
stop
)
-
stop
*
func
(
start
))
/
\
(
func
(
stop
)
-
func
(
start
))
if
np
.
sign
(
func
(
cN
))
!=
np
.
sign
(
func
(
start
)):
stop
=
cN
elif
np
.
sign
(
func
(
cN
))
!=
np
.
sign
(
func
(
stop
)):
start
=
cN
iter
+=
1
return
cN
def
rec_bisection
(
start
:
float
,
stop
:
float
,
func
,
debug
:
bool
)
->
float
:
cN
=
(
stop
+
start
)
/
2
if
np
.
sign
(
func
(
cN
))
!=
np
.
sign
(
func
(
start
)):
...
...
@@ -44,14 +68,41 @@ def bissect_method(start: float, stop: float, func) -> float:
start
=
cN
if
np
.
abs
(
stop
-
start
)
>
1e-9
:
print
(
f
"
Intermediate value of Cn =
{
cN
}
\t
f(Cn) =
{
func
(
cN
)
}
"
)
return
bissect_method
(
start
,
stop
,
func
)
if
debug
:
print
(
f
"
x =
{
cN
}
\t
f(x) =
{
func
(
cN
)
}
"
)
return
rec_bisection
(
start
,
stop
,
func
,
debug
)
else
:
return
cN
def
iter_bisection
(
init_start
:
float
,
init_stop
:
float
,
func
,
debug
:
bool
)
->
\
float
:
cN
=
(
init_stop
+
init_start
)
/
2
start
=
init_start
stop
=
init_stop
iter
=
0
while
np
.
abs
(
stop
-
start
)
>
1e-9
:
if
debug
:
print
(
f
"
Iter =
{
iter
}
\t
x =
{
cN
}
\t
f(x) =
{
func
(
cN
)
}
"
)
cN
=
(
stop
+
start
)
/
2
if
np
.
sign
(
func
(
cN
))
!=
np
.
sign
(
func
(
start
)):
stop
=
cN
elif
np
.
sign
(
func
(
cN
))
!=
np
.
sign
(
func
(
stop
)):
start
=
cN
iter
+=
1
return
cN
if
__name__
==
"
__main__
"
:
# print(f"Bissection = {bissect_method(0.5, 2, g)}")
# print(f"Regula falsi = {regula_falsi(-3, 7, h)}")
# print(f"Bissection = {bissect_method(-3, 7, h)}")
print
(
f
"
Regula falsi =
{
regula_falsi
(
0
,
3
,
z
)
}
"
)
# print(f"Regula falsi = {rec_regula_falsi(0, 3, z)}")
print
(
f
"
Iter bisection=
{
iter_bisection
(
0.5
,
2
,
g
,
False
)
}
"
)
print
(
f
"
Rec bisection=
{
rec_bisection
(
0.5
,
2
,
g
,
False
)
}
"
)
print
(
f
"
Iter regula falsi=
{
iter_regula_falsi
(
0.5
,
2
,
g
,
False
)
}
"
)
print
(
f
"
Rec regula falsi =
{
rec_regula_falsi
(
0.5
,
2
,
g
,
False
)
}
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment