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

  1. /* ------------------------------------------------- */
  2. /*                       NLINTEST.C                  */
  3. /*      (c) 1992 Klaus Röbenack & DMV-Verlag         */
  4. /*       Lösung nichtlinearer Gleichungen (Demo)     */
  5. /* ------------------------------------------------- */
  6.  
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <math.h>
  10. #include "nlin.c"
  11.  
  12. /* ------------------------------------------------- */
  13. /* Beispiel für eine Funktion                        */
  14.  
  15. float funktion (float x)
  16. {
  17.   return (x-exp(x/10)-5);
  18. }
  19.  
  20. /* ------------------------------------------------- */
  21. /* Ableitung der Funktion                            */
  22.  
  23. float ableitung (float x)
  24. {
  25.   return (1-exp(x/10)/10);
  26. }
  27.  
  28. /* ------------------------------------------------- */
  29. /* Für direkte Iteration                             */
  30.  
  31. float funktion2 (float x)
  32. {
  33.   return (exp(x/10)+5);
  34. }
  35.  
  36. /* ------------------------------------------------- */
  37. /*  Hilfsfunktionen für Ausgabe                      */
  38.  
  39. void output (float xn)
  40. {
  41.   gotoxy (3,8);
  42.   cprintf ("X : %25.15f",xn);
  43.   gotoxy (3,9);
  44.   cprintf ("Y : %25.15f",funktion(xn));
  45.   getchar();
  46. }
  47.  
  48. void fenster1(str1, str2, str3, xu, xo)
  49.   char  *str1, *str2, *str3;
  50.   float *xu, *xo;
  51. {
  52.   window(35,12,78,22); textcolor(BLACK);
  53.   textbackground(CYAN); clrscr();
  54.   gotoxy(3,2); cputs(str1);
  55.   gotoxy(3,4); cputs(str2); cscanf("%f",xu);
  56.   if (*str3!=0) {
  57.     gotoxy(3,5); cputs(str3); cscanf("%f",xo);
  58.   };
  59. }
  60.  
  61. void fenster2(str1, xu, xo)
  62.   char  *str1;
  63.   float *xu, *xo;
  64. {
  65.   fenster1(str1,"Schranke 1 : ","Schranke 2 : ",xu,xo);
  66. }
  67.  
  68. /* ------------------------------------------------- */
  69.  
  70. void main(void)
  71. {
  72.   char  c;
  73.   float x1, x2, xn;
  74.  
  75.   do {
  76.    window(1,1,80,25); textbackground(RED); clrscr();
  77.    window(5,5,65,20); textcolor(YELLOW);
  78.    textbackground(BLUE); clrscr();
  79.    gotoxy(5,2);
  80.    cputs ("*** LÖSUNG NICHTLINEARE GLEICHUNGEN ***");
  81.    gotoxy(5,3);
  82.    cputs
  83.    ("    Implementierte Funktion: f(x)=x-exp(x/10)-5");
  84.    gotoxy(9,5); cputs ("I   ... Iterations-Verfahren");
  85.    gotoxy(9,6); cputs ("N   ... Newton-Verfahren");
  86.    gotoxy(9,7); cputs ("R   ... Regula-Falsi");
  87.    gotoxy(9,8);
  88.    cputs ("H   ... Halbierungs-Verfahren");
  89.    gotoxy(9,9); cputs ("S   ... Sehnen-Verfahren");
  90.    gotoxy(9,10);cputs ("P   ... Pegasus-Verfahren");
  91.    gotoxy(9,12);cputs ("ESC ... Ende");
  92.    textcolor(YELLOW);
  93.    gotoxy(9,14);cputs ("Bitte wählen Sie !");
  94.    textcolor(WHITE);
  95.    c=getch();
  96.    switch(c) {
  97.      case 'I':
  98.      case 'i': {
  99.        fenster1("*** Direkte Iteration ***",
  100.                 "Startwert : ","",&x1,&x2);
  101.        xn=IterationsVerfahren(funktion2,x1);
  102.        output(xn);
  103.        break;
  104.      };
  105.      case 'N':
  106.      case 'n': {
  107.        fenster1("*** Newton ***","Startwert : ","",
  108.                 &x1,&x2);
  109.        xn=NewtonVerfahren(funktion,ableitung,x1);
  110.        output(xn);
  111.        break;
  112.      };
  113.      case 'R':
  114.      case 'r': {
  115.       fenster1("*** Regula-Falsi ***","Startwert 1 : ",
  116.                 "Startwert 2 : ",&x1,&x2);
  117.       xn=SekandenVerfahren(funktion,x1,x2);
  118.       output(xn);
  119.       break;
  120.      };
  121.      case 'H':
  122.      case 'h': {
  123.        do {
  124.          fenster2("*** Halbierung des Intervalls ***",
  125.                   &x1,&x2);
  126.        } while(funktion(x1)*funktion(x2) >= 0);
  127.        xn=HalbierungsVerfahren(funktion,&x1,&x2);
  128.        output(xn);
  129.        break;
  130.      };
  131.      case 'S':
  132.      case 's': {
  133.        do {
  134.          fenster2("*** Sehnen-Verfahren ***",&x1,&x2);
  135.        } while(funktion(x1)*funktion(x2) >= 0);
  136.        xn=SehnenVerfahren(funktion,&x1,&x2);
  137.        output(xn);
  138.        break;
  139.      };
  140.      case 'P':
  141.      case 'p': {
  142.        do {
  143.          fenster2("*** Pegasus-Algorithmus ***",
  144.                   &x1,&x2);
  145.        } while(funktion(x1)*funktion(x2) >= 0);
  146.        xn=PegasusVerfahren(funktion,&x1,&x2);
  147.        output(xn);
  148.        break;
  149.      };
  150.     };
  151.   } while(c!=27);
  152.   window(1,1,80,25);
  153.   clrscr();
  154. }
  155. /* ------------------------------------------------- */
  156. /*              Ende von NLINTEST.C                  */
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.