# Método de Newton-Raphson

El método de Newton-Raphson o método de la tangente usa la derivada de la función

$$x_{i+1} = x_{i} - \frac{1}{f'(x_{i})} f(x_{i})$$

Algoritmo

x_0 es la raiz aproximada
x_1 = x_0 - f(x_0)/f'(x_0)
x_2 = x_1 - f(x_1)/f'(x_1)
x_3 = x_2 - f(x_2)/f'(x_2)
...



## Ejemplo 1

Encontrar la raiz de

\begin{equation*} y = x^{3} - 2 x^{2} + x - 3 \end{equation*}

\begin{equation*} y' = 3 x^{2} - 4 x + 1 \end{equation*}

usar $x = 0$ como valor inicial

### Iteración 0

\begin{equation*} x_{0} = 0 \end{equation*}

Error relativo

\begin{equation*} e_{r} = ? \end{equation*}

### Iteración 1

\begin{align*} f(x_{0}) &= f(0) = -3 \\ f'(x_{0}) &= f'(0) = 1 \end{align*}

\begin{equation*} x_{1} = x_{0} - \frac{f(x_{0})}{f'(x_{0})} = 0 - \frac{-3}{1} = 3 \end{equation*}

Error relativo

\begin{equation*} e_{r} = \bigg|\frac{x_{1} - x_{0}}{x_{1}}\bigg| \times 100\% = \bigg|\frac{3 - 0}{3}\bigg| \times 100\% = 100\% \end{equation*}

### Iteración 2

\begin{align*} f(x_{1}) &= f(3) = 9 \\ f'(x_{1}) &= f'(3) = 16 \end{align*}

\begin{equation*} x_{2} = x_{1} - \frac{f(x_{1})}{f'(x_{1})} = 3 - \frac{9}{16} = 2.4375 \end{equation*}

Error relativo

\begin{equation*} e_{r} = \bigg|\frac{x_{2} - x_{1}}{x_{2}}\bigg| \times 100\% = \bigg|\frac{2.4375 - 3}{2.4375}\bigg| \times 100\% = 23.08\% \end{equation*}

### Iteración 3

\begin{align*} f(x_{2}) &= f(2.4375) = 2.036865 \\ f'(x_{2}) &= f'(2.4375) = 9.074219 \end{align*}

\begin{equation*} x_{3} = x_{2} - \frac{f(x_{2})}{f'(x_{2})} = 2.4375 - \frac{2.036865}{9.074219} = 2.213033 \end{equation*}

Error relativo

\begin{equation*} e_{r} = \bigg|\frac{x_{3} - x_{2}}{x_{3}}\bigg| \times 100\% = \bigg|\frac{2.213033 - 2.4375}{2.213033}\bigg| \times 100\% = 10.14\% \end{equation*}

Seudocódigo

function newton_rahpson(f(x), f'(x), x_0)
x_actual = x_0
error_permitido = 0.000001
while(True)
x_anterior = x_actual
x_actual = x_anterior - f(x_anterior)/f'(x_anterior)
if x_raiz_actual != 0
error_relativo = abs((x_raiz_actual - x_raiz_anterior)/x_raiz_actual)*100
end if
if error_relativo < error_permitido
exit
end if
end while
mostrar x_actual
end function


o también

function newton_rahpson(f(x), f'(x), x_0)
x_actual = x_0
for 1 to maxima_iteracion do
x_anterior = x_actual
x_actual = x_anterior - f(x_anterior)/f'(x_anterior)
end for
mostrar x_actual
end function



In [1]:

def newton_rahpson(f, fx, x_0):
print("{0:s} \t {1:15s} \t {2:15s} \t {3:15s}".format('i', 'x anterior', 'x actual', 'error relativo %'))
x_actual = x_0
i = 0
print("{0:d} \t {1:15s} \t {2:.15f} \t {3:15s}".format(i, '???????????????', x_actual, '???????????????'))
error_permitido = 0.000001
while True:
x_anterior = x_actual
x_actual = x_anterior - f(x_anterior)/fx(x_anterior)
if x_actual != 0:
error_relativo = abs((x_actual - x_anterior)/x_actual)*100
i = i + 1
print("{0:d} \t {1:.15f} \t {2:.15f} \t {3:15.11f}".format(i, x_anterior, x_actual, error_relativo))
if (error_relativo < error_permitido) or (i>=20):
break
print('\nx =', x_actual)




In [2]:

def f(x):
# f(x) = x^3 - 2x^2 + x - 3
y = x**3 - 2*x**2 + x - 3
return y

def fx(x):
# f'(x) = 3x^2 - 4x + 1
y = 3*x**2 - 4*x + 1
return y




In [3]:

newton_rahpson(f, fx, 0)




i 	 x anterior      	 x actual        	 error relativo %
0 	 ??????????????? 	 0.000000000000000 	 ???????????????
1 	 0.000000000000000 	 3.000000000000000 	 100.00000000000
2 	 3.000000000000000 	 2.437500000000000 	  23.07692307692
3 	 2.437500000000000 	 2.213032716315110 	  10.14297177037
4 	 2.213032716315110 	 2.175554938721488 	   1.72267668017
5 	 2.175554938721488 	 2.174560100666446 	   0.04574893353
6 	 2.174560100666446 	 2.174559410293313 	   0.00003174772
7 	 2.174559410293313 	 2.174559410292980 	   0.00000000002

x = 2.17455941029298