Created
July 10, 2021 02:13
-
-
Save AnuragAnalog/3b1de1ebd524059d7acccf193d21cd8c to your computer and use it in GitHub Desktop.
Chebysav method Implementation in python
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
#!/usr/bin/python3.6 | |
# Chebysav method is like approimating the given Transcedental Equation into a quadratic equation f(x) = 0, f(x) ~ a0 + a1x + a2x^2 | |
# | |
# Let xk be an approximate root | |
# f'(x) = a1 + a2x | |
# f''(x) = 2a2 by subsituting the value xk in all the equations we get the values of f(xk), f'(xk), f''(xk) | |
# we get, | |
# f(x) ~ fk + (x-xk)f'_k + (x-xk)^2*f''_k/2 ==> 0 | |
# x_(k+1) = xk - fk/f'k - (fk^2 f''_k)/2((f'_k)^3) | |
def f(x): | |
fx = 3*x**2-15*x+7 | |
return fx | |
def fd(x): | |
fdx = 6*x-15 | |
return fdx | |
def fdd(x): | |
fddx = 6 | |
return fddx | |
def chebyshev(guess): | |
i = 0 | |
while True: | |
root = guess-f(guess)/fd(guess)-(f(guess)**2*fd(guess)/(2*fd(guess)**3)) | |
print(f"Root after iteration {i+1} is {root}.") | |
tmp1 = round(root, 9) | |
tmp2 = round(guess, 9) | |
if tmp1 == tmp2: | |
return root | |
guess = root | |
i = i + 1 | |
guess = float(input("Enter the guess value: ")) | |
root = chebyshev(guess) | |
print(f"The final root is {root}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment