LAB WORK # 2
Write a MATLAB M-file for bisection method.
F=@(x) x^2-exp(-x);
a= 0;
b=1;
Fa= F(a);
Fb = F(b);
max_iterations = 20;
tolerance = 0.002;
for i= 1: max_iterations
c = (a+b)/2;
Fc =F(c);
if i>1
tol = abs((c-g)/c);
if tol < tolerance
break
end
end
g = c;
if F(a)*F(c)<0
b = c;
else
a = c;
end
end
c
Write a MATLAB M-file for Newton Method.
F=@(x) sqrt(x) + (x.^2) -7;
FD =@(x) 1/(2*sqrt(x))+(2*x);
Xi = 7;
% F = Function
% FD = Derivative of Function
% Xi =Initial Guess
Tolerance = 0.0002;
max_iterations = 20;
for i= 1: max_iterations
Xnew = Xi - F(Xi)/(FD(Xi));
if abs((Xnew-Xi)/Xi) < Tolerance
Xans = Xnew;
break
end
1
Xi = Xnew;
Xans = Xnew;
end
Xans