Pella86

Bisect and Newton

Feb 4th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <stdexcept>
  5.  
  6. using namespace std;
  7.  
  8. // This function if is 0 is the solutioon of sqrt(2) - n = 0
  9. double f(double n){
  10.     return n*n - 2;
  11.     //return n*n*n*n - n*n;
  12. }
  13.  
  14. double fprime(double n){
  15.     //return 2*n;
  16.     double h = 0.001;
  17.     return (f(n + h) - f(n))/ h;
  18. }
  19.  
  20. // function that returns the "sign" of the value -1, 0 for 0 or, 1 for positive
  21. template <typename T>
  22. int sgn(T val) {
  23.     return (T(0) < val) - (val < T(0));
  24. }
  25.  
  26. // contains the result of the function bysect, the iteration count and the
  27. // resulting number to calculate the error
  28. struct BisectResult{
  29.     int itr_count;
  30.     double result;
  31. };
  32.  
  33. // type of function pointer f(x)
  34. typedef double (*fptr_t) (double);
  35.  
  36. // calculates how many iterations to reach the given tolerance
  37. BisectResult bisect(double lower, double upper, double tolerance, fptr_t func){
  38.     int itr_max = 10000;
  39.     int itr_count = 0;
  40.     BisectResult bs_res;
  41.  
  42.     double half = (upper + lower) / 2.0;
  43.     double res;
  44.  
  45.     do{
  46.         res = func(half);
  47.         bs_res.result = half;
  48.         bs_res.itr_count = itr_count;
  49.  
  50.         if( sgn<double>(res) == sgn<double>(f(lower)) ){
  51.             lower = half;
  52.         }
  53.         else{
  54.             upper = half;
  55.         }
  56.  
  57.         half = (lower + upper) / 2;
  58.  
  59.         ++itr_count;
  60.  
  61.         if(itr_count >= itr_max){
  62.             throw runtime_error("Function bisect: Max iteration reached without convergence");
  63.         }
  64.  
  65.     } while(abs(res) > tolerance);
  66.  
  67.     return bs_res;
  68. }
  69.  
  70. // calculate the error given the expected result
  71. // 0 as expected result is a pain tho
  72. double calc_error(double expected_result, double experimental_result){
  73.     double err = (expected_result - experimental_result) / expected_result;
  74.     err = err * 100;
  75.     return err;
  76.  
  77. }
  78.  
  79. BisectResult newton(double x0, fptr_t fx, fptr_t fprime, double tolerance){
  80.     int itr_count = 0;
  81.     BisectResult bs_res;
  82.     bool cond;
  83.  
  84.     do{
  85.         double x1 = x0 - fx(x0)/fprime(x0);
  86.         cond = abs(x1 - x0) <= tolerance * abs(x1);
  87.         bs_res.itr_count = itr_count, bs_res.result = x1;
  88.         x0 = x1;
  89.         itr_count += 1;
  90.  
  91.     }while(!cond);
  92.  
  93.     return bs_res;
  94. }
  95.  
  96.  
  97.  
  98. int main()
  99. {
  100.     //------------------ user inputs --------------------------------------
  101.  
  102.     // define the search interval
  103.     double lower = 0, upper = 2.0;
  104.  
  105.     //------------------ algorithm starts --------------------------------------
  106.     BisectResult br;
  107.     for(double tolerance = 0.1; tolerance >= 0.00000001; tolerance/=10){
  108.             cout << "------------------------------" << endl;
  109.             cout << "tolerance: " << tolerance << endl;
  110.             //br = bisect(lower, upper, tolerance, f);
  111.             br = newton(upper, f, fprime, tolerance);
  112.             cout << "iterations: " << br.itr_count << " result: " << br.result << endl;
  113.             cout << "error: " << calc_error(sqrt(2), br.result) << "%" << endl;
  114.     }
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment