Foros del Web » Programación para mayores de 30 ;) » C/C++ »

error de calculo con métodos numéricos

Estas en el tema de error de calculo con métodos numéricos en el foro de C/C++ en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 13/09/2012, 20:42
Avatar de Tyreal  
Fecha de Ingreso: noviembre-2011
Ubicación: ke-li
Mensajes: 39
Antigüedad: 12 años, 5 meses
Puntos: 0
error de calculo con métodos numéricos

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
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. #include<dos.h>
  5. //se cicla al hacer iteracciones cuando se le da el margen de error
  6. //los resultados que arroja, no estoy muy seguro que los de bien
  7. //es para polinomios no sirve para euler
  8. float coeficiente[100];
  9. float potencia[100];
  10. int incog;
  11.  
  12. float evalua_funcion(float);
  13. float evalua_funcion_derivada(float);
  14. void ingresa_ecuacion();
  15.  
  16. int main()
  17. {
  18. float xi,xr,x_ant,ea=100.0,es;
  19. int i=1,opc,iter;
  20. char resp='s';
  21.  
  22.  
  23. printf("BIENVENIDO AL METODO DE \n\t\t\t NEWTON – RAPHSON\n\n");
  24. while(resp=='s' || resp=='S')
  25. {
  26. printf("INGRESE EL VALOR INICIAL: ");
  27. scanf("%f",&xi);
  28. while(xi==0)
  29. {
  30. printf("\n\n\t\t\tINGRESE UN VALOR DISTINTO DE CERO: ");
  31. scanf("%f",&xi);
  32. }
  33. printf("\n\t\t\t\t ESCOJA");
  34. printf("\n\t\t 1 – SI DESEA INGRESAR UN LIMITE DE ITERACIONES");
  35. printf("\n\t\t 2 – SI PREFIERE INGRESAR EL ERROR TOPE");
  36. printf("\n\n\t\t\t SELECCIONE UNA OPCION: ");
  37. scanf("%d",&opc);
  38. switch(opc)
  39. {
  40. case 1:
  41. printf("LIMITE DE ITERACIONES: ");
  42. scanf("%d",&iter);
  43. ingresa_ecuacion();
  44.  
  45. printf("ACCEDENDO AL METODO NEWTON – RAPHSON");
  46. for(int z=25;z<=45;z++)
  47. {
  48.  
  49. printf("**");
  50.  
  51. }
  52.  
  53. printf("Iteracion\t Xr Ea\n");
  54. xr=xi;
  55. while(i<=iter)
  56. {
  57. printf("\n\n\t\t\t %d %.5f %.5f",i,xr,ea);
  58. x_ant=xr;
  59. xr =( x_ant -(evalua_funcion(x_ant))/(evalua_funcion_derivada(x_ant)));
  60. ea=fabs((xr-x_ant)/xr)*0.1;
  61. i++;
  62.     if(xr<1){
  63.          i=i+iter;
  64.          }
  65.  
  66. }
  67.  
  68. break;
  69.  
  70. case 2:
  71. printf("INGRESE EL VALOR DEL ERROR TOPE: ");
  72. scanf("%f",&es);
  73. ingresa_ecuacion();
  74.  
  75. printf("ACCEDIENDO AL METODO NEWTON – RAPHSON");
  76. for(int x=25;x<=45;x++)
  77. {
  78.  
  79. printf("Å");
  80.  
  81. }
  82. //gotoxy(23,24);
  83. printf("Iteracion\t Xr Ea\n");
  84. xr=xi;
  85. while(ea>=es)
  86. {
  87. printf("\n\n\t\t\t %d %.5f %.5f",i,xr,ea);
  88. x_ant=xr;
  89. xr = x_ant -(evalua_funcion(x_ant)/evalua_funcion_derivada(x_ant));
  90. ea=fabs((xr-x_ant)/xr)*0.1;
  91. i++;
  92. if(xr<1){
  93.          ea=ea+es;
  94.          }
  95. }
  96. break;
  97.  
  98. default: printf("\n\n\t\t\t\t OPCION INVALIDA");
  99. }//fin de switch
  100. printf("\n\n\n\t\tPARA CONTINUAR PRESIONE S, PARA SALIR PRESIONE N: ");
  101. scanf("%s",&resp);
  102. if(resp=='N'|| resp=='n')
  103. {
  104. //clrscr();
  105. //gotoxy(30,24);
  106. printf("Bye");
  107. break;
  108. }
  109. else
  110. {
  111. i=1;
  112. xr=0;
  113. ea=100;
  114. //clrscr();
  115. }
  116. }//fin de while
  117. }//fin de main
  118.  
  119. //FUNCION//
  120. void ingresa_ecuacion()
  121. {
  122. int grado,u=0;
  123. int aux;
  124. //gotoxy(22,22);
  125. printf("CUANTAS INCOGNITAS TENDRA SU FUNCION?:_");
  126. scanf("%d",&incog);
  127. aux= incog;
  128. while(incog<=0)
  129. {
  130. printf("\n\n\t\t INGRESE UN NUEVO VALOR_");
  131. scanf("%d",&incog);
  132. }
  133.  
  134. while(u<incog)
  135. {
  136. printf("\n\n\t\t\tINGRESE EL COEFICIENTE PARA X ^ %d :_",aux);
  137. scanf("%f",&coeficiente[u]);
  138. potencia[u]=aux;
  139. u++;
  140. aux;
  141. }//fin del for
  142.  
  143. printf("\n\t\t\t AHORA INGRESE EL TERMINO INDEPENDIENTE:_");
  144. scanf("%f",&coeficiente[u+1]);
  145. printf("\n\n\n\n\t LA ECUACION INGRESADA ES:\n\n\n");
  146.  
  147. printf(" %.1f X^ %.f",coeficiente[0],potencia[0]);//imprimiendo el primer valor
  148. for(u=1;u<=incog;u++) //imprimiendo los
  149. { //demas valores
  150. if(coeficiente[u]!=0)
  151. {
  152. if(coeficiente[u]>0)
  153. printf(" + %.2f X^ %.2f",coeficiente[u],potencia[u]);
  154. else
  155. printf(" %.2f X^ %.2f",coeficiente[u],potencia[u]);
  156. }//fin del if
  157. }//fin del for
  158. if(coeficiente[incog+1]!=0)
  159. { //imprimiendo
  160. if(coeficiente[incog+1]>0) // el termino
  161. printf(" + %.2f ",coeficiente[incog+1]); //independiente
  162. else
  163. printf(" %.2f ",coeficiente[incog+1]);
  164. }
  165.  
  166. }//fin de funcion
  167. //FUNCIONES//
  168.  
  169. float evalua_funcion(float a)
  170. {
  171. float resp;
  172. int b;
  173.  
  174. resp=(coeficiente[0])*(pow(a,potencia[0])); // resolviendo el primer factor
  175.  
  176. for(b=1;b<=incog;b++) //reslviendo desde la segunda
  177. { //posicion del vector
  178. if(coeficiente[b]!=0)
  179. resp = resp + ((coeficiente[b])*(pow(a,potencia[b])));
  180. }//fin del for
  181.  
  182. resp = resp + coeficiente[incog+1]; //sumando algebraicamente
  183. // el termino independiente
  184. return(resp);
  185. }//fin de evalua funcion
  186.  
  187. float evalua_funcion_derivada(float y)
  188. {
  189. float result=0;
  190. int c;
  191.  
  192. for(c=0;c<=incog;c++)
  193. {
  194. if(coeficiente[c]!=0 && potencia[c]!=1) //derivando
  195. {
  196. coeficiente[c]=coeficiente[c]*potencia[c];
  197. potencia[c]=potencia[c]-1;
  198. }
  199. }//fin de for
  200.  
  201. //result=coeficiente[0]*(pow(y,potencia[0])); //resolviendo la primera posicion
  202. for(int a=0;a<=incog-2;a++)
  203. {
  204. if(coeficiente[a]!=0)
  205. result=result + ((coeficiente[a])*(pow(y,potencia[a])));
  206. }//fin del for
  207. result=result+coeficiente[incog-1];
  208.  
  209. return(result);
  210. }
Lin-Bairstow
Código C++:
Ver original
  1. public static void main(String[] args) {
  2.     //double a[] = {-2.5, 5.25, -4, 1};
  3.     double a[] ={1.25, -3.875, 2.125, 2.75, -3.5, 1};
  4.     int n = a.length;
  5.     double re[] = new double[n];
  6.     double im[] = new double[n];
  7.  
  8.     Bairstow(a, -1, -1, re, im);
  9.     //Bairstow(a, -1, 2, re, im);
  10.   }
  11.  
  12.   static public void Bairstow(double a[], double r0, double s0, double re[], double im[])
  13.   {
  14.     int n = a.length, iter =0;
  15.     double b[] = new double[n], c[] = new double[n];
  16.     double ea1 = 1, ea2 = 1, T = 0.00001;
  17.     double r=r0, s=s0,det, ds, dr;
  18.     int MaxIter = 100, i;
  19.  
  20.    for(iter=0; iter< MaxIter && n>3; iter++)
  21.     {
  22.       do {
  23.         Division_Derivada(a, b, c, r, s, n);
  24.  
  25. /*
  26.         System.out.println("Solucionando ");
  27.         System.out.print("fn(x) = "); imprime(a, n);
  28.         System.out.print("fn-2(x) = "); imprime2(b, n);
  29.         System.out.println("f2(x) = x2 + " + r + "x + " + s);
  30.         System.out.println(c[2] + " " + c[3] + " = " + -b[1]);
  31.         System.out.println(c[1] + " " + c[2] + " = " + -b[0]);
  32. */
  33.         det = c[2]*c[2] - c[3]*c[1];
  34.         if(det!=0)
  35.         {
  36.           dr = (-b[1]*c[2] + b[0]*c[3])/det;
  37.           ds = (-b[0]*c[2] + b[1]*c[1])/det;
  38.           /*
  39.           System.out.println("*********************************");
  40.           System.out.println(r+dr +" = " + r + " + " + dr);
  41.           System.out.println(s+ds +" = " + s + " + " + ds);
  42.           System.out.println("-----------------------------------");*/
  43.           r = r+dr;
  44.           s = s+ds;
  45.           if(r!=0) ea1 = Math.abs(dr/r)*100.0;
  46.           if(s!=0) ea2 = Math.abs(ds/s)*100.0;
  47.         }
  48.         else
  49.         {
  50.           r = 5*r+1;
  51.           s = s+1;
  52.           iter = 0;
  53.         }
  54.       }
  55.       while ((ea1 > T) && (ea2 > T));
  56.       raices(r, s, re, im, n);
  57.       System.out.println("iter " +iter);
  58.       System.out.print("fn(x) = "); imprime(a, n);
  59.       System.out.print("fn-2(x) = "); imprime2(b, n);
  60.       System.out.println("f2(x) = x2 -(" +r + ")x -(" +s +")");
  61.       n = n-2;
  62.       for(i=0; i<n; i++)
  63.         a[i] = b[i+2];
  64.       if (n < 4) break;
  65.     }
  66.     if(n==3)
  67.     {
  68.       System.out.println("n = " + n);
  69.       r = -a[1]/a[2];
  70.       s = -a[0]/a[2];
  71.       imprime(a, n);
  72.       raices(r, s, re, im, n);
  73.     }
  74.     else
  75.     {
  76.       re[n-1] = -a[0]/a[1];
  77.       im[n-1] = 0;
  78.     }
  79.     for(i=1; i<re.length; i++)
  80.       System.out.println( "X["+i+"]= " + re[i] + " j " + im[i]);
  81.   }
  82.  
  83.   public static void Division_Derivada(double a[], double b[], double c[], double r, double s, int n)
  84.   {
  85.     int i;
  86.  
  87.     b[n-1] = a[n-1];
  88.     b[n-2] = a[n-2] + r*b[n-1];
  89.  
  90.     c[n-1] = b[n-1];
  91.     c[n-2] = b[n-2] + r*c[n-1];
  92.  
  93.     for(i=n-3; i>=0; i--)
  94.     {
  95.       b[i] = a[i] + r*b[i+1] + s*b[i+2];
  96.       c[i] = b[i] + r*c[i+1] + s*c[i+2];
  97.     }
  98.  
  99.   }
  100.  
  101.   public static void imprime(double x[], int n)
  102.   {
  103.     int i;
  104.  
  105.     for (i = n - 1; i >= 0; i--)
  106.       if(x[i] > 0) System.out.print("+ " +x[i] + "x"+i+" ");
  107.       else System.out.print("- " + -x[i] + "x"+i+" ");
  108.     System.out.println("");
  109.   }
  110.  
  111.   public static void imprime2(double x[], int n)
  112.   {
  113.     int i;
  114.  
  115.     for (i = n - 1; i >= 2; i--)
  116.       if(x[i] > 0) System.out.print("+ " +x[i] + "x"+(i-2)+" ");
  117.       else System.out.print("- " + -x[i] + "x"+(i-2)+" ");
  118.     System.out.println("Residuo = {"+ x[1]+ ", " + x[0] + "}");
  119.  
  120.   }
  121.  
  122.   public static void raices(double r, double s, double re[], double im[], int n)
  123.   {
  124.     double d = r*r + 4*s;
  125.     if(d > 0)
  126.     {
  127.       re[n-1] = (r + Math.sqrt(d))/2.0;
  128.       re[n-2] = (r - Math.sqrt(d))/2.0;
  129.       im[n-1] = 0;
  130.       im[n-2] = 0;
  131.     }
  132.     else
  133.     {
  134.       re[n-1] = r/2.0;
  135.       re[n-2] = re[n-1];
  136.       im[n-1] = Math.sqrt(-d)/2.0;
  137.       im[n-2] = -im[n-1];
  138.     }
  139.   }
  140. }
este lo hicimos entre un amigo y yo, pero nos funciona del todo

Etiquetas: calculo, funcion, int, programa, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:40.