Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- f=@(x)(x-cos(x));
- fprintf("root=%.6f\n", findroot(f, -1000, 1000))
- function root = findroot(f, a, b)
- if f(a) * f(b) > 0
- root = NaN;
- return
- end
- TOL = 1E-6;
- while (b - a) / 2 > TOL
- mid = (a + b) / 2;
- if f(a) * f(mid) <= 0
- b = mid;
- else
- a = mid;
- end
- root = mid;
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement