update examples/demo.py and tutorials/tutorial4_root_finding.ipynb# On branch main

This commit is contained in:
Deniz
2025-09-17 09:24:25 +03:00
parent 17b7fd4657
commit 44f7559aa1
2 changed files with 104 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
import sys
sys.path.append('../')
sys.path.append("../")
from numethods import *
@@ -13,6 +14,7 @@ def demo_linear_solvers():
print("Jacobi:", Jacobi(A, b, tol=1e-12).solve())
print("Gauss-Seidel:", GaussSeidel(A, b, tol=1e-12).solve())
def demo_qr():
A = Matrix([[2, -1], [1, 2], [1, 1]])
@@ -38,11 +40,7 @@ def demo_qr():
print("Least squares solution:", x_ls)
def demo_roots():
f = lambda x: x**2 - 2
df = lambda x: 2 * x
# Newton
steps = NewtonRoot(f, df, x0=1.0).trace()
print("Newton Method Trace (x^2 - 2):")
@@ -74,6 +72,7 @@ def demo_interpolation():
print("Newton interpolation at", t, "=", newt.evaluate(t))
print("Lagrange interpolation at", t, "=", lagr.evaluate(t))
def demo_differentiation():
f = lambda x: x**3 # f'(x) = 3x^2, f''(x) = 6x
x0 = 2.0
@@ -86,7 +85,6 @@ def demo_differentiation():
print("Second derivative:", SecondDerivative(f, x0))
if __name__ == "__main__":
demo_linear_solvers()
demo_roots()