-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a49f18f
commit 124f014
Showing
4 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .nr import newton_raphson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from ltypes import f64, i32 | ||
|
||
|
||
def func(x: f64, c: f64) -> f64: | ||
return x**2.0 - c**2.0 | ||
|
||
|
||
def func_prime(x: f64, c: f64) -> f64: | ||
return 2.0*x | ||
|
||
|
||
def newton_raphson(x: f64, c: f64, maxiter: i32) -> f64: | ||
h: f64 = func(x, c) / func_prime(x, c) | ||
err: f64 = 1e-5 | ||
i: i32 = 0 | ||
while abs(func(x, c)) > err and i < maxiter: | ||
h = func(x, c) / func_prime(x, c) | ||
x = x - h | ||
i += 1 | ||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from nrp import newton_raphson | ||
from ltypes import f64, i32 | ||
|
||
|
||
def check(): | ||
x0: f64 = 20.0 | ||
c: f64 = 3.0 | ||
maxiter: i32 = 20 | ||
x: f64 | ||
x = newton_raphson(x0, c, maxiter) | ||
assert abs(x - 3.0) < 1e-5 | ||
|
||
check() |