# Bisection Method, Newtons method, fixed point, and Globally Convergent Newton's method

12 views (last 30 days)
Sarah Smith on 26 Jan 2020
Commented: Sarah Smith on 26 Jan 2020
I want to adjust the functions I created for the four methods I used so that I save the errors for all the iterates into a vector. So, basically I want to draw the four error vectors for the four algorithms I used, and put them all in one graph. But, my graphs do not seem to look elegant. I will show you the code I have so far. Can someone help me put all these four algorithms in one graph ? In % problem #6, where I am trying to put all four algorithms in one gragh, this is where I am stuck.
% problem #6.
clc;
a = -8;
b = 8;
maxIter = 1e6; %max iteration of the function
epsilon = 10e-12; %tolerance value
f1 = @(x) exp(2*sin(x))-x; %function
gx = @(x) asin(log(sqrt(x))); %alternative g(x) for problem 3
x0 = 2; %initial guess
% Calling the Interval Bisection Method Function
[xk2,i2,error2] = Intbisections(f1,a,b,epsilon);
% Calling the Fixed Point Method Function
[xk3,i3,error3] = FixPoint(x0,maxIter,gx,epsilon);
% Calling the Newton's Method function Problem 4
[xk4,i4,error4] = NewtonsMethod(x0,maxIter,f1,epsilon);
% Calling Globally Convergent Newtons's Method Function
[xk5,i5,error5] = NewtonsMethodGlobal(x0,a,b,f1,epsilon);
%saving errors as vectors
errorVector = [error2,error3,error4,error5];
%Graphing the error vector
x_axis = [1,2,3,4]; %x-axis values
semilogy(x_axis,errorVector,'-mo'); %graph of error vector in logarithmic y-axis
grid on %logarithmic grid (y-axis only
%problem 4 Interval Bisection Method Function
function [xk,i,error] = Intbisections(f,a,b,epsilon)
i = 1;
if f(a)*f(b)>0
disp('Wrong choice of interval')
else
xk = (a + b)/2;
error = abs(f(xk));
while error > epsilon
if f(a)*f(xk)>0
a = xk;
else
b = xk;
end
xk = (a + b)/2;
error = abs(f(xk));
i = i + 1;
end
end
end
% Problem 3: Fixed Point Method Function
function [xk,i,error]=FixPoint(xk,maxIter,f1,epsilon)
xold = xk;
for i = 1:maxIter
xk = f1(xk);
error = abs(xk-xold);
xold = xk;
if (error<epsilon)
break;
end
end
end
% Problem 4: Function for Newton's Method function
function [xk,i,error] = NewtonsMethod(xk,maxIter,f1,epsilon)
xold = xk;
for i = 1:maxIter
[f1x,df1x]=valuedif(f1,xk);
xk = xk - (f1x/df1x);
error = abs(xk-xold);
xold = xk;
if (error<epsilon)
break;
end
end
end
% Problem 5: Globally Convergent Newtons's Method Function
function[xk,i,error] = NewtonsMethodGlobal(xk,a,b,f1,epsilon)
i = 0;
if f1(a)*f1(b)>0
disp('Wrong choice of interval')
else
error = abs(f1(xk));
while double(error) > epsilon
[f1x,df1x]=valuedif(f1,xk);
xk = xk - (f1x/df1x);
if (xk > a) && (xk < b)
[f1x,df1x]=valuedif(f1,xk);
xk = xk - (f1x/df1x);
else
xk = (a+b)/2;
end
if (f1(a)*f1(xk)) > 0
a = xk;
else
b = xk;
end
xk = (a + b)/2;
error = abs(f1(xk));
i = i + 1;
end
end
end
% Problem 1: Function for calling the value and the derivative of the function
function [xvalue,difx] = valuedif(f,xv)
xvalue = f(xv);
syms x
if nargout > 1
g(x) = diff(f(x));
difx = double(g(xv));
end
end

#### 1 Comment

Walter Roberson on 26 Jan 2020
Your code is not returning error vectors: it is returning error scalars.

From your text I belive you actually want to plot the convergence of the methods. For this you need to save the errors for all iterations and then plot each error vector (which don't necessarily have the same size), in your case you're returning just the last error. The following modifications in your code make the plot you want:
% problem #6.
clc;
a = -8;
b = 8;
maxIter = 1e6; %max iteration of the function
epsilon = 10e-12; %tolerance value
f1 = @(x) exp(2*sin(x))-x; %function
gx = @(x) asin(log(sqrt(x))); %alternative g(x) for problem 3
x0 = 2; %initial guess
% Calling the Interval Bisection Method Function
[xk2,i2,error2] = Intbisections(f1,a,b,epsilon);
% Calling the Fixed Point Method Function
[xk3,i3,error3] = FixPoint(x0,maxIter,gx,epsilon);
% Calling the Newton's Method function Problem 4
[xk4,i4,error4] = NewtonsMethod(x0,maxIter,f1,epsilon);
% Calling Globally Convergent Newtons's Method Function
[xk5,i5,error5] = NewtonsMethodGlobal(x0,a,b,f1,epsilon);
%saving errors as vectors
errorVector = {error2,error3,error4,error5};
%Graphing the error vector
figure
for idx=1:length(errorVector)
semilogy(errorVector{idx},'-o'); %graph of error vector in logarithmic y-axis
hold all
end
legend({'Bisection','Fix Point','Newton','Newton Global'})
grid on %logarithmic grid (y-axis only
%problem 4 Interval Bisection Method Function
function [xk,i,error] = Intbisections(f,a,b,epsilon)
i = 1;
error = [];
if f(a)*f(b)>0
disp('Wrong choice of interval')
else
xk = (a + b)/2;
error = abs(f(xk));
while error > epsilon
if f(a)*f(xk)>0
a = xk;
else
b = xk;
end
xk = (a + b)/2;
error = [error,abs(f(xk))];
i = i + 1;
end
end
end
% Problem 3: Fixed Point Method Function
function [xk,i,error]=FixPoint(xk,maxIter,f1,epsilon)
xold = xk;
error = [];
for i = 1:maxIter
xk = f1(xk);
error = [error,abs(xk-xold)];
xold = xk;
if (error(end)<epsilon)
break;
end
end
end
% Problem 4: Function for Newton's Method function
function [xk,i,error] = NewtonsMethod(xk,maxIter,f1,epsilon)
xold = xk;
error = [];
for i = 1:maxIter
[f1x,df1x]=valuedif(f1,xk);
xk = xk - (f1x/df1x);
error = [error,abs(xk-xold)];
xold = xk;
if (error(end)<epsilon)
break;
end
end
end
% Problem 5: Globally Convergent Newtons's Method Function
function[xk,i,error] = NewtonsMethodGlobal(xk,a,b,f1,epsilon)
i = 0;
error = [];
if f1(a)*f1(b)>0
disp('Wrong choice of interval')
else
error = [error,abs(f1(xk))];
while double(error(end)) > epsilon
[f1x,df1x]=valuedif(f1,xk);
xk = xk - (f1x/df1x);
if (xk > a) && (xk < b)
[f1x,df1x]=valuedif(f1,xk);
xk = xk - (f1x/df1x);
else
xk = (a+b)/2;
end
if (f1(a)*f1(xk)) > 0
a = xk;
else
b = xk;
end
xk = (a + b)/2;
error = [error, abs(f1(xk))];
i = i + 1;
end
end
end
% Problem 1: Function for calling the value and the derivative of the function
function [xvalue,difx] = valuedif(f,xv)
xvalue = f(xv);
syms x
if nargout > 1
g(x) = diff(f(x));
difx = double(g(xv));
end
end

#### 1 Comment

Sarah Smith on 26 Jan 2020
That make sense. Thank you!