Make an octave code to find the root of cos(x) – x * e x = 0 by using bisection method. The answer should be corrected up to four decimal places.
f = @(x) cos(x)-x.*exp(x);
EPS = 1e-4;
a = 0;
b = 1;
fa = f(a);
fb = f(b);
while b-a > EPS
c = (b + a) / 2;
fc = f(c);
if fc == 0
break
end
if fc*fa < 0
b = c;
fb = fc;
else
a = c;
fa = fc;
end
end
fprintf("The root is %f\n", c)
Comments
Leave a comment