Created
November 22, 2020 06:37
-
-
Save rud72k/781834e88966211d4144bc90073fdf23 to your computer and use it in GitHub Desktop.
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
def GD(f,Df,x0,alpha, epsilon, max_iter,): | |
xn = x0 | |
for n in range(0,max_iter): | |
fxn = f(xn) | |
Dfxn = Df(xn) | |
if Dfxn == 0: | |
print('Turunan Nol. Stop.') | |
return None | |
xn = xn - alpha*Dfxn | |
print(n+1," ", xn) | |
print('Melebihi Maks Iterasi.') | |
return None | |
def newton(f,Df,x0,epsilon,max_iter): | |
'''Approximate solution of f(x)=0 by Newton's method.''' | |
xn = x0 | |
for n in range(0,max_iter): | |
fxn = f(xn) | |
if abs(fxn) < epsilon: | |
print('Solusi didapat setelah',n,'iterasi.') | |
return xn | |
Dfxn = Df(xn) | |
if Dfxn == 0: | |
print('Turunan Nol. Stop.') | |
return None | |
xn = xn - fxn/Dfxn | |
print(xn) | |
print('Melebihi Maks Iterasi.') | |
return None | |
x0 = 5 | |
eror = 1e-10 | |
iterasi = 100 | |
approx = GD(p,Dp,5,eror,iterasi) | |
print(approx) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment