The Taylor series expansion of the natural log function ln
The Taylor series expansion of the natural log function ln ( x ) is given by ln (x) = ( x 1 ) ( x 1 )^2/2 + ( x 1 )^3/3 ( x 1 )^4/4 + ( x 1 )^5/5 ... This expansion is valid for values of x where 0 < x 2 . Write a Matlab function ( mylog ) that will calculate the natural log function using this expansion. This is an infinite sum, but your function should stop the summation at step k, when ( x 1 )^k/k < tol where tol is a user - supplied value. Your function should take two inputs. You must check for nonsensical input values. For the factorial, you should use your my factorial (n) function which returns n!. Your function ( mylog ) should return two values – the approximation of ln ( x ) and the num ber of steps k. Submit one function : mylog . m .
Solution
Here is the code fro you:
function [sum, steps] = mylog(x, tol)
sum = x - 1;
steps = 1;
sign = -1;
term = (x - 1)^steps / steps;
while(term >= tol)
steps = steps + 1
term = sign * (x - 1)^steps / steps;
sum = sum + term;
sign = sign * -1;
end
end
