Método newton - raphson
#include <iostream>
//#include "ionanip"
#include <math.h>
#define f1(x) (1.0*pow(x,5) - 4.0*pow(x,4)+5.0*pow(x,3)-6.0*x+4.0) //FUNCION
ORIGINAL
#define fp(x) (5.0*pow(x,4)-16.0*pow(x,3)+15.0*pow(x,2)-6.0) //DERIVADA DE LA
FUNCION
double newton_raphson(double x1, double es, int iter); //(PUNTO INICIAL, ERROR DE
CONVERGENCIA,NUMERO DE ITERACIONES)
using namespace std;
int main()
{
int iter;
double es, x1, raiz;
cout << "--------programa metodo de newton-raphson-------";
cout << "nota: se especifica el numero de iteraciones si el metodo converge
antes" << endl;
cout << "el programa hara una notificacion";
cout << "punto inicial(xi)";
cin >> x1;
cout << endl;
cout << "error de convergencia (dado por unidad):";
cin >> es;
cout << endl;
cout << "numero de iteraciones (entre 100 y 150";
cin >> iter;
cout << endl;
//BANDERAS CON FUNCIONES DE FLUJO
[Link](ios::fixed); //PUNTO FIJO
[Link](ios::showpoint); //INCLUYE UN PUNTO DECIMAL EN NUMEROS EN COMA
FLOTANTE
[Link](ios::showpos); //SIGNO + PARA NUMEROS POSITIVOS
[Link](5);
raiz = newton_raphson(x1, es, iter);
cout << "la raiz es: " << raiz;
return 0;
}
double newton_raphson(double x1, double es, int iter)
{
double x2, ea, y1, yp1;
cout << "iteracion" << " " << "xi" << " " << "xi+1" << " "
<< "Ea" << " " << "f(xi)" << " " << "f'(xi)" << endl;
for (int cont = 1; cont <= iter; cont++)
{
y1 = f1(x1); //FUNCION EVALUADA EN LA FUNCION ORIGINAL
yp1 = fp(x1); //FUNCION EVALUADA EN LA DERIVADA DE LA FUNCION
if (yp1 == 0)
{
cout << "hay una division entre cero. f'(xi)-0" << endl;
break;
}
x2 = x1 - (y1 / yp1);
ea = fabs((x2 - x1) / x2)*100;
cout << " " << cont << " " << x1 << " " << x2 << " " << ea <<
" " << y1 << " " << yp1 << endl;
x1 = x2;
if (ea < es)
{
cout << "el metodo converge a" << cont << "iteraciones" << endl;
return x2;
break;
}
if (ea > es)
{
cout << "el metodo no converge para los " << iter << "iteraciones
indicadas. aumente el numero de " << "iteraciones, use otro punto inicial o en su
defecto; use otro metodo numerico";
}