The Maclaurin series expansion for ex is computed as follows

The Maclaurin series expansion for e^x is computed as follows: e^x = 1 + x + x^2/2! + x^3/3! + The function IterMeth calculates the exponential using this series. This code can be downloaded from the textbook web page or Blackboard. Make a copy of the function IterMeth and modify it to calculate a new approximation: a^x = e^x ln(a) = 1 + x ln(a) + (x ln(a))^2/2! + (x ln(a))^3/3! + Include the following modifications: Add an additional input parameter to the input of function IterMeth and rename the function. function [fx, ea, iter] = IterMeth_HW4[a, x, es, maxit] Modify the comment lines to reflect the changes made to the function. Modify the default settings on lines 14 and 15, since there are now 4 possible input values: if nargin

Solution

function [fx,ea,iter] = IterMeth_HW4(a,x,es,maxit)
% Maclaurin series expansion function for a^x
% [fx,ea,iter] = IterMeth_HW4(a,x,es,maxit)
% input:
% a = value of the number for which power needs to be calculated
% 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<3|isempty(es),es=0.0001;end
if nargin<4|isempty(maxit),maxit=50;end
% initialization
iter = 1; sol = 1; ea = 100;
% iterative calculation
while (1)
solold = sol;
sol = sol + (x*log(a)) ^ 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

OUTPUT:

>> m = IterMeth_HW4(2,0.5,0.0001,50)

m =

1.414213557004892

>> n = sqrt(2)

n =

1.414213562373095

>> error = (n-m)*100/n

error =

3.795892896314411e-07

 The Maclaurin series expansion for e^x is computed as follows: e^x = 1 + x + x^2/2! + x^3/3! + The function IterMeth calculates the exponential using this seri

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site