0% found this document useful (0 votes)
0 views

Secant - Polynom - python

The document describes the implementation of the Newton-Raphson and Secant methods for finding roots of functions using Python scripts. It includes iterations and results for both methods, showing the convergence to solutions and the error values. Additionally, it presents a polynomial interpolation using numpy and matplotlib, demonstrating how to fit a polynomial to given data points and visualize the result.

Uploaded by

Affan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Secant - Polynom - python

The document describes the implementation of the Newton-Raphson and Secant methods for finding roots of functions using Python scripts. It includes iterations and results for both methods, showing the convergence to solutions and the error values. Additionally, it presents a polynomial interpolation using numpy and matplotlib, demonstrating how to fit a polynomial to given data points and visualize the result.

Uploaded by

Affan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

return none

newrap(x0,e,imax)

[Running] python -u "e:\Script Python\Metode Newton Raphson.py"


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 Secant.py"


|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 matplotlib.pyplot as plt

x = np.array([1.2,1.4,1.6,1.8,2.0,2.2,2.4,2.6,2.8,3.0])
y = np.array([4.13,4.82,5.57,6.38,7.25,8.18,9.17,10.22,11.33,12.50])
orde = 2

x_new = np.arange(0,200,1)
f = np.poly1d(np.polyfit(x, y, orde))
print('fungsi f =\n',f)
xp = float(2.5)
yp = f(xp)
print('Nilai interpolasi = %0.6f.' % (yp))

mymodel = np.poly1d(np.polyfit(x, y, 2))

myline = np.linspace(1, 3, 50)

plt.title('Polinomial orde 2')


plt.scatter(x, y)
plt.plot(myline, mymodel(myline))
plt.show()

Hasil

fungsi f =
2

You might also like