PLEASE ANSWER USING MATLAB ONLY AND INLCUDE SCREENSHOTS OF COMMAND/EDITOR WINDOW
ID: 3597837 • Letter: P
Question
PLEASE ANSWER USING MATLAB ONLY AND INLCUDE SCREENSHOTS OF COMMAND/EDITOR WINDOW!
3.16 Write a MATLAB user-defined function that solves for a root of a nonlinear equation f(x) 0 using the bisection method. Name the function xs = Bi sectio nRoot(Fun, a, b). The output argument Xs is the solution. The input argument Fun is a name for the function that calculates f(x) for a given x (it is a dummy name for the function that is imported into BisectionRoot); a and b are two points that bracket the root. The iterations should stop when the tolerance in f(x) (Eq. (3.5) is smaller than 0.000001. The program should check if points a and b are on opposite sides of the solution. If not, the program should stop and display an error message. Use BisectionRoot to solve Problem 3.2 Tolerance!nf = Equation 3.5 -> 3.2 Determine the root of f(x) - x-2e* by: (a) Using the bisection method. Start with a -0 and b-1, and carry out the first three iterations. (b) Using the secant method. Start with the two points, x-0 and x2-1, and carry out the first three iter ations. (c) Using Newton's method. Start at x,1 and carry out the first three iterations.Explanation / Answer
syms f x
eps =0.00001;
% Case a:
f=exp(x)-2;
[x_root_bisec,x_bisec,f_bisec]=bisection(0,1,f,eps);
[x_root_secant,x_secant,f_secant]=secant(0,1,f,eps);
[x_root_newton,x_newton,f_newton]=newton(1,f,eps);
disp(['x_exact=',num2str(log(2)),' x_bisection=',num2str(x_root_bisec),..
' x_secant=',num2str(x_root_secant),' x_newton=',num2str(x_root_newton)])
figure(1)
hold off
semilogy(abs(f_bisec),'r-')
ylabel('error')
xlabel('iteration')
hold on
plot(abs(f_secant),'m-')
plot(abs(f_newton),'b-')
% Case b:
f=atan(x-1/sqrt(2));
[x_root_bisec,x_bisec,f_bisec]=bisection(0,1,f,eps);
[x_root_secant,x_secant,f_secant]=secant(0,1,f,eps);
[x_root_newton,x_newton,f_newton]=newton(1,f,eps);
disp(['x_exact=',num2str(log(2)),' x_bisection=',num2str(x_root_bisec),...
' x_secant=',num2str(x_root_secant),' x_newton=',num2str(x_root_newton)])
figure(2)
hold off
semilogy(abs(f_bisec),'r-')
ylabel('error')
xlabel('iteration')
hold on
plot(abs(f_secant),'m-')
plot(abs(f_newton),'b-')
%-------------------------------------------------------------
%Bisection
function [x_root,x_guess,f_guess]=bisection(x1,x2,fun,eps)
xh1=min([x1 x2]);
xh2=max([x1,x2]);
x_guess = [xh1 xh2];
f_guess = subs(fun,x_guess);
if (f_guess(1)*f_guess(2)) > 0
disp('wrong guess');
x_root=NaN;
return
end
if min(abs(f_guess)) < eps
[f_min,i_min] = min(abs(f_guess));
x_root=x_guess(i_min);
disp('excellent guess')
return
end
iter = 2;
check = logical(1);
while check
iter = iter +1;
x_guess(iter)=(xh1+xh2)/2;
f_guess(iter) = subs(fun,x_guess(iter));
if min(abs(f_guess)) < eps
[f_min,i_min] = min(abs(f_guess));
x_root=x_guess(i_min);
check = logical(0);
else
if f_guess(iter) < 0
xh1=x_guess(iter);
elseif f_guess(iter) > 0
xh2=x_guess(iter);
end
end
end
%----------------------------------------------------------
%Secant
function [x_root,x_guess,f_guess]=secant(x1,x2,fun,eps)
xh1=min([x1 x2]);
xh2=max([x1,x2]);
x_guess = [xh1 xh2];
f_guess = subs(fun,x_guess);
fh1=f_guess(1);
fh2=f_guess(2);
if (f_guess(1)*f_guess(2)) > 0
disp('wrong guess');
x_root=NaN;
return
end
if min(abs(f_guess)) < eps
[f_min,i_min] = min(abs(f_guess));
x_root=x_guess(i_min);
disp('excellent guess')
return
end
iter = 2;
check = logical(1);
while check
iter = iter +1;
x_guess(iter)=xh1-fh1*(xh1-xh2)/(fh1-fh2);
f_guess(iter) = subs(fun,x_guess(iter));
if min(abs(f_guess)) < eps
[f_min,i_min] = min(abs(f_guess));
x_root=x_guess(i_min);
check = logical(0);
else
if f_guess(iter) < 0
xh1=x_guess(iter);
fh1=f_guess(iter);
elseif f_guess(iter) > 0
xh2=x_guess(iter);
fh2=f_guess(iter);
end
end
end
%------------------------------------------------------
%Newton
function [x_root,x_guess,f_guess]=newton(x1,fun,eps)
x_guess(1) = [x1];
f_guess(1) = subs(fun,x_guess);
df=subs(diff(fun),x_guess(1));
if abs(f_guess) < eps
x_root=x_guess;
disp('excellent guess');
return
end
iter = 1;
check = logical(1);
while check
iter = iter +1;
x_guess(iter)=0;
f_guess(iter)=0;
x_guess(iter)=x_guess(iter-1)-f_guess(iter-1)/df;
f_guess(iter) = subs(fun,x_guess(iter));
df=subs(diff(fun),x_guess(iter));
if abs(f_guess(iter)) < eps
x_root=x_guess(iter);
check = logical(0);
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.