_Latihan soal ini dari perkuliahan Analisis Sumber Daya Air (SI-6232) tahun 2018 Semester Genap diajarkan oleh Dr. Dhemi Harlan ST, MT, MSc._

_Kode dibawah ini dibuat oleh Taruma S.M._

# Metoda Secant
> Tentukan solusi positif dari $f(x) = x - 2\ \sin{x} = 0$ dengan metode secant, mulai dari $x_0 = 2, x_1 = 1.9$

Penyelesaian:
Solusi persamaan :
$$x_{n+1} = x_n - \frac{(x_n-2\sin{x_n})(x_n-x_{n-1})}{x_n-x_{n-1}+2(\sin{x_{n-1}}-\sin{x_n})}=x_n-\frac{N_n}{D_n}$$


```python
from math import sin

x0, x1 = 2, 1.9

lebar_text = 10*5+12+2*6+7
print('='*lebar_text)
print('|{:^{n:d}s}|'.format(
    'Solusi Numerik Menggunakan Metoda Secant', n=lebar_text-2))
print('='*(10*5+12+2*6+7))
print('| {:^10s} | {:^10s} | {:^10s} | {:^10s} | {:^10s} | {:^12s} |'.format(
    'n', 'x_{n-1}', 'x_n', 'N_n', 'D_n', 'x_{n+1}-x_n'))
print('='*(10*5+12+2*6+7))

iterasi = 5

xn = x1
xn_m1 = x0
xn_p1 = 0
for n in range(1, iterasi+1):
    Nn = (xn - 2*sin(xn))*(xn - xn_m1)
    Dn = (xn - xn_m1) + 2*(sin(xn_m1) - sin(xn))
    xn_p1 = xn - Nn / Dn
    dx = xn_p1-xn

    print('| {:^10d} | {:^+1.7f} | {:^+1.7f} | {:^+1.7f} | {:^+1.7f} | {:^+1.9f} |'.format(n, xn_m1, xn, Nn, Dn, dx))

    xn_m1 = xn
    xn = xn_p1

print('='*(10*5+12+2*6+7))
print('Maka diperoleh nilai akar-akarnya = {:10.6f}'.format(xn))
```

    =================================================================================
    |                   Solusi Numerik Menggunakan Metoda Secant                    |
    =================================================================================
    |     n      |  x_{n-1}   |    x_n     |    N_n     |    D_n     | x_{n+1}-x_n  |
    =================================================================================
    |     1      | +2.0000000 | +1.9000000 | -0.0007400 | -0.1740053 | -0.004252643 |
    |     2      | +1.9000000 | +1.8957474 | -0.0000018 | -0.0069852 | -0.000252433 |
    |     3      | +1.8957474 | +1.8954949 | -0.0000000 | -0.0004136 | -0.000000658 |
    |     4      | +1.8954949 | +1.8954943 | -0.0000000 | -0.0000011 | -0.000000000 |
    |     5      | +1.8954943 | +1.8954943 | -0.0000000 | -0.0000000 | +0.000000000 |
    =================================================================================
    Maka diperoleh nilai akar-akarnya =   1.895494


# Metode Newton-Rhapson

> Tentukan sebuah akar persamaan dari fungsi berikut: $f(x) = x^3 - 3.5x^2+2x-10$

Solusi:
Persamaan umumnya:
$$\large{x_{k+1} = x_k - \frac{f(x_k)}{f'(x_k)}}$$


```python
# Defining function
def f(x):  # Fungsi f(x)
    return x**3 - 3.5*x**2 + 2*x - 10


def f_d(x):  # Turunan Fungsi f(x) -> f'(x)
    return 3*x**2 - 7*x + 2


iterasi = 6  # Iteration
xk = 3  # x_k

lebar_text = 12*5 + 6
print('='*lebar_text)
print('|{:^{n:d}s}|'.format('Solusi Numerik Metoda Newton Rhapson', n=lebar_text-2))
print('='*lebar_text)
print('| {:^{n:d}s} | {:^{n:d}s} | {:^{n:d}s} | {:^{n:d}s} | {:^{n:d}s} |'.format(
    'k', 'x_k', 'f(x_k)', 'f\'(x_k)', 'x_{k+1}', n=10))
print('='*lebar_text)

for i in range(1, iterasi+1):
    xk_p1 = xk - f(xk)/f_d(xk)
    print('| {:^{n:d}d} | {:^ {f1:d}.{f2:d}f} | {:^ {f1:d}.{f2:d}f} | {:^ {f1:d}.{f2:d}f} | {:^ {f1:d}.{f2:d}f} | '.format(i,
                                                                                                                           xk, f(xk), f_d(xk), xk_p1, n=10, f1=10, f2=5))
    xk = xk_p1
print('='*lebar_text)
print('Maka diperoleh nilai akar-akarnya = {:10.6f}'.format(xk))
```

    ==================================================================
    |              Solusi Numerik Metoda Newton Rhapson              |
    ==================================================================
    |     k      |    x_k     |   f(x_k)   |  f'(x_k)   |  x_{k+1}   |
    ==================================================================
    |     1      |   3.00000  |  -8.50000  |   8.00000  |   4.06250  | 
    |     2      |   4.06250  |   7.40845  |  23.07422  |   3.74143  | 
    |     3      |   3.74143  |   0.86246  |  17.80488  |   3.69299  | 
    |     4      |   3.69299  |   0.01801  |  17.06359  |   3.69193  | 
    |     5      |   3.69193  |   0.00001  |  17.04760  |   3.69193  | 
    |     6      |   3.69193  |   0.00000  |  17.04759  |   3.69193  | 
    ==================================================================
    Maka diperoleh nilai akar-akarnya =   3.691934


# Metode Interval-halving

> Gunakan metode interval-halving, fungsi berikut didapat mempunyai suatu akar real antara x = 1 dan x = 3; tentukan akar dari: $f(x) = x^3-5x^2-2x+10$

Solusi:

Evaluasi fungsi pada kedua nilai awal, $x_1 = 1 \text{ dan } x_2 = 3$
$$\begin{align} x_1 &=& 1 &\rightarrow& f(x_1) &=& 4 \\
x_2 &=& 3 &\rightarrow& f(x_2) &=& -14 \\ \end{align}$$


```python
def f(x):
    return x**3 - 5*x**2 - 2*x + 10

def newline(x=30):
    print('='*x)

x1, x2 = 1, 3
iterasi = 6
xa, xb = x1, x2

pad = 70
newline(pad)
print('Periksa nilai akarnya berada diantara xa dan xb')
if f(xa)*f(xb) < 0:
    print('f(x_a) x f(x_b) < 0 === OK \n\tdengan nilai f(x_a) = {fxa} dan f(x_b) = {fxb}'.format(
        fxa=f(xa), fxb=f(xb)))
else:
    print('f(x_a) x f(x_b) < 0 === NOT OK \n\ttidak akan ditemukan akarnya diantara {xa} dan {xb}'.format(
        xa=xa, xb=xb))
newline(pad)
print('\n')

lebar_text = 12*7 + 8
print('='*lebar_text)
print('|{:^{n:d}s}|'.format('Solusi Numerik Metoda Interval Halving', n=lebar_text-2))
print('='*lebar_text)
print('| {:^{n:d}s} | {:^{n:d}s} | {:^{n:d}s} | {:^{n:d}s} | {:^{n:d}s} | {:^{n:d}s} | {:^{n:d}s} |'.format(
    'n', 'x_a', 'x_b', 'f(x_a)', 'f(x_b)', 'x_h', 'f(x_h)', n=10))
print('='*lebar_text)

for i in range(1, iterasi+1):
    fxa, fxb = f(xa), f(xb)
    xh = (xa+xb)/2
    fxh = f(xh)

    print('| {:^{n:d}d} | {:^ {f1:d}.{f2:d}f} | {:^ {f1:d}.{f2:d}f} | {:^ {f1:d}.{f2:d}f} | {:^ {f1:d}.{f2:d}f} | {:^ {f1:d}.{f2:d}f} | {:^ {f1:d}.{f2:d}f} |'.format(
        i, xa, xb, f(xa), f(xb), xh, f(xh), n=10, f1=10, f2=5))
    if fxa*fxh < 0:
        xb = xh
    elif fxb*fxh < 0:
        xa = xh
print('='*lebar_text)
print('Maka diperoleh nilai akar-akarnya = {:10.6f}'.format(xh))
```

    ======================================================================
    Periksa nilai akarnya berada diantara xa dan xb
    f(x_a) x f(x_b) < 0 === OK 
    	dengan nilai f(x_a) = 4 dan f(x_b) = -14
    ======================================================================
    
    
    ============================================================================================
    |                          Solusi Numerik Metoda Interval Halving                          |
    ============================================================================================
    |     n      |    x_a     |    x_b     |   f(x_a)   |   f(x_b)   |    x_h     |   f(x_h)   |
    ============================================================================================
    |     1      |   1.00000  |   3.00000  |   4.00000  | -14.00000  |   2.00000  |  -6.00000  |
    |     2      |   1.00000  |   2.00000  |   4.00000  |  -6.00000  |   1.50000  |  -0.87500  |
    |     3      |   1.00000  |   1.50000  |   4.00000  |  -0.87500  |   1.25000  |   1.64062  |
    |     4      |   1.25000  |   1.50000  |   1.64062  |  -0.87500  |   1.37500  |   0.39648  |
    |     5      |   1.37500  |   1.50000  |   0.39648  |  -0.87500  |   1.43750  |  -0.23657  |
    |     6      |   1.37500  |   1.43750  |   0.39648  |  -0.23657  |   1.40625  |   0.08072  |
    ============================================================================================
    Maka diperoleh nilai akar-akarnya =   1.406250



```python

```

283d45478237f9960ff0b3a37b04370925c87d41
ipynb
Jupyter Notebook
ansis/Interval-Halving, Newton-Rhapson, Secant (Minggu 15).ipynb
taruma/belajar-tsa
ded702846f0453993ba90738a4b6b2401abe9177
MIT
ansis/Interval-Halving, Newton-Rhapson, Secant (Minggu 15).ipynb
taruma/belajar-tsa
ded702846f0453993ba90738a4b6b2401abe9177
MIT
ansis/Interval-Halving, Newton-Rhapson, Secant (Minggu 15).ipynb
taruma/belajar-tsa
ded702846f0453993ba90738a4b6b2401abe9177
MIT
Qwen/Qwen-72B
1. YES
2. YES