You are on page 1of 3

Metode numerik Secant

Nama : Nur Fadhlur Rahman

NIM : 21.11.4349

Kelas : 21IF08
In [2]: # Defining function
def f(x):
return x**3 - 5*x - 9

# Implementing secant method

def secant(x0, x1, e, N):


print("SECANT METHOD IMPLEMENTATION")
step = 1
condition = True
while condition:
if f(x0) == f(x1):
print('Devide by zero error!')
break

x2 = x0 - (x1 - x0) * f(x0) / (f(x1) - f(x0))


print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))
x0 = x1
x1 = x2
step = step + 1

if step > N:
print('Not Convergent!')
break

condition = abs(f(x2)) > e

print('Akarnya: %0.8f' % x2)

# Input section
x0 = float(input('Nilai x0: '))
x1 = float(input('Nilai x1: '))
e = float(input('Batas Toleransi: '))
N = int(input('Maximum step: '))

# starting secant method


secant(x0, x1, e, N)

Nilai x0: 4
Nilai x1: 3
Batas Toleransi: 0.001
Maximum step: 450
SECANT METHOD IMPLEMENTATION
Iteration-1, x2 = 2.906250 and f(x2) = 1.015778
Iteration-2, x2 = 2.858257 and f(x2) = 0.059622
Iteration-3, x2 = 2.855264 and f(x2) = 0.001315
Iteration-4, x2 = 2.855197 and f(x2) = 0.000002
Akarnya: 2.85519663

soal Latihan Nomor 1


Cari hasil perkalian dari tiga persamaan berikut (x + 2)^2, (x − 1)^4, (x + 5)

In [3]: from sympy import symbols, expand

x = symbols('x')

# definisikan persamaan
expr1 = (x + 2)**2
expr2 = (x - 1)**4
expr3 = x + 5

# perkalian persamaan
result = expand(expr1 * expr2 * expr3)

print(f'Hasil perkalian persamaan adalah {result}')

Hasil perkalian persamaan adalah x**7 + 5*x**6 - 6*x**5 - 26*x**4 + 29*x**3 + 33*x**2 - 56*x + 20
Soal Latihan Nomor 2
Cari akar dari persamaan 6x^4 + 11x^3 − 56x^2 − x + 60

In [4]: # Defining function


def f(x):
return (6 * (x ** 4)) + (11 * (x ** 3)) - (56 * (x ** 2)) - x + 60

# Implementing secant method


def secant(x0, x1, e, N):
print("SECANT METHOD IMPLEMENTATION")
step = 1
condition = True
while condition:
if f(x0) == f(x1):
print('Devide by zero error!')
break

x2 = x0 - (x1 - x0) * f(x0) / (f(x1) - f(x0))


print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))
x0 = x1
x1 = x2
step = step + 1

if step > N:
print('Not Convergent!')
break

condition = abs(f(x2)) > e

print('Akarnya: %0.8f' % x2)

# Input section
x0 = float(input('Nilai x0: '))
x1 = float(input('Nilai x1: '))
e = float(input('Batas Toleransi: '))
N = int(input('Maximum step: '))

# starting secant method


secant(x0, x1, e, N)

Nilai x0: 4
Nilai x1: 9
Batas Toleransi: 0.0001
Maximum step: 100
SECANT METHOD IMPLEMENTATION
Iteration-1, x2 = 3.831325 and f(x2) = 1145.631879
Iteration-2, x2 = 3.689510 and f(x2) = 958.267361
Iteration-3, x2 = 2.964203 and f(x2) = 314.702191
Iteration-4, x2 = 2.609530 and f(x2) = 149.747673
Iteration-5, x2 = 2.287553 and f(x2) = 60.645323
Iteration-6, x2 = 2.068407 and f(x2) = 25.511971
Iteration-7, x2 = 1.909275 and f(x2) = 10.242162
Iteration-8, x2 = 1.802538 and f(x2) = 4.010779
Iteration-9, x2 = 1.733838 and f(x2) = 1.477287
Iteration-10, x2 = 1.693778 and f(x2) = 0.483472
Iteration-11, x2 = 1.674290 and f(x2) = 0.120973
Iteration-12, x2 = 1.667786 and f(x2) = 0.017045
Iteration-13, x2 = 1.666720 and f(x2) = 0.000803
Iteration-14, x2 = 1.666667 and f(x2) = 0.000006
Akarnya: 1.66666705

Latihan Soal Nomor 3


Cari penyelesaian persamaan x^4 − 5x^3 + 3x^2 + x = 0
In [6]: # Defining function
def f(x):
return (x ** 4) - (5 * (x ** 3)) + (3 * (x ** 2)) + x

# Implementing secant method


def secant(x0, x1, e, N):
print("SECANT METHOD IMPLEMENTATION")
step = 1
condition = True
while condition:
if f(x0) == f(x1):
print('Devide by zero error!')
break

x2 = x0 - (x1 - x0) * f(x0) / (f(x1) - f(x0))


print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))
x0 = x1
x1 = x2
step = step + 1

if step > N:
print('Not Convergent!')
break

condition = abs(f(x2)) > e

print('Akarnya: %0.8f' % x2)

# Input section
x0 = float(input('Nilai x0: '))
x1 = float(input('Nilai x1: '))
e = float(input('Batas Toleransi: '))
N = int(input('Maximum step: '))

# starting secant method


secant(x0, x1, e, N)

Nilai x0: 2
Nilai x1: 3
Batas Toleransi: 0.0001
Maximum step: 100
SECANT METHOD IMPLEMENTATION
Iteration-1, x2 = 1.285714 and f(x2) = -1.649313
Iteration-2, x2 = 1.159213 and f(x2) = -0.792337
Iteration-3, x2 = 1.042253 and f(x2) = -0.179796
Iteration-4, x2 = 1.007922 and f(x2) = -0.032066
Iteration-5, x2 = 1.000470 and f(x2) = -0.001883
Iteration-6, x2 = 1.000006 and f(x2) = -0.000022
Akarnya: 1.00000553

You might also like