Advertisement
fenvel

root buat sendiri

Apr 8th, 2020
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Mar 23 10:43:03 2020
  4.  
  5. @author: varenza
  6. """
  7.  
  8. import numpy as np
  9.  
  10. def fungsi(xi):
  11.     fxi = xi**3-0.165*xi**2+3.993e-4
  12.     return fxi
  13. def dfungsi(xi):
  14.     eps = 1e-5
  15.     fplus = fungsi(xi+eps)
  16.     fmin = fungsi(xi-eps)
  17.     dfxi = (fplus-fmin)/(2*eps)
  18.     return dfxi
  19.  
  20. tol = 0.01
  21. iterasi = 1
  22. xold = 0.05
  23. fold = fungsi(xold)
  24. dfold = dfungsi(xold)
  25. x = np.array([xold])
  26. fx = np.array([fold])
  27. xnew = xold - fold/dfold
  28. ea = abs((xnew-xold)/(xnew))*100
  29.  
  30. while ea >= tol:
  31.     iterasi += 1
  32.     xold = xnew
  33.     fold = fungsi(xold)
  34.     dfold = dfungsi(xold)
  35.     x = np.append(x,xold)
  36.     fx = np.append(fx,fold)
  37.     xnew = xold-(fold/dfold)
  38.     ea = abs((xnew-xold)/(xnew))*100
  39. fnew = fungsi(xnew)
  40.  
  41.  
  42. tabel = np.zeros([iterasi,3])
  43. tabel[:,0] = np.arange(1,iterasi+1)
  44. tabel[:,1] = x
  45. tabel[:,2] = fx
  46. for baris in tabel:
  47.     print('{:^9.0f} {:11.9f} {:>12.4e} '.format(*baris))
  48. print('Akar persamaan = {:.9f} dengan nilai f(x) = {:.4e}'.format(xnew,fnew))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement