Using MATLAB Write a function that computes the natural loga

Using MATLAB

Write a function that computes the natural logarithm of x, using the Taylor\'s Series (TS) for log(x) (natural log). The basic definition is given in the following formula. log(x) = sigma_k=1^infinity (-1)^(k+1) * (x - 1)^k/k for 0

Solution


%matlab code

function [y,terms] = TS_log(x, MaxTerms)
if x < 2 && x > 0
y = 0;
terms = MaxTerms;
for k=1:terms
y = y + power((-1),(k+1))*power((x-1),k)/k;
end
elseif (x > 2)
y = 0.69314718055994529 + TS_log(x/2, MaxTerms);
end
end


x = input(\"Enter x: \");
MaxTerms = 10;
[y,terms] = TS_log(x, MaxTerms);
fprintf(\"y: %0.6f\ \", y);

%{
output:

Enter x: 1.4
y: 0.336469

%}

Using MATLAB Write a function that computes the natural logarithm of x, using the Taylor\'s Series (TS) for log(x) (natural log). The basic definition is given

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site