home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / extra18 / grdlagen / gleichg / nlin.c next >
Encoding:
C/C++ Source or Header  |  1992-01-22  |  4.9 KB  |  195 lines

  1. /* ------------------------------------------------- */
  2. /*                       NLIN.C                      */
  3. /*      (c) 1992 Klaus Röbenack & DMV-Verlag         */
  4. /*         Lösung nichtlinearer Gleichungen          */
  5. /* ------------------------------------------------- */
  6.  
  7. #ifndef _MATH_H         /* ggf. Bibliothek einbinden */
  8. #include <math.h>
  9. #endif
  10.  
  11. #define EPSILON 0.00001  /* Abbruch-Schranke         */
  12. #define MAX_ANZ 200      /* max. Durchläufe          */
  13.  
  14. /* ------------------------------------------------- */
  15. /*   Iterations-Verfahren für F(x)=x                 */
  16. /* ------------------------------------------------- */
  17.  
  18. float  IterationsVerfahren(FUNKTION, STARTWERT)
  19.   float  (*FUNKTION)(float); /* num. Teilfunktion    */
  20.   float  STARTWERT;          /* Anfangswert für Arg. */
  21. {
  22.   int   i;
  23.   float x, y;
  24.  
  25.   x=STARTWERT;
  26.   for (i=1; i < MAX_ANZ; i++)
  27.   {
  28.     y = FUNKTION(x);
  29.     if (fabs(x-y) < EPSILON) break;
  30.     x = y;
  31.   };
  32.   return(x);
  33. }
  34.  
  35. /* ------------------------------------------------- */
  36. /*   Verfahren nach Newton / Raphson                 */
  37. /* ------------------------------------------------- */
  38.  
  39. float NewtonVerfahren (FUNKTION, ABLEITUNG, STARTWERT)
  40.   float (*FUNKTION)(float);  /* numerische Funktion  */
  41.   float (*ABLEITUNG)(float); /* erste Ableitung      */
  42.   float STARTWERT;           /* Anfangswert des Arg. */
  43. {
  44.   int   i;
  45.   float x0, x1, y;
  46.  
  47.   x0 = STARTWERT;
  48.   for (i=1; i < MAX_ANZ; i++)
  49.   {
  50.     y  = FUNKTION(x0);
  51.     x1 = x0 - y / ABLEITUNG(x0);
  52.     if (fabs(y) < EPSILON) break;
  53.     x0 = x1;
  54.   };
  55.   return(x1);
  56. }
  57.  
  58. /* ------------------------------------------------- */
  59. /*  Regula - Falsi                                   */
  60. /* ------------------------------------------------- */
  61.  
  62. float SekandenVerfahren (FUNKTION, STARTWERT1,
  63.                          STARTWERT2)
  64.   float (*FUNKTION)(float);   /* numerische Funktion */
  65.   float STARTWERT1,STARTWERT2; /* zwei versch. Werte */
  66. {
  67.   int   i;
  68.   float x0, x1, x2, y0, y1;
  69.  
  70.   x0 = STARTWERT1;
  71.   x1 = STARTWERT2;
  72.   y0 = FUNKTION(x0);
  73.   for (i=1; i < MAX_ANZ; i++)
  74.   {
  75.     y1 = FUNKTION(x1);
  76.     if (fabs(y1)<EPSILON) break;
  77.     x2 = x1 - y1 * (x1 - x0) / (y1 - y0);
  78.     x0 = x1;
  79.     x1 = x2;
  80.     y0 = y1;
  81.   };
  82.   return(x2);
  83. }
  84.  
  85. /* ------------------------------------------------- */
  86. /*  Intervall - Halbierung                           */
  87. /* ------------------------------------------------- */
  88.  
  89. float   HalbierungsVerfahren (FUNKTION, UNTERE_GRENZE,
  90.                               OBERE_GRENZE)
  91.   float (*FUNKTION)(float);   /* numerische Funktion */
  92.   float *UNTERE_GRENZE;   /* Arg. mit negativem Wert */
  93.   float *OBERE_GRENZE;    /* Arg. mit positivem Wert */
  94. {
  95.   int   i;
  96.   float x1, x2, x3, y1, y3;
  97.  
  98.   x1 = *UNTERE_GRENZE;
  99.   x2 = *OBERE_GRENZE;
  100.   y1 = FUNKTION(x1);
  101.   for (i=1; i < MAX_ANZ; i++)
  102.   {
  103.     x3 = (x1+x2) / 2;
  104.     y3 = FUNKTION(x3);
  105.     if (y1*y3<0)
  106.       x2 = x3;
  107.     else {
  108.       x1 = x3;
  109.       y1 = y3;
  110.     };
  111.     if (fabs(y3) < EPSILON) break;
  112.   };
  113.   *UNTERE_GRENZE = x1;
  114.   *OBERE_GRENZE  = x2;
  115.   return(x3);
  116. }
  117.  
  118. /* ------------------------------------------------- */
  119. /*  Einschränkende Regula - Falsi                    */
  120. /* ------------------------------------------------- */
  121.  
  122. float SehnenVerfahren (FUNKTION, UNTERE_GRENZE,
  123.                        OBERE_GRENZE)
  124.   float (*FUNKTION)(float);
  125.   float *UNTERE_GRENZE, *OBERE_GRENZE;
  126. {
  127.   int   i;
  128.   float x1, x2, x3, y1, y2, y3;
  129.  
  130.   x1 = *UNTERE_GRENZE;
  131.   y1 = FUNKTION(x1);
  132.   x2 = *OBERE_GRENZE;
  133.   y2 = FUNKTION(x2);
  134.   for (i=1; i < MAX_ANZ; i++)
  135.   {
  136.     x3 = x1 - y1 * (x2 - x1) / (y2 - y1);
  137.     y3 = FUNKTION(x3);
  138.     if (y1*y3 < 0) {
  139.       x1 = x3;
  140.       y1 = y3; }
  141.     else {
  142.       x2 = x3;
  143.       y2 = y3;
  144.     };
  145.     if (fabs(y3) < EPSILON) break;
  146.   };
  147.   *UNTERE_GRENZE = x1;
  148.   *OBERE_GRENZE  = x2;
  149.   return(x3);
  150. }
  151.  
  152. /* ------------------------------------------------- */
  153. /*   Pegasus - Algorithmus                           */
  154. /* ------------------------------------------------- */
  155.  
  156. float PegasusVerfahren(FUNKTION, UNTERE_GRENZE,
  157.                        OBERE_GRENZE)
  158.   float (*FUNKTION)(float);
  159.   float *UNTERE_GRENZE, *OBERE_GRENZE;
  160. {
  161.   int   i;
  162.   float x1, x2, x3, y1, y2, y3;
  163.  
  164.   x1 = *UNTERE_GRENZE;
  165.   y1 = FUNKTION(x1);
  166.   x2 = *OBERE_GRENZE;
  167.   y2 = FUNKTION(x2);
  168.   for (i=1; i < MAX_ANZ; i++)
  169.   {
  170.     x3 = x1 - y1 * (x2-x1) / (y2-y1);
  171.     y3 = FUNKTION(x3);
  172.     if (fabs(y3) < EPSILON) break;
  173.     if (y1*y3 < 0)
  174.       y2 = y1 * y2 / (y1 + y3);
  175.     else {
  176.       x2 = x1;
  177.       y2 = y1;
  178.     };
  179.     x1 = x3;
  180.     y1 = y3;
  181.   };
  182.   if (x1 < x3) {
  183.     *UNTERE_GRENZE = x1;
  184.     *OBERE_GRENZE  = x2;
  185.   }
  186.   else {
  187.     *UNTERE_GRENZE = x2;
  188.     *OBERE_GRENZE  = x1;
  189.   };
  190.   return (x3);
  191. }
  192. /* ------------------------------------------------- */
  193. /*                Ende von NLIN.C                    */
  194.  
  195.