Advertisement
fenvel

root pak azis

Apr 8th, 2020
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Mar 10 07:27:42 2020
  4.  
  5. @author: varenza
  6. """
  7.  
  8. # NEWTON RAPHSON
  9. import numpy as np
  10. #data
  11. tol=1e-4
  12. xold = 0.05
  13. iterasi = 1
  14. err = 1
  15.  
  16. x = []
  17. y = []
  18. #subrutin 1 :F(x)
  19. def myfun(x):
  20.     F = x**3-0.165*x**2+3.993e-4
  21.     return F
  22.  
  23. #subrutin 2 :calc dFx
  24. def myfun1(x):
  25.     dFx = 3*x**2-0.31*x
  26.     return dFx
  27.  
  28. def myfun2(xold):
  29.     err = 1
  30.     while err > tol:
  31.         xnew = xold-myfun(xold)/myfun1(xold)
  32.         residual = (xnew-xold)/xnew
  33.         err = np.abs(residual)
  34.         xold = xnew
  35.     return xnew
  36.  
  37. xout=myfun2(xold)
  38. yout=myfun(xout)
  39. print('_'*38)
  40. print('nilai x adalah %.4f'%xout)
  41. print('nilai F(x) adalah %e'%yout)
  42. print(38*'_')
  43.  
  44.  
  45. # SECANT
  46.  
  47. import numpy as np
  48. #data
  49. tol=1e-4
  50. xold = 0.05
  51. iterasi = 1
  52. err = 1
  53.  
  54. x = []
  55. y = []
  56. #subrutin 1 :F(x)
  57. def myfun(x):
  58.     F = x**3-0.165*x**2+3.993e-4
  59.     return F
  60.  
  61. #subrutin 2 :calc dFx
  62. def myfun1(x):
  63.     eps = 1e-4
  64.     Fplus=myfun(x+eps)
  65.     Fminus=myfun(x-eps)
  66.     dFx = (Fplus-Fminus)/(2*eps)
  67.     return dFx
  68.  
  69. def myfun2(xold):
  70.     err = 1
  71.     while err > tol:
  72.         xnew = xold-myfun(xold)/myfun1(xold)
  73.         residual = (xnew-xold)/xnew
  74.         err = np.abs(residual)
  75.         xold = xnew
  76.     return xnew
  77.  
  78. xout=myfun2(xold)
  79. yout=myfun(xout)
  80. print('_'*38)
  81. print('nilai x adalah %.4f'%xout)
  82. print('nilai F(x) adalah %e'%yout)
  83. print(38*'_')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement