MATLAB: Displaying % error

chart;homeworkpercent error display

can you just provide me another simpler code? based on the codes above, we want to compute the percent error for each h at value at time t =1,2,3,4,5?
Here is my code so far:
%Script that demonstrates Euler integration for a first order problem using
%MATLAB.
%The problem to be solved is:
%y'(t)+2*y(t)=2-exp(-4*t)
%Note: This problem has a known exact solution
%y(t)=1+0.58*exp(-4*t)-0.5*exp(-2*t)
function ystar = Eulermethod20(n)
a=0;
b=5;
h=(b-a)/n;
t=0:h:5;%Initialize time variable
clear ystar;%wipe out old variable
ystar(1)=1.0;%Initial condition (same for approximation)
for i=1:length(t)-1, %Set up "for" loop
k1=2-exp(-4*t(i))-2*ystar(i); %Calculate the derivative
ystar(i+1)=ystar(i)+h*k1;%Estimate new value of y
end
%Exact solution
y=1+0.5*exp(-4*t)-0.5*exp(-2*t);
%Error calculation
percent_error=100*abs(y-ystar)./y;
%Plot approximate and exact solutions
plot(t,ystar,'b--',t,y,'r-',t,percent_error,'g');
legend('Approximate','Exact','Error');
title('Euler Approximation n=5000');
xlabel('Time');
ylabel('y*(t), y(t)');
thanks.

Best Answer

t=1:10;
y=1:10;
str=cellstr(num2str(y.','%%%5.2f'));
plot(t,y);
text(t,y,str);