I need to save my output into an array so i can plot my results
Mostrar comentarios más antiguos
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
Más respuestas (1)
KSSV
el 17 de Oct. de 2016
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.
Categorías
Más información sobre Profile and Improve Performance en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!