I need to save my output into an array so i can plot my results

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.

 Respuesta aceptada

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)

Más respuestas (1)

function [T,FE,FPE,FPAX1,FPAX2]= threepointformula(Fun,FunPrime)
disp(' t f(t) fprimeexact fprimeapprox1 Error fprimeapprox2 Error ')
T = 0:0.1:2.5 ;
FE = zeros(length(T),1) ;
FPE = FE ;
FPAX1 = FE ;
FPAX2 = FE ;
for i=1:length(T)
t = T(i) ;
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)
FE(i) = fexact ;
FPE(i) = fprimeexact ;
FPAX1(i) = fprimeapprox1 ;
FPAX2(i) = fprimeapprox2 ;
end
size(fexact)
Call the function by:
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] = threepointformula(u,v);
plot(T,FE,'r') ;
hold on
plot(T,FPE,'b') ;
plot(T,FPAX1,'m') ;
plot(T,FPAX2,'c') ;
legend('exact' , 'prime exact','appx.1','appx.2')
Note that code can be simplified.

5 comentarios

so when i ran this code it no longer prints out the values in columns like the one i wrote does it only prints out the last values when t=2.5
No change is made at the print part....only after printing it is saved in the array.
also if i ask it to show me FE its just a vector of 0's except the last row
I have plotted it at the end. Plot is coming out non zeros.
the plot is wrong as well it should be a cos graph not what you get when that code is runned

Iniciar sesión para comentar.

Categorías

Preguntada:

el 17 de Oct. de 2016

Respondida:

el 18 de Oct. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by