CS计算机代考程序代写 f=@(x) exp(-x)-x;

f=@(x) exp(-x)-x;

fplot(f,[0,5])
hold on
plot([0,5],[0,0])
hold off

%% Root occurs between 0 and 1
x_l = 0; %Initial lower guess of root
x_u = 1; %Initial upper guess of root

eps = 1.0e-6; %Accuracy of your answer
maxiter = 50; %maximum number of iterations

x_r=(x_l+x_u)/2

while abs(f(x_r))>eps
f_l=f(x_l);
f_u=f(x_u);
f_r=f(x_r);
if f_r*f_l < 0 x_u= x_r; elseif f_r*f_u < 0 x_l= x_r; end x_r=(x_l+x_u)/2 end abs(x_r-fzero(f,1))