Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.83 KB | None | 0 0
  1. //code fo bisection
  2.  
  3. //bisection method
  4. #include<iostream>
  5. #include<cmath>
  6. #include<iomanip>
  7. using namespace std;
  8. double f(double x);    //declare the function for the given equation
  9. double f(double x)    //define the function here, ie give the equation
  10. {
  11.     double a=pow(x,3)-(6*pow(x,2))+(11.0*x)-6;    //write the equation whose roots are to be determined
  12.     return a;
  13. }
  14. int main()
  15. {    
  16.     cout.precision(4);        //set the precision
  17.     cout.setf(ios::fixed);
  18.     double a,b,c,e,fa,fb,fc;    //declare some needed variables
  19.     a:cout<<"Enter the initial guesses:\na=";    //Enter the value of a(set a label('a:') for later use with goto)
  20.     cin>>a;
  21.     cout<<"\nb=";            //Enter the value of b
  22.     cin>>b;
  23.     cout<<"\nEnter the degree of accuracy desired, eg E=0.000002"<<endl;    //Enter the accuracy
  24.     cin>>e;                //e stands for  accuracy
  25.     if (f(a)*f(b)>0)        //Check if a root exists between a and b
  26.     {                //If f(a)*f(b)>0 then the root does not exist between a and b
  27.         cout<<"No root exist, enter another guess please"<<endl;
  28.         goto a;            //go back to 'a' ie 17 and ask for different values of a and b
  29.     }
  30.     else                //else a root exists between a and b
  31.     {
  32.    
  33.     cout<<"There is a root, I am calculating now: hold on " << endl;
  34.     while (fabs(a-b)>=e)        /*if the mod of a-b is greater than the accuracy desired keep                         bisecting the interval*/
  35.     {
  36.         c=(a+b)/2.0;        //bisect the interval and find the value of c
  37.         fa=f(a);        
  38.         fb=f(b);
  39.         fc=f(c);
  40.         cout<<"a="<<a<<"     "<<"b="<<b<<"     "<<"c="<<c<<"      fc="<<fc<<endl;/*print the                             values of a,b,c and fc  after each iteration*/        
  41.         if (fc==0)        //if f(c)=0, that means we have found the root of the equation
  42.         {
  43.             cout<<"The root of the equation is "<<c;    /*print the root of the equation                                         and break out of the loop*/
  44.             break;
  45.         }
  46.  
  47.         if (fa*fc>0)    //if f(a)xf(c)>0, that means no root exist between a and c
  48.         {
  49.             a=c;    /*hence make a=c, ie make c the starting point of the interval and b the                     end point*/
  50.         }
  51.         else if (fa*fc<0)
  52.         {    
  53.             b=c;    /*this means that a root exist between a and c therfore make c the end                     point of the interval*/
  54.         }    
  55.        
  56.    
  57.     }
  58.     }            //The loop ends when the difference between a and b becomes less than the desired accuracy ie now the value stored in 'c' can be called the approximate root of the equation        
  59.     cout<<"The root of the equation is "<<c;    //print the root    
  60.     return 0;    
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. //##################################################################################################################
  73. // this program for solving linear equations
  74. // C++ program to demostrate working of Guassian Elimination
  75. // method
  76. #include<bits/stdc++.h>
  77. using namespace std;
  78. #include <iomanip>
  79.  
  80. #define N 3  // Number of unknowns
  81.  
  82. // function to reduce matrix to r.e.f. Returns a value to
  83. // indicate whether matrix is singular or not
  84. int forwardElim(double mat[N][N+1]);
  85.  
  86. // function to calculate the values of the unknowns
  87. void backSub(double mat[N][N+1]);
  88.  
  89. // function to get matrix content
  90. void gaussianElimination(double mat[N][N+1])
  91. {
  92.     /* reduction into r.e.f. */
  93.     int singular_flag = forwardElim(mat);
  94.  
  95.     /* if matrix is singular */
  96.     if (singular_flag != -1)
  97.     {
  98.         printf("Singular Matrix.\n");
  99.  
  100.         /* if the RHS of equation corresponding to
  101.         zero row is 0, * system has infinitely
  102.         many solutions, else inconsistent*/
  103.         if (mat[singular_flag][N])
  104.             printf("Inconsistent System.");
  105.         else
  106.             printf("May have infinitely many "
  107.                 "solutions.");
  108.  
  109.         return;
  110.     }
  111.  
  112.     /* get solution to system and print it using
  113.     backward substitution */
  114.     backSub(mat);
  115. }
  116.  
  117. // function for elemntary operation of swapping two rows
  118. void swap_row(double mat[N][N+1], int i, int j)
  119. {
  120.     //printf("Swapped rows %d and %d\n", i, j);
  121.  
  122.     for (int k=0; k<=N; k++)
  123.     {
  124.         double temp = mat[i][k];
  125.         mat[i][k] = mat[j][k];
  126.         mat[j][k] = temp;
  127.     }
  128. }
  129.  
  130. // function to print matrix content at any stage
  131. void print(double mat[N][N+1])
  132. {
  133.     for (int i=0; i<N; i++, printf("\n"))
  134.         for (int j=0; j<=N; j++)
  135.             printf("%lf ", mat[i][j]);
  136.  
  137.     printf("\n");
  138. }
  139.  
  140. // function to reduce matrix to r.e.f.
  141. int forwardElim(double mat[N][N+1])
  142. {
  143.     for (int k=0; k<N; k++)
  144.     {
  145.         // Initialize maximum value and index for pivot
  146.         int i_max = k;
  147.         int v_max = mat[i_max][k];
  148.  
  149.         /* find greater amplitude for pivot if any */
  150.         for (int i = k+1; i < N; i++)
  151.             if (abs(mat[i][k]) > v_max)
  152.                 v_max = mat[i][k], i_max = i;
  153.  
  154.         /* if a prinicipal diagonal element is zero,
  155.         * it denotes that matrix is singular, and
  156.         * will lead to a division-by-zero later. */
  157.         if (!mat[k][i_max])
  158.             return k; // Matrix is singular
  159.  
  160.         /* Swap the greatest value row with current row */
  161.         if (i_max != k)
  162.             swap_row(mat, k, i_max);
  163.  
  164.  
  165.         for (int i=k+1; i<N; i++)
  166.         {
  167.             /* factor f to set current row kth elemnt to 0,
  168.             * and subsequently remaining kth column to 0 */
  169.             double f = mat[i][k]/mat[k][k];
  170.  
  171.             /* subtract fth multiple of corresponding kth
  172.             row element*/
  173.             for (int j=k+1; j<=N; j++)
  174.                 mat[i][j] -= mat[k][j]*f;
  175.  
  176.             /* filling lower triangular matrix with zeros*/
  177.             mat[i][k] = 0;
  178.         }
  179.  
  180.         //print(mat);    //for matrix state
  181.     }
  182.     //print(mat);        //for matrix state
  183.     return -1;
  184. }
  185.  
  186. // function to calculate the values of the unknowns
  187. void backSub(double mat[N][N+1])
  188. {
  189.     double x[N]; // An array to store solution
  190.  
  191.     /* Start calculating from last equation up to the
  192.     first */
  193.     for (int i = N-1; i >= 0; i--)
  194.     {
  195.         /* start with the RHS of the equation */
  196.         x[i] = mat[i][N];
  197.  
  198.         /* Initialize j to i+1 since matrix is upper
  199.         triangular*/
  200.         for (int j=i+1; j<N; j++)
  201.         {
  202.             /* subtract all the lhs values
  203.             * except the coefficient of the variable
  204.             * whose value is being calculated */
  205.             x[i] -= mat[i][j]*x[j];
  206.         }
  207.  
  208.         /* divide the RHS by the coefficient of the
  209.         unknown being calculated */
  210.         x[i] = x[i]/mat[i][i];
  211.     }
  212.  
  213.     printf("\nSolution for the system:\n");
  214.     for (int i=0; i<N; i++)
  215.         printf("%lf\n", x[i]);
  216. }
  217.  
  218. // Driver program
  219. int main()
  220. {
  221.    
  222.     //don't touch it, it works   check this link: https://pics.me.me/most-of-our-code-the-parts-copied-from-stack-overflow-38269718.png
  223.     /* input matrix */
  224.    
  225.     cout<<fixed << setprecision(2)<<showpoint ;
  226.     double mat[N][N+1] = {{8.0, 2.0,-4.0, 84.0},
  227.                         {2.0, 3.0, 3.0, 15.0},
  228.                         {5.0, -3,7.0, 14.0}
  229.                         };
  230.  
  231.     gaussianElimination(mat);
  232.  
  233.     return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement