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

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)
%

 Accepted Answer

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
% %
.

6 Comments

I got an error when I plot the error
Please check my code
I cannot check it without the arguments.
Also, note the changes I made to your original code. They may solve that problem. (I commented-out your functions so they would not run and so I could provide an answer to the question you actually asked.)
What do you mean by arguments ?
I tried the following but I did not get the graph
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=11;
Error = newtonmethod2(f,df,x0,tol,n);
Errorh = Hmethod2(f,df,ddf,x0,tol,n);
x=linspace(1,11);
figure
plot(x,Error,'r-','Linewidth',02)
hold on
plot(x,Errorh,'b-','Linewidth',02)
hold off
xlabel('No of Iteration','Interpreter','latex','FontSize',12)
ylabel('Error=$|x_{n+1}-n_n|$','Interpreter','latex','FontSize',12)
I got the graph!
There is also an error in the ‘newtonmethod’ fprintf call that I corrected. (The format string had too many fields.) I also added a legend call.
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=11;
figure
Error = 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
hold on
Errorh = 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
hold off
grid
legend('newtonmethod','Hmethod','Location','best')
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\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
.
Thank you so much. Finally, I got it

Sign in to comment.

More Answers (1)

%% 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

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!