home *** CD-ROM | disk | FTP | other *** search
- /* ------------------------------------------------- */
- /* NLIN.C */
- /* (c) 1992 Klaus Röbenack & DMV-Verlag */
- /* Lösung nichtlinearer Gleichungen */
- /* ------------------------------------------------- */
-
- #ifndef _MATH_H /* ggf. Bibliothek einbinden */
- #include <math.h>
- #endif
-
- #define EPSILON 0.00001 /* Abbruch-Schranke */
- #define MAX_ANZ 200 /* max. Durchläufe */
-
- /* ------------------------------------------------- */
- /* Iterations-Verfahren für F(x)=x */
- /* ------------------------------------------------- */
-
- float IterationsVerfahren(FUNKTION, STARTWERT)
- float (*FUNKTION)(float); /* num. Teilfunktion */
- float STARTWERT; /* Anfangswert für Arg. */
- {
- int i;
- float x, y;
-
- x=STARTWERT;
- for (i=1; i < MAX_ANZ; i++)
- {
- y = FUNKTION(x);
- if (fabs(x-y) < EPSILON) break;
- x = y;
- };
- return(x);
- }
-
- /* ------------------------------------------------- */
- /* Verfahren nach Newton / Raphson */
- /* ------------------------------------------------- */
-
- float NewtonVerfahren (FUNKTION, ABLEITUNG, STARTWERT)
- float (*FUNKTION)(float); /* numerische Funktion */
- float (*ABLEITUNG)(float); /* erste Ableitung */
- float STARTWERT; /* Anfangswert des Arg. */
- {
- int i;
- float x0, x1, y;
-
- x0 = STARTWERT;
- for (i=1; i < MAX_ANZ; i++)
- {
- y = FUNKTION(x0);
- x1 = x0 - y / ABLEITUNG(x0);
- if (fabs(y) < EPSILON) break;
- x0 = x1;
- };
- return(x1);
- }
-
- /* ------------------------------------------------- */
- /* Regula - Falsi */
- /* ------------------------------------------------- */
-
- float SekandenVerfahren (FUNKTION, STARTWERT1,
- STARTWERT2)
- float (*FUNKTION)(float); /* numerische Funktion */
- float STARTWERT1,STARTWERT2; /* zwei versch. Werte */
- {
- int i;
- float x0, x1, x2, y0, y1;
-
- x0 = STARTWERT1;
- x1 = STARTWERT2;
- y0 = FUNKTION(x0);
- for (i=1; i < MAX_ANZ; i++)
- {
- y1 = FUNKTION(x1);
- if (fabs(y1)<EPSILON) break;
- x2 = x1 - y1 * (x1 - x0) / (y1 - y0);
- x0 = x1;
- x1 = x2;
- y0 = y1;
- };
- return(x2);
- }
-
- /* ------------------------------------------------- */
- /* Intervall - Halbierung */
- /* ------------------------------------------------- */
-
- float HalbierungsVerfahren (FUNKTION, UNTERE_GRENZE,
- OBERE_GRENZE)
- float (*FUNKTION)(float); /* numerische Funktion */
- float *UNTERE_GRENZE; /* Arg. mit negativem Wert */
- float *OBERE_GRENZE; /* Arg. mit positivem Wert */
- {
- int i;
- float x1, x2, x3, y1, y3;
-
- x1 = *UNTERE_GRENZE;
- x2 = *OBERE_GRENZE;
- y1 = FUNKTION(x1);
- for (i=1; i < MAX_ANZ; i++)
- {
- x3 = (x1+x2) / 2;
- y3 = FUNKTION(x3);
- if (y1*y3<0)
- x2 = x3;
- else {
- x1 = x3;
- y1 = y3;
- };
- if (fabs(y3) < EPSILON) break;
- };
- *UNTERE_GRENZE = x1;
- *OBERE_GRENZE = x2;
- return(x3);
- }
-
- /* ------------------------------------------------- */
- /* Einschränkende Regula - Falsi */
- /* ------------------------------------------------- */
-
- float SehnenVerfahren (FUNKTION, UNTERE_GRENZE,
- OBERE_GRENZE)
- float (*FUNKTION)(float);
- float *UNTERE_GRENZE, *OBERE_GRENZE;
- {
- int i;
- float x1, x2, x3, y1, y2, y3;
-
- x1 = *UNTERE_GRENZE;
- y1 = FUNKTION(x1);
- x2 = *OBERE_GRENZE;
- y2 = FUNKTION(x2);
- for (i=1; i < MAX_ANZ; i++)
- {
- x3 = x1 - y1 * (x2 - x1) / (y2 - y1);
- y3 = FUNKTION(x3);
- if (y1*y3 < 0) {
- x1 = x3;
- y1 = y3; }
- else {
- x2 = x3;
- y2 = y3;
- };
- if (fabs(y3) < EPSILON) break;
- };
- *UNTERE_GRENZE = x1;
- *OBERE_GRENZE = x2;
- return(x3);
- }
-
- /* ------------------------------------------------- */
- /* Pegasus - Algorithmus */
- /* ------------------------------------------------- */
-
- float PegasusVerfahren(FUNKTION, UNTERE_GRENZE,
- OBERE_GRENZE)
- float (*FUNKTION)(float);
- float *UNTERE_GRENZE, *OBERE_GRENZE;
- {
- int i;
- float x1, x2, x3, y1, y2, y3;
-
- x1 = *UNTERE_GRENZE;
- y1 = FUNKTION(x1);
- x2 = *OBERE_GRENZE;
- y2 = FUNKTION(x2);
- for (i=1; i < MAX_ANZ; i++)
- {
- x3 = x1 - y1 * (x2-x1) / (y2-y1);
- y3 = FUNKTION(x3);
- if (fabs(y3) < EPSILON) break;
- if (y1*y3 < 0)
- y2 = y1 * y2 / (y1 + y3);
- else {
- x2 = x1;
- y2 = y1;
- };
- x1 = x3;
- y1 = y3;
- };
- if (x1 < x3) {
- *UNTERE_GRENZE = x1;
- *OBERE_GRENZE = x2;
- }
- else {
- *UNTERE_GRENZE = x2;
- *OBERE_GRENZE = x1;
- };
- return (x3);
- }
- /* ------------------------------------------------- */
- /* Ende von NLIN.C */
-
-