Advertisement
heroys6

Lab 3

Nov 6th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <conio.h>
  6.  
  7. double unlinear_func(double x)
  8. {
  9.     double f;
  10.     f = pow(x,3)-3;
  11.     //f = pow(x,3)+2*x;
  12.     //f = pow(x,3)-x+1;
  13.     return f;
  14. }
  15.  
  16. double chords(double x1, double x2)
  17. {
  18.     return (unlinear_func(x2)*x1-unlinear_func(x1)*x2)/(unlinear_func(x2)-unlinear_func(x1));
  19. }
  20.  
  21. double bisection(double x1, double x2)
  22. {
  23.     return (x1+x2)/2;
  24. }
  25.  
  26. int iter(int imax, int i, double xi, double x1, double x2, int ch, int deb, double e, clock_t start)
  27. {
  28.     int ich;
  29.     clock_t stop;
  30.  
  31.     if (i>imax)
  32.     {
  33.         printf("\nExceeded max number of i(iterations)!\n\nWhat we should do?\n");
  34.         printf("1.Continue with the same number of i\n2.Run to find a solution\n3.Exit and print current result\nYour choose: ");
  35.         scanf("%d", &ich);
  36.         switch(ich)
  37.         {
  38.         case 1:
  39.             return i-imax;
  40.             break;
  41.  
  42.         case 2:
  43.             if (ch==1)
  44.             {
  45.  
  46.                 do
  47.                 {
  48.                     xi=chords(x1, x2);
  49.  
  50.                     if (deb==1)
  51.                     {
  52.                         stop=clock();
  53.                         ccout(xi, i, stop, start);
  54.                         printf("x1=%lf    x2=%lf\n", x1, x2);
  55.                     }
  56.  
  57.                     i++;
  58.  
  59.                     if (unlinear_func(xi)*unlinear_func(x1)<0) x2=xi;
  60.                     else
  61.                     {
  62.                         if (unlinear_func(xi)*unlinear_func(x2)<0) x1=xi;
  63.                     }
  64.  
  65.                 }
  66.                 while (e<fabs(unlinear_func(xi)));
  67.                 stop=clock();
  68.  
  69.                 printf("\n(!)Final: \n");
  70.                 stop=clock();
  71.                 ccout(xi, i=0, stop, start);
  72.                 exit(0);
  73.  
  74.             }
  75.             else
  76.             {
  77.                 if (ch==2)
  78.                 {
  79.                     do
  80.                     {
  81.                         xi=bisection(x1, x2);
  82.  
  83.                         if (deb==1)
  84.                         {
  85.                             stop=clock();
  86.                             ccout(xi, i, stop, start);
  87.                             printf("x1=%lf    x2=%lf\n", x1, x2);
  88.                         }
  89.  
  90.                         i++;
  91.  
  92.                         if (unlinear_func(xi)<0) x1=xi;
  93.                         else
  94.                         {
  95.                             if (unlinear_func(xi)>0) x2=xi;
  96.                         }
  97.  
  98.                     }
  99.                     while(e<fabs(x1-x2));
  100.  
  101.                     printf("\n(!)Final: \n");
  102.                     stop=clock();
  103.                     ccout(xi, i=0, stop, start);
  104.                     exit(0);
  105.  
  106.                 }
  107.             }
  108.  
  109.             break;
  110.  
  111.         case 3:
  112.             stop=clock();
  113.             ccout(xi, i=0, stop, start);
  114.             exit(0);
  115.             break;
  116.  
  117.         default:
  118.             printf("\nBad answer!");
  119.             exit(0);
  120.             break;
  121.         }
  122.     }
  123. }
  124.  
  125. int ccout(double xi, int i, clock_t stop, clock_t start)
  126. {
  127.     printf("\n\t\t\t\t.::-Result-::.");
  128.     printf("\nRoot value(Xi) = %.8lf", xi);
  129.     printf("\nFunction value = %.8lf", unlinear_func(xi));
  130.     if (i==0) ;
  131.     else
  132.     {
  133.         printf("\nIterations value = %d", i);
  134.     }
  135.     printf("\nElapsed time: %.10f seconds\n", (stop-start)/CLK_TCK);
  136.     return 101;
  137. }
  138.  
  139. int repeat(int povt)
  140. {
  141.     char Y_n;
  142.  
  143.     printf("\nDo you want to start program again?(Y/n)");
  144.     fflush(stdin);
  145.     scanf("%c", &Y_n);
  146.  
  147.     switch (Y_n)
  148.         {
  149.             case 'Y':
  150.                 povt=1;
  151.                 printf("Ok, press something...");
  152.                 break;
  153.  
  154.             case 'y':
  155.                 povt=1;
  156.                 printf("Ok, press something...");
  157.                 break;
  158.  
  159.             case 'N':
  160.                 return 0;
  161.  
  162.  
  163.             case 'n':
  164.                 return 0;
  165.  
  166.  
  167.             default:
  168.                 printf("Wrong answer. Program will finish...\n");
  169.                 return 0;
  170.  
  171.             }
  172. return povt;
  173. }
  174.  
  175. int main()
  176. {
  177.     double x1, x2, e, xi;
  178.     int i=0, imax, ch, povt=0, prvr, deb;
  179.     clock_t start, stop;
  180.  
  181.     printf("\t\t+++_Calculating_of_nonlinear_equation_+++\n\n");
  182.  
  183.     do
  184.     {
  185.         if (povt==1)
  186.         {
  187.             getch();
  188.             system("cls");
  189.         }
  190.  
  191.         povt=0;
  192.  
  193.         do
  194.         {
  195.             prvr=0;
  196.             printf("Enter x1: ");
  197.             fflush(stdin);
  198.             scanf("%lf", &x1);
  199.  
  200.             printf("Enter x2: ");
  201.             fflush(stdin);
  202.             scanf("%lf", &x2);
  203.  
  204.             if (unlinear_func(x1)*unlinear_func(x2)>0)
  205.             {
  206.                 printf("Wrong x1 or x2. Try again!\n");
  207.                 prvr=1;
  208.             }
  209.         }
  210.         while(prvr>0);
  211.  
  212.         do
  213.         {
  214.             prvr=0;
  215.             printf("Enter allowable error(E): ");
  216.             fflush(stdin);
  217.             scanf("%lf", &e);
  218.             if (e<pow(10,-5) || e>pow(10,-3))
  219.             {
  220.                 printf("E must be from 10^-5 to 10^-3 Try again!\n");
  221.                 prvr=1;
  222.             }
  223.         }
  224.         while(prvr>0);
  225.  
  226.         do
  227.         {
  228.             prvr=0;
  229.             printf("Enter max number of iterations(i): ");
  230.             fflush(stdin);
  231.             scanf("%d", &imax);
  232.             if (imax<=0)
  233.             {
  234.                 printf("Max 'i' must be more then 0! Try again\n");
  235.                 prvr=1;
  236.             }
  237.         }
  238.         while(prvr>0);
  239.  
  240.         printf("\nSelect method:\n\n1.Chords\n2.Bisection\nYour choose will be: ");
  241.         fflush(stdin);
  242.         scanf("%d", &ch);
  243.  
  244.         switch(ch)
  245.         {
  246.         case 1:
  247.             printf("\n\t\t\tSelected method of chords\n\n");
  248.  
  249.             printf("Activate debugging mod?(1-yes/2-no): ");
  250.             scanf("%d", &deb);
  251.  
  252.             if (deb==1) printf("\n\t\t\t\tDebugging mod status: [ON]\n\n");
  253.             else printf("\n\t\t\t\tDebugging mod status: [OFF]\n\n");
  254.  
  255.             start=clock();
  256.             do
  257.             {
  258.             xi=chords(x1, x2);
  259.             i++;
  260.             i=iter(imax, i, xi, x1, x2, ch, deb, e, start);
  261.  
  262.             if (deb==1)
  263.             {
  264.                 ccout(xi, i, stop, start);
  265.                 printf("x1=%lf    x2=%lf\n", x1, x2);
  266.             }
  267.  
  268.             if (unlinear_func(xi)*unlinear_func(x1)<0) x2=xi;
  269.             else
  270.             {
  271.                 if (unlinear_func(xi)*unlinear_func(x2)<0) x1=xi;
  272.             }
  273.  
  274.             }
  275.             while (e<fabs(unlinear_func(xi))); // f(xi)<=E --> End of cycle
  276.             stop=clock();
  277.  
  278.             ccout(xi, i, stop, start);
  279.  
  280.             break;
  281.  
  282.         case 2:
  283.             printf("\n\t\t\tSelected method of bisection\n\n");
  284.  
  285.             printf("Activate debugging mod?(1-yes/2-no): ");
  286.             scanf("%d", &deb);
  287.  
  288.             if (deb==1) printf("\n\t\t\t\tDebugging mod status: [ON]\n\n");
  289.             else printf("\n\t\t\t\tDebugging mod status: [OFF]\n\n");
  290.  
  291.             start=clock();
  292.             do
  293.             {
  294.             xi=bisection(x1, x2);
  295.             i++;
  296.             i=iter(imax, i, xi, x1, x2, ch, deb, e, start);
  297.  
  298.             if (deb==1)
  299.             {
  300.                 ccout(xi, i, stop, start);
  301.                 printf("x1=%lf    x2=%lf\n", x1, x2);
  302.             }
  303.  
  304.             if (unlinear_func(xi)<0) x1=xi;
  305.             else
  306.             {
  307.                 if (unlinear_func(xi)>0) x2=xi;
  308.             }
  309.  
  310.             }
  311.             while(e<fabs(x1-x2));
  312.             stop=clock();
  313.  
  314.             ccout(xi, i, stop, start);
  315.  
  316.             break;
  317.  
  318.         default:
  319.             printf("\nBad answer! Try again!");
  320.             break;
  321.         }
  322.  
  323. povt=repeat(povt);
  324.     }
  325.     while(povt==1);
  326.  
  327.     return 0;
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement