function [fx,ea,iter] = IterMeth(x,es,maxit) % Maclaurin series of exponential f
ID: 669271 • Letter: F
Question
function [fx,ea,iter] = IterMeth(x,es,maxit)
% Maclaurin series of exponential function
% [fx,ea,iter] = IterMeth(x,es,maxit)
% input:
% x = value at which series evaluated
% es = stopping criterion (default = 0.0001)
% maxit = maximum iterations (default = 50)
% output:
% fx = estimated value
% ea = approximate relative error (%)
% iter = number of iterations
% defaults:
if nargin<2|isempty(es),es=0.0001;end
if nargin<3|isempty(maxit),maxit=50;end
% initialization
iter = 1; sol = 1; ea = 100;
% iterative calculation
while (1)
solold = sol;
sol = sol + x ^ iter / factorial(iter);
iter = iter + 1;
if sol~=0
ea=abs((sol - solold)/sol)*100;
end
if ea<=es | iter>=maxit,break,end
end
fx = sol;
end
Explanation / Answer
x=pi/4;
% value of x where the approximation is computed
Nvalues=100;
% number of epsilon values
EpsilonVector=logspace(-10,-1,Nvalues);
NumTerms=zeros(Nvalues,1);
for
i=1:Nvalues % loop over epsilon values
epsilon=EpsilonVector(i);
SineApprox=x;
N=1; % first term of approximation
while abs(SineApprox - sin(x)) > epsilon % check for convergence
N=N+1;
SineApprox=SineApprox-((-1)^(N))*(x^(2*N-1))/factorial(2*N-1);
end
NumTerms(i)=N;
% number of tetrms for convergence
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.