return none
newrap(x0,e,imax)
[Running] python -u "e:\Script Python\Metode Newton [Link]"
Iterasi ke 1, xn = 4.25000000, dan f(xn)=7.56250000
Iterasi ke 2, xn = 3.08653846, dan f(xn)=1.35364275
Iterasi ke 3, xn = 2.76216324, dan f(xn)=0.10521928
Iterasi ke 4, xn = 2.73230809, dan f(xn)=0.00089133
Iterasi ke 5, xn = 2.73205083, dan f(xn)=0.00000007
solusi pada iterasi ke- 5
[Done] exited with code=0 in 0.255 seconds
Metode Secant
Script :
import numpy as np
# x0=a dan x1=b nilai awal
a=1.5
b=3
eps=0.0001 #minimal error
def f(a): #program fungsi
f= a**2-2*a-2 #fungsi awal f(x)
return f
print('|Iterasi|\tf(c)\t |') #keterangan
for i in range (20):
c=b-((f(b)*(b-a))/(f(b)-f(a))) #rumus xr+1
e =abs(c-b) #|xr+1 − xr|
if abs (c-b)> eps:
b=c
print('|\t%d\t|%e\t|' %(i+1, abs(c)))
elif abs (c-b)< eps:
print('Solusi Pada Iterasi ke- :',i+1)
print('Nilai Solusi :',c)
break
print('Nilai Error :',e)
Hasil:
[Running] python -u "e:\Script Python\Metode [Link]"
|Iterasi| f(c) |
| 1 |2.600000e+00 |
| 2 |2.809524e+00 |
| 3 |2.690722e+00 |
| 4 |2.755294e+00 |
| 5 |2.719353e+00 |
| 6 |2.739100e+00 |
| 7 |2.728172e+00 |
| 8 |2.734195e+00 |
| 9 |2.730868e+00 |
| 10 |2.732704e+00 |
| 11 |2.731690e+00 |
| 12 |2.732250e+00 |
| 13 |2.731941e+00 |
| 14 |2.732111e+00 |
Solusi Pada Iterasi ke- : 15
Nilai Solusi : 2.7320173449862164
Nilai Error : 9.408687902778823e-05
[Done] exited with code=0 in 0.495 seconds
4. Polinomial
import numpy as np
import [Link] as plt
x = [Link]([1.2,1.4,1.6,1.8,2.0,2.2,2.4,2.6,2.8,3.0])
y = [Link]([4.13,4.82,5.57,6.38,7.25,8.18,9.17,10.22,11.33,12.50])
orde = 2
x_new = [Link](0,200,1)
f = np.poly1d([Link](x, y, orde))
print('fungsi f =\n',f)
xp = float(2.5)
yp = f(xp)
print('Nilai interpolasi = %0.6f.' % (yp))
mymodel = np.poly1d([Link](x, y, 2))
myline = [Link](1, 3, 50)
[Link]('Polinomial orde 2')
[Link](x, y)
[Link](myline, mymodel(myline))
[Link]()
Hasil
fungsi f =
2