Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Mon Mar 23 10:43:03 2020
- @author: varenza
- """
- import numpy as np
- def fungsi(xi):
- fxi = xi**3-0.165*xi**2+3.993e-4
- return fxi
- def dfungsi(xi):
- eps = 1e-5
- fplus = fungsi(xi+eps)
- fmin = fungsi(xi-eps)
- dfxi = (fplus-fmin)/(2*eps)
- return dfxi
- tol = 0.01
- iterasi = 1
- xold = 0.05
- fold = fungsi(xold)
- dfold = dfungsi(xold)
- x = np.array([xold])
- fx = np.array([fold])
- xnew = xold - fold/dfold
- ea = abs((xnew-xold)/(xnew))*100
- while ea >= tol:
- iterasi += 1
- xold = xnew
- fold = fungsi(xold)
- dfold = dfungsi(xold)
- x = np.append(x,xold)
- fx = np.append(fx,fold)
- xnew = xold-(fold/dfold)
- ea = abs((xnew-xold)/(xnew))*100
- fnew = fungsi(xnew)
- tabel = np.zeros([iterasi,3])
- tabel[:,0] = np.arange(1,iterasi+1)
- tabel[:,1] = x
- tabel[:,2] = fx
- for baris in tabel:
- print('{:^9.0f} {:11.9f} {:>12.4e} '.format(*baris))
- print('Akar persamaan = {:.9f} dengan nilai f(x) = {:.4e}'.format(xnew,fnew))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement