Advertisement
STANAANDREY

mac ex6 c

Feb 24th, 2023
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.38 KB | None | 0 0
  1. f=@(x)(x-cos(x));
  2. fprintf("root=%.6f\n", findroot(f, -1000, 1000))
  3.  
  4. function root = findroot(f, a, b)
  5.     if f(a) * f(b) > 0
  6.         root = NaN;
  7.         return
  8.     end
  9.  
  10.     TOL = 1E-6;
  11.     while (b - a) / 2 > TOL
  12.         mid = (a + b) / 2;
  13.  
  14.         if f(a) * f(mid) <= 0
  15.             b = mid;
  16.         else
  17.             a = mid;
  18.         end
  19.  
  20.         root = mid;
  21.     end
  22. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement