In: Advanced Math
Determine the positive real root of ln(x^2)=0.8 by the following methods. (Note that you need to show the details of your derivations in MATLAB).
a) Graphically ( plot the function and copy your figure to word).
b) Using two iterations of the bisection method with initial guesses of xl=0.4 and xu=2 and populate the following table. What is the root after two iterations? Provide justification for the values you have obtained in your MATLAB code as comments.
i xl xu xr
1 0.4 2
2
c) Using two iterations of the false position method, with the same initial guesses as in b) and populate the table below. What is the root after two iterations? Provide justification for the values you have obtained in your MATLAB code as comments.
i xl xu xr
1 0.4 2
2
d) Compute the actual root of the function (use a built in MATLAB function) and identify which method (bisection or false position) achieves a better estimate of the root after two iterations. Prove your answer by calculating the True Percentage Error ( Assume that your answer in part d, obtained using the MATLAB Built in Function is the true value ). ALL Calculations must be performed in MATLAB.
We have developed a MATLAB code (single code for all problem) for all the problems.
--------------------MATLAB code--------------------
f=@(x) log(x.^2)-0.8; % defining function;
% answer of (a)
x=0.4:0.001:2; % discretized value of x in [0.4,2];
y=f(x); % computing function values.
plot(x,y,'LineWidth',1.25);
set(gca,'XAxisLocation','origin');
set(gca,'YAxisLocation','origin');
% answer of (b): Bisection method.
n=5; % number of iterations;
xl=0.4; % lower value of the initial interval;
xu=2; % lower value of the initial interval;
xr=(xl+xu)/2; % new root % mid-point of the initial interval;
A_bisec(1,:)=[1 xl xu xr f(xr)]; % storing the the values;
it=1; % iteration number;
while it<n
it=it+1; % updating iteration number;
if f(xl)*f(xr)<0 % updating interval
xu=xr;
elseif f(xr)*f(xu)<0
xl=xr;
end
xr=(xl+xu)/2; % new root
A_bisec(it,:)=[it xl xu xr f(xr)];
end
VarNames = {'Iteration','xl','xu','xr','f_xr'}; % creating table to
display all datails
TT=table(A_bisec(:,1),A_bisec(:,2),A_bisec(:,3),A_bisec(:,4),A_bisec(:,5),
'VariableNames',VarNames);
disp(TT);
xr_bisec=xr;
fprintf('\nRoot using Bisection method after %d iterations is:
%g\n\n',n,xr_bisec);
% answer of (c): false position method;
n=5;
xl=0.4;
xu=2;
xr=(xl*f(xu)-xu*f(xl))/(f(xu)-f(xl)); % new root
A_false(1,:)=[1 xl xu xr f(xr)]; % storing the the values;
it=1; % iteration number;
while it<n
it=it+1; % updating iteration number;
if f(xl)*f(xr)<0 % updating interval
xu=xr;
elseif f(xr)*f(xu)<0
xl=xr;
end
xr=(xl*f(xu)-xu*f(xl))/(f(xu)-f(xl)); % new root
A_false(it,:)=[it xl xu xr f(xr)];
end
VarNames = {'Iteration','xl','xu','xr','f_xr'}; % creating table to
display all datails
TT1=table(A_false(:,1),A_false(:,2),A_false(:,3),A_false(:,4),A_false(:,5),
'VariableNames',VarNames);
disp(TT1);
xr_false=xr;
fprintf('\nRoot using False Position method after %d iterations is:
%g\n\n',n,xr_false);
% answer of (d): MATLAB inbuilt function
xr = fzero(f,2);
fprintf('Root using MATLAB function is: %g\n\n',xr);
fprintf('Percentage error for Bisection method after %d iterations
is=%g %%\n\n',n,100*abs(xr-xr_bisec)/xr);
fprintf('Percentage error for False Position method after %d
iterations is=%g %%\n\n',n,100*abs(xr-xr_false)/xr);
Output for 5 iterations: (You can obtained the desired number of iterations by changing the value of n in the code)