A good numerical approximation of natural logarithm of a number between -1 and 1
ID: 3841963 • Letter: A
Question
A good numerical approximation of natural logarithm of a number between -1 and 1 is the Taylor series shown below. Develop a MATLAB code to compute the natural log of a number. Your code should keep track of the convergence error by comparing the new value with the previous iteration value (That means: store the convergence error in a vector). The program should ask the users to input a value of x, and a value for n. The code then will then provide the approximation for n- terms in the series. In(x) = (x - 1) - (x - 1)^2/2 + (x - 1)^3/3 - (x - 1)^4/4 + (x - 1)^5/5...plusminus... Features that must be included in your code: Check that the value of x is between -1 and 1 Check that the user has input a valid value for n Track the error. Plot the error vs the number of iterations (label axis). (use snipping tool OR save image as png and insert to your word document) Format your approximated value and compare to the true value (must use fprintf for this) ANSWER THIS: with your code what is In(0.5)? compare to Matlab's log(0.5)Explanation / Answer
function answer = ln (x, n)
if x < -1 || x > 1
disp("x should be between -1 and 1");
return;
end
if n < 1
disp("n should be more than 1");
end
sgn = 1;
answer = 0;
for i = 1:n
answer = answer + sgn*((x-1)^i)/i;
sgn = sgn * -1;
end
end
x = [];
y = [];
actual = log(0.7)/log(exp(1));
for i = 1:5:100
out = ln(0.7, i);
actual = log(0.7)/log(exp(1));
x = [x actual - out)];
y = [y i];
end
plot (x,y)
This will help you with the coding pat. I hope you like it.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.