In: Computer Science
Write a function in Matlab that will compute the approximation for ln(x) given the inputs to the function are x and n. This function requires a running sum using a for-loop. ln (?) = lim?→∞∑ (−1)^(?−1)*(? − 1)^?/k
Hi,
Please find the Matlab script below:
--------------------------------------------------------
Matlab Function:
function [output] = lnOfX(x,n)
%function in Matlab that will compute the approximation for
ln(x)
%given the
%Inputs to the function are : x and n
%For loop
sum=0;
for k=1:n
%Accumulate
sum = sum + (((-1)^(k-1))*(((x-1)^k)/k));
% fprintf('Sum = %6.2f \n',sum);
end
output=sum;
end
Driver Script:
%Sample Driver Script
result = lnOfX(1/2,10);
fprintf('Approximation for ln(x) = %6.2f \n',result);
Screenshot:

------------------------------------------------------------------
Like the solution if you find it useful.
Hope this helps.