realice 4 algoritmos de los cuales 2 de ellos me arrojan resultados diferentes, de los que saco personalmente con ayuda de mi calculadora. Los programas son los métodos numericos de lin-bairstow y el de newton rhapson. Me arrojan resultados diferentes en las interacciones
Espero que me digan con que me equivoque y que le cambio para que den los resultados correctos
Newtonwn Rhapson
Código C++:
Ver original#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
//se cicla al hacer iteracciones cuando se le da el margen de error
//los resultados que arroja, no estoy muy seguro que los de bien
//es para polinomios no sirve para euler
float coeficiente[100];
float potencia[100];
int incog;
float evalua_funcion(float);
float evalua_funcion_derivada(float);
void ingresa_ecuacion();
int main()
{
float xi,xr,x_ant,ea=100.0,es;
int i=1,opc,iter;
char resp='s';
printf("BIENVENIDO AL METODO DE \n\t\t\t NEWTON – RAPHSON\n\n"); while(resp=='s' || resp=='S')
{
printf("INGRESE EL VALOR INICIAL: "); while(xi==0)
{
printf("\n\n\t\t\tINGRESE UN VALOR DISTINTO DE CERO: "); }
printf("\n\t\t 1 – SI DESEA INGRESAR UN LIMITE DE ITERACIONES"); printf("\n\t\t 2 – SI PREFIERE INGRESAR EL ERROR TOPE"); printf("\n\n\t\t\t SELECCIONE UNA OPCION: "); switch(opc)
{
case 1:
printf("LIMITE DE ITERACIONES: "); ingresa_ecuacion();
printf("ACCEDENDO AL METODO NEWTON – RAPHSON"); for(int z=25;z<=45;z++)
{
}
printf("Iteracion\t Xr Ea\n"); xr=xi;
while(i<=iter)
{
printf("\n\n\t\t\t %d %.5f %.5f",i
,xr
,ea
); x_ant=xr;
xr =( x_ant -(evalua_funcion(x_ant))/(evalua_funcion_derivada(x_ant)));
ea
=fabs((xr
-x_ant
)/xr
)*0.1;i++;
if(xr<1){
i=i+iter;
}
}
break;
case 2:
printf("INGRESE EL VALOR DEL ERROR TOPE: "); ingresa_ecuacion();
printf("ACCEDIENDO AL METODO NEWTON – RAPHSON"); for(int x=25;x<=45;x++)
{
}
//gotoxy(23,24);
printf("Iteracion\t Xr Ea\n"); xr=xi;
while(ea>=es)
{
printf("\n\n\t\t\t %d %.5f %.5f",i
,xr
,ea
); x_ant=xr;
xr = x_ant -(evalua_funcion(x_ant)/evalua_funcion_derivada(x_ant));
ea
=fabs((xr
-x_ant
)/xr
)*0.1;i++;
if(xr<1){
ea=ea+es;
}
}
break;
default: printf("\n\n\t\t\t\t OPCION INVALIDA"); }//fin de switch
printf("\n\n\n\t\tPARA CONTINUAR PRESIONE S, PARA SALIR PRESIONE N: "); if(resp=='N'|| resp=='n')
{
//clrscr();
//gotoxy(30,24);
break;
}
else
{
i=1;
xr=0;
ea=100;
//clrscr();
}
}//fin de while
}//fin de main
//FUNCION//
void ingresa_ecuacion()
{
int grado,u=0;
int aux;
//gotoxy(22,22);
printf("CUANTAS INCOGNITAS TENDRA SU FUNCION?:_"); aux= incog;
while(incog<=0)
{
printf("\n\n\t\t INGRESE UN NUEVO VALOR_"); }
while(u<incog)
{
printf("\n\n\t\t\tINGRESE EL COEFICIENTE PARA X ^ %d :_",aux
); scanf("%f",&coeficiente
[u
]); potencia[u]=aux;
u++;
aux;
}//fin del for
printf("\n\t\t\t AHORA INGRESE EL TERMINO INDEPENDIENTE:_"); scanf("%f",&coeficiente
[u
+1]); printf("\n\n\n\n\t LA ECUACION INGRESADA ES:\n\n\n");
printf(" %.1f X^ %.f",coeficiente
[0],potencia
[0]);//imprimiendo el primer valor for(u=1;u<=incog;u++) //imprimiendo los
{ //demas valores
if(coeficiente[u]!=0)
{
if(coeficiente[u]>0)
printf(" + %.2f X^ %.2f",coeficiente
[u
],potencia
[u
]); else
printf(" %.2f X^ %.2f",coeficiente
[u
],potencia
[u
]); }//fin del if
}//fin del for
if(coeficiente[incog+1]!=0)
{ //imprimiendo
if(coeficiente[incog+1]>0) // el termino
printf(" + %.2f ",coeficiente
[incog
+1]); //independiente else
printf(" %.2f ",coeficiente
[incog
+1]); }
}//fin de funcion
//FUNCIONES//
float evalua_funcion(float a)
{
float resp;
int b;
resp
=(coeficiente
[0])*(pow(a
,potencia
[0])); // resolviendo el primer factor
for(b=1;b<=incog;b++) //reslviendo desde la segunda
{ //posicion del vector
if(coeficiente[b]!=0)
resp
= resp
+ ((coeficiente
[b
])*(pow(a
,potencia
[b
])));}//fin del for
resp = resp + coeficiente[incog+1]; //sumando algebraicamente
// el termino independiente
return(resp);
}//fin de evalua funcion
float evalua_funcion_derivada(float y)
{
float result=0;
int c;
for(c=0;c<=incog;c++)
{
if(coeficiente[c]!=0 && potencia[c]!=1) //derivando
{
coeficiente[c]=coeficiente[c]*potencia[c];
potencia[c]=potencia[c]-1;
}
}//fin de for
//result=coeficiente[0]*(pow(y,potencia[0])); //resolviendo la primera posicion
for(int a=0;a<=incog-2;a++)
{
if(coeficiente[a]!=0)
result
=result
+ ((coeficiente
[a
])*(pow(y
,potencia
[a
])));}//fin del for
result=result+coeficiente[incog-1];
return(result);
}
Lin-Bairstow
Código C++:
Ver originalpublic static void main(String[] args) {
//double a[] = {-2.5, 5.25, -4, 1};
double a[] ={1.25, -3.875, 2.125, 2.75, -3.5, 1};
int n = a.length;
double re[] = new double[n];
double im[] = new double[n];
Bairstow(a, -1, -1, re, im);
//Bairstow(a, -1, 2, re, im);
}
static public void Bairstow(double a[], double r0, double s0, double re[], double im[])
{
int n = a.length, iter =0;
double b[] = new double[n], c[] = new double[n];
double ea1 = 1, ea2 = 1, T = 0.00001;
double r=r0, s=s0,det, ds, dr;
int MaxIter = 100, i;
for(iter=0; iter< MaxIter && n>3; iter++)
{
do {
Division_Derivada(a, b, c, r, s, n);
/*
System.out.println("Solucionando ");
System.out.print("fn(x) = "); imprime(a, n);
System.out.print("fn-2(x) = "); imprime2(b, n);
System.out.println("f2(x) = x2 + " + r + "x + " + s);
System.out.println(c[2] + " " + c[3] + " = " + -b[1]);
System.out.println(c[1] + " " + c[2] + " = " + -b[0]);
*/
det = c[2]*c[2] - c[3]*c[1];
if(det!=0)
{
dr = (-b[1]*c[2] + b[0]*c[3])/det;
ds = (-b[0]*c[2] + b[1]*c[1])/det;
/*
System.out.println("*********************************");
System.out.println(r+dr +" = " + r + " + " + dr);
System.out.println(s+ds +" = " + s + " + " + ds);
System.out.println("-----------------------------------");*/
r = r+dr;
s = s+ds;
if(r
!=0) ea1
= Math.
abs(dr
/r
)*100.0; if(s
!=0) ea2
= Math.
abs(ds
/s
)*100.0; }
else
{
r = 5*r+1;
s = s+1;
iter = 0;
}
}
while ((ea1 > T) && (ea2 > T));
raices(r, s, re, im, n);
System.out.println("iter " +iter);
System.out.print("fn(x) = "); imprime(a, n);
System.out.print("fn-2(x) = "); imprime2(b, n);
System.out.println("f2(x) = x2 -(" +r + ")x -(" +s +")");
n = n-2;
for(i=0; i<n; i++)
a[i] = b[i+2];
if (n < 4) break;
}
if(n==3)
{
System.out.println("n = " + n);
r = -a[1]/a[2];
s = -a[0]/a[2];
imprime(a, n);
raices(r, s, re, im, n);
}
else
{
re[n-1] = -a[0]/a[1];
im[n-1] = 0;
}
for(i=1; i<re.length; i++)
System.out.println( "X["+i+"]= " + re[i] + " j " + im[i]);
}
public static void Division_Derivada(double a[], double b[], double c[], double r, double s, int n)
{
int i;
b[n-1] = a[n-1];
b[n-2] = a[n-2] + r*b[n-1];
c[n-1] = b[n-1];
c[n-2] = b[n-2] + r*c[n-1];
for(i=n-3; i>=0; i--)
{
b[i] = a[i] + r*b[i+1] + s*b[i+2];
c[i] = b[i] + r*c[i+1] + s*c[i+2];
}
}
public static void imprime(double x[], int n)
{
int i;
for (i = n - 1; i >= 0; i--)
if(x[i] > 0) System.out.print("+ " +x[i] + "x"+i+" ");
else System.out.print("- " + -x[i] + "x"+i+" ");
System.out.println("");
}
public static void imprime2(double x[], int n)
{
int i;
for (i = n - 1; i >= 2; i--)
if(x[i] > 0) System.out.print("+ " +x[i] + "x"+(i-2)+" ");
else System.out.print("- " + -x[i] + "x"+(i-2)+" ");
System.out.println("Residuo = {"+ x[1]+ ", " + x[0] + "}");
}
public static void raices(double r, double s, double re[], double im[], int n)
{
double d = r*r + 4*s;
if(d > 0)
{
re
[n
-1] = (r
+ Math.
sqrt(d
))/2.0; re
[n
-2] = (r
- Math.
sqrt(d
))/2.0; im[n-1] = 0;
im[n-2] = 0;
}
else
{
re[n-1] = r/2.0;
re[n-2] = re[n-1];
im
[n
-1] = Math.
sqrt(-d
)/2.0; im[n-2] = -im[n-1];
}
}
}
este lo hicimos entre un amigo y yo, pero nos funciona del todo