Skip to content

Instantly share code, notes, and snippets.

@rud72k
Created November 22, 2020 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rud72k/781834e88966211d4144bc90073fdf23 to your computer and use it in GitHub Desktop.
Save rud72k/781834e88966211d4144bc90073fdf23 to your computer and use it in GitHub Desktop.
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