MATLAB: I need to save the output into an array so i can plot the results

arrayarraysforfor loopmatrix array

Hi I wrote the following function so that it could be used in general
function [fprimeapprox1,fprimeapprox2]= threepointformula(Fun,FunPrime)
disp(' t f(t) fprimeexact fprimeapprox1 Error fprimeapprox2 Error ')
for t=0:0.1:2.5
h=0.1;
fexact=feval(Fun,t);
fprimeexact= feval(FunPrime,t);
fprimeapprox1= (1/(2*h))*((feval(Fun,t+h)-feval(Fun,t-h)));
fprimeapprox2= (1/(2*h))*(-3*feval(Fun,t)+4*feval(Fun,t+h)-feval(Fun,t+2*h));
ErrorOf1= abs(fprimeexact-fprimeapprox1);
ErrorOf2= abs(fprimeexact-fprimeapprox2);
fprintf('\t%f \t%f \t%f \t%f \t%f \t%f \t%f \n',t,fexact,fprimeexact,fprimeapprox1,ErrorOf1,fprimeapprox2,ErrorOf2)
end
and then a script that calls it
clc
u= @(x) exp(-x).*cos(5.*x);
v= @(x) -5*exp(-x).*sin(5.*x)-exp(-x).*cos(5*x);
Y= threepointformula(u,v);
so my issue is that in the function i need to save fexact fprimeexact fprimeapprox1 and fprimeapprox2 into an array so i can graph it vs t. I've seen a lot of people make a zeros vector and start adding them in but my increments aren't by positive integers any help would be greatly appreciated thank you.

Best Answer

function [t,FE,FPE,FPAX1,FPAX2,ErrorOf1,ErrorOf2]= threepointformula(Fun,FunPrime)
t=0:0.1:2.5;
FE=zeros(length(t),1);
FPE=zeros(length(t),1);
FPAX1=zeros(length(t),1);
FPAX2=zeros(length(t),1);
for i=1:length(t)
h=0.1;
fexact=feval(Fun,t(i));
FE(i)=fexact;
fprimeexact= feval(FunPrime,t(i));
FPE(i)=fprimeexact;
fprimeapprox1= (1./(2.*h)).*((feval(Fun,t(i)+h)-feval(Fun,t(i)-h)));
FPAX1(i)=fprimeapprox1;
fprimeapprox2= (1./(2.*h)).*(-3.*feval(Fun,t(i))+4.*feval(Fun,t(i)+h)-feval(Fun,t(i)+2.*h));
FPAX2(i)=fprimeapprox2;
end
ErrorOf1= abs(FPE-FPAX1);
ErrorOf2= abs(FPE-FPAX2);
to call the function
clc
clear all
u= @(x) exp(-x).*cos(5.*x);
v= @(x) -5*exp(-x).*sin(5.*x)-exp(-x).*cos(5*x);
[t,FE,FPE,FPAX1,FPAX2,ErrorOf1,ErrorOf2]= threepointformula(u,v);
DISP=[t',FE,FPE,FPAX1,ErrorOf1,FPAX2,ErrorOf2];
disp(' t FE FPE FPAX1 Error FPAX2 Error')
disp(DISP)
plot(t,FE)