Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Tue Mar 10 07:27:42 2020
- @author: varenza
- """
- # NEWTON RAPHSON
- import numpy as np
- #data
- tol=1e-4
- xold = 0.05
- iterasi = 1
- err = 1
- x = []
- y = []
- #subrutin 1 :F(x)
- def myfun(x):
- F = x**3-0.165*x**2+3.993e-4
- return F
- #subrutin 2 :calc dFx
- def myfun1(x):
- dFx = 3*x**2-0.31*x
- return dFx
- def myfun2(xold):
- err = 1
- while err > tol:
- xnew = xold-myfun(xold)/myfun1(xold)
- residual = (xnew-xold)/xnew
- err = np.abs(residual)
- xold = xnew
- return xnew
- xout=myfun2(xold)
- yout=myfun(xout)
- print('_'*38)
- print('nilai x adalah %.4f'%xout)
- print('nilai F(x) adalah %e'%yout)
- print(38*'_')
- # SECANT
- import numpy as np
- #data
- tol=1e-4
- xold = 0.05
- iterasi = 1
- err = 1
- x = []
- y = []
- #subrutin 1 :F(x)
- def myfun(x):
- F = x**3-0.165*x**2+3.993e-4
- return F
- #subrutin 2 :calc dFx
- def myfun1(x):
- eps = 1e-4
- Fplus=myfun(x+eps)
- Fminus=myfun(x-eps)
- dFx = (Fplus-Fminus)/(2*eps)
- return dFx
- def myfun2(xold):
- err = 1
- while err > tol:
- xnew = xold-myfun(xold)/myfun1(xold)
- residual = (xnew-xold)/xnew
- err = np.abs(residual)
- xold = xnew
- return xnew
- xout=myfun2(xold)
- yout=myfun(xout)
- print('_'*38)
- print('nilai x adalah %.4f'%xout)
- print('nilai F(x) adalah %e'%yout)
- print(38*'_')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement