# How to plot the error of two numerical methods on the same graph?

3 views (last 30 days)
Omar B. on 16 Feb 2024
Commented: Star Strider on 16 Feb 2024
I'm trying to plot the error of two methods, but I got the following error
Index exceeds the number of array elements (3)
Also, How can I plot the error of the two methods on the same graph?
function xnew = newtonmethod(f,df,x0,tol,n)
%% Given data
f=@(x) 8-4.5*(x-sin(x));
df=@(x) -4.5*(1-cos(x));
x0=1;
tol=0.0001;
n=50;
%% Newton code
disp('No Itr Solution Error ')
Error=[];
for i=1:n
xnew=x0-(f(x0)/df(x0));
err=abs(xnew-x0);
fprintf('%3i %11.4f %11.4f %11.4f\n',i,x0,err);
if (err<tol)
break
end
x0=xnew;
Error=[Error;err];
end
%% Graph
plot(1:i,Error(1:i),'r-','Linewidth',02)
xlabel('No of Iteration','Interpreter','latex','FontSize',12)
ylabel('Error=\$|x_{n+1}-n_n|\$','Interpreter','latex','FontSize',12)
title('Error Decay','Interpreter','latex','FontSize',12)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 2nd method
function xnewh = Hmethod(f,df,ddf,x0,tol,n)
%% Given data
f=@(x) 8-4.5*(x-sin(x));
df=@(x) -4.5*(1-cos(x));
ddf=@(x) -4.5*sin(x);
x0=1;
tol=0.0001;
n=50;
%% code
disp('No Itr Solution Errorh')
Errorh=[];
for i=1:n
xnewh=x0- (2*f(x0).*df(x0)) ./ (2*(df(x0)).^2-ddf(x0).*f(x0));
errh=abs(xnewh-x0);
fprintf('%3i %11.4f %11.4f\n',i,x0,errh);
if (errh<tol)
break
end
x0=xnewh;
Errorh=[Errorh;errh];
end
%% Graph
plot(1:i,Errorh(1:i),'b-','Linewidth',02)
xlabel('No of Iteration','Interpreter','latex','FontSize',12)
ylabel('Error=\$|x_{n+1}-n_n|\$','Interpreter','latex','FontSize',12)
title('Error Decay','Interpreter','latex','FontSize',12)
%

Star Strider on 16 Feb 2024
I cannot run your code because I do not have arguments for the functions. (I tweaked them to make them a bit more efficient.)
Plotting both results in the same axes is relativbely straightforward.
Example (using different functions)
figure
plot1
hold on
plot2
hold off
grid
function plot1
plot((0:0.1:5), sin((0:0.1:5)*pi))
end
function plot2
plot((0:0.1:5), cos((0:0.1:5)*pi))
end
%
%
% function xnew = newtonmethod(f,df,x0,tol,n,Axh)
% %% Given data
% f=@(x) 8-4.5*(x-sin(x));
% df=@(x) -4.5*(1-cos(x));
% x0=1;
% tol=0.0001;
% n=50;
% %% Newton code
% disp('No Itr Solution Error ')
% Error=zeros(1,n);
% for i=1:n
% xnew=x0-(f(x0)/df(x0));
% err=abs(xnew-x0);
% fprintf('%3i %11.4f %11.4f %11.4f\n',i,x0,err);
% if (err<tol)
% break
% end
% x0=xnew;
% Error(i)=err;
% end
% %% Graph
%
%
% plot(1:n,Error,'r-','Linewidth',02)
% xlabel('No of Iteration','Interpreter','latex','FontSize',12)
% ylabel('Error=\$|x_{n+1}-n_n|\$','Interpreter','latex','FontSize',12)
% title('Error Decay','Interpreter','latex','FontSize',12)
% end
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %% 2nd method
% function xnewh = Hmethod(f,df,ddf,x0,tol,n,Axh)
% %% Given data
% f=@(x) 8-4.5*(x-sin(x));
% df=@(x) -4.5*(1-cos(x));
% ddf=@(x) -4.5*sin(x);
%
% x0=1;
% tol=0.0001;
% n=50;
% %% code
% disp('No Itr Solution Errorh')
%
% Errorh=zeros(1,n);
% for i=1:n
%
% xnewh=x0- (2*f(x0).*df(x0)) ./ (2*(df(x0)).^2-ddf(x0).*f(x0));
% errh=abs(xnewh-x0);
%
% fprintf('%3i %11.4f %11.4f\n',i,x0,errh);
% if (errh<tol)
% break
% end
%
% x0=xnewh;
% Errorh(i)=errh;
% end
% %% Graph
%
%
% plot(1:n,Errorh,'b-','Linewidth',02)
% xlabel('No of Iteration','Interpreter','latex','FontSize',12)
% ylabel('Error=\$|x_{n+1}-n_n|\$','Interpreter','latex','FontSize',12)
% title('Error Decay','Interpreter','latex','FontSize',12)
% end
% %
.
Omar B. on 16 Feb 2024
Thank you so much. Finally, I got it
Star Strider on 16 Feb 2024
As always, my pleasure!

Torsten on 16 Feb 2024
Edited: Torsten on 16 Feb 2024
%% Given data
f=@(x) 8-4.5*(x-sin(x));
df=@(x) -4.5*(1-cos(x));
x0=1;
tol=0.0001;
n=50;
[x_newton,i_newton,Error_newton]=newtonmethod(f,df,x0,tol,n);
No Itr Solution Error 1 1.0000 3.5224 2 4.5224 3.1347 3 1.3877 1.6791 4 3.0668 0.6080 5 2.4588 0.0282 6 2.4306 0.0001 7 2.4305 0.0000
ddf=@(x) -4.5*sin(x);
[x_Hmethod,i_Hmethod,Error_Hmethod]=Hmethod(f,df,ddf,x0,tol,n);
No Itr Solution Errorh 1 1.0000 0.8339 2 1.8339 0.5654 3 2.3993 0.0312 4 2.4305 0.0000
%Plot results
hold on
plot(1:i_newton,Error_newton,'r-','Linewidth',02)
plot(1:i_Hmethod,Error_Hmethod,'b-','Linewidth',02)
hold off
xlabel('No of Iteration','Interpreter','latex','FontSize',12)
ylabel('Error=\$|x_{n+1}-n_n|\$','Interpreter','latex','FontSize',12)
title('Error Decay','Interpreter','latex','FontSize',12)
function [xnew,i,Error] = newtonmethod(f,df,x0,tol,n)
%% Newton code
disp('No Itr Solution Error ')
Error=[];
for i=1:n
xnew=x0-(f(x0)/df(x0));
err=abs(xnew-x0);
Error=[Error;err];
fprintf('%3i %11.4f %11.4f\n',i,x0,err);
if (err<tol)
break
end
x0=xnew;
end
end
%% 2nd method
function [xnewh,i,Errorh] = Hmethod(f,df,ddf,x0,tol,n)
%% code
disp('No Itr Solution Errorh')
Errorh=[];
for i=1:n
xnewh=x0- (2*f(x0).*df(x0)) ./ (2*(df(x0)).^2-ddf(x0).*f(x0));
errh=abs(xnewh-x0);
Errorh=[Errorh;errh];
fprintf('%3i %11.4f %11.4f\n',i,x0,errh);
if (errh<tol)
break
end
x0=xnewh;
end
end
Omar B. on 16 Feb 2024
Thank you so much.