Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

The Maclaurin series expansion for sin x is: Sin x =x-x^3/3!+x^5/5!+... Edit the MATLAB function IterMeth (shown on page 94 of the course text) to approximate this series. Run the function for the following input variables to make sure that the function runs. x=pi/6 stopping criteria, es= 0.0001 Maximum iterations, maxit=50

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