Skip to content
Snippets Groups Projects
Commit f1f3d676 authored by iliya's avatar iliya
Browse files

feat: made explicit gradient function for 2d and 3d case, might be better to...

feat: made explicit gradient function for 2d and 3d case, might be better to create a N dimensional case but icba honestly
parent 26d62541
No related branches found
No related tags found
No related merge requests found
from rf_methods import rec_bisection, rec_regula_falsi, iter_bisection, \
iter_regula_falsi
from Dual_Numbers import Dual_Number
from typing import Callable
import numpy as np
......@@ -46,41 +47,39 @@ import numpy as np
# return h(Dual_Number(x, 1))
def P(x: Dual_Number, y: Dual_Number) -> float:
def A(x: Dual_Number, y: Dual_Number) -> float:
return (3 * (x * x) * y + 2 * x * (y * y) - x * y + 2 * x - 1).d
def delPdelx(x: float, y: float) -> float:
return P(Dual_Number(x, 1), y)
def delPdely(x: float, y: float) -> float:
return P(x, Dual_Number(y, 1))
def B(x: Dual_Number, y: Dual_Number, z: Dual_Number) -> float:
return (x * y * z + x * x + y * y).d
def Z(x: Dual_Number, y: Dual_Number, z: Dual_Number) -> float:
return (x * y * z + x * x + y * y).d
def C(x: Dual_Number, y: Dual_Number, z: Dual_Number) -> float:
return (x * Dual_Number.exp(y * y + z * z)).d
def delZdelx(x: float, y: float, z: float) -> float:
return Z(Dual_Number(x, 1), y, z)
def D(x: Dual_Number, y: Dual_Number) -> float:
return (Dual_Number.sin(x) * Dual_Number.cos(y) + Dual_Number.sin(y) * Dual_Number.cos(x)).d
def delZdely(x: float, y: float, z: float) -> float:
return Z(x, Dual_Number(y, 1), z)
def grad2D(x: float, y: float, func: Callable[[Dual_Number, Dual_Number], float]) -> tuple[float, float]:
return (func(Dual_Number(x, 1), y), func(x, Dual_Number(y, 1)))
def delZdelz(x: float, y: float, z: float) -> float:
return Z(x, y, Dual_Number(z, 1))
def grad3D(x: float, y: float, z: float, func: Callable[[Dual_Number, Dual_Number, Dual_Number], float]) -> tuple[float, float, float]:
return (func(Dual_Number(x, 1), y, z), func(x, Dual_Number(y, 1), z), func(x, y, Dual_Number(z, 1)))
if __name__ == "__main__":
print(delPdelx(1, 1))
print(delPdely(1, 1))
print()
print(delZdelx(0, 1, -1))
print(delZdely(0, 1, -1))
print(delZdelz(0, 1, -1))
print(f"3x²y + 2xy² − xy + 2x − 1, P = (1, 1)\t∇ = {grad2D(1, 1, A)}")
print(f"xyz + x² + y², P = (0, 1, -1)\t∇ = {grad3D(0, 1, -1, B)}")
print(f"xe^(y² + z²), P = (1, 1, 1)\t∇ = {grad3D(1, 1, 1, C)}")
print(
f"sin(x)cos(y) + sin(y)cos(x), P(π / 4, π / 4)\t∇ = {grad2D(np.pi / 4, np.pi / 4, D)}")
# print(delZdelx(0, 1, -1))
# print(delZdely(0, 1, -1))
# print(delZdelz(0, 1, -1))
# Example for f(x)
# start = -6
# stop = -1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment