Estimate the natural logarithm using a Taylor While Loop

6 visualizaciones (últimos 30 días)
Morgan Altherr
Morgan Altherr el 5 de Oct. de 2021
Editada: Harsh Mahalwar el 24 de Feb. de 2024
Hey guys, I wanted to know if I could get some help writing code for this problem. I am having trouble defining variables and the actually while loop:
Estimate the natural logarithm of a number with an error less than a specified error tolerance. Error of the estimated value is calculated as the absolute value of the difference between the estimated value and the actual value. Have the user input a number x such that 0 < x < 2 and an error tolerance. Include error handling (using a while loop) to make sure that the user inputs a number in the specified range and provide guidance to the user to explain the appropriate range. Pass the number and error tolerance as input arguments to a function that calculates the natural logarithm for the numbers using the following Taylor Series expansion: (Since it is not practical to add an infinite number of terms, include the number of terms that is required to produce the first sum that gives a value with an error that is less than the error tolerance.)
How can i approach this? Also what code can accompolish this?

Respuestas (1)

Harsh Mahalwar
Harsh Mahalwar el 23 de Feb. de 2024
Editada: Harsh Mahalwar el 24 de Feb. de 2024
Hi Morgon,
As I can understand that you are trying to code Taylor series in MATLAB. Also, you want to add some error handling components with it.
Above is the Mercator series which is also the Taylor series for the natural algorithm, important points to know about it:
  1. It only works between -1 < x < 1 (which isn’t going to work for your use case.)
  2. It’s very slow!!
I was able to find a thread on Mathematics stack exchange which gave me an expansion of this Taylor series which is way faster than the previous approach, you can check out that thread from the following link,
For implementing my code, I will be using this equation:
This equation works for x > 0.
Here’s a code snippet to help you out:
function logVal = taylorSeries(maxIterations, x)
it = 1; logVal = 0; currPower = 1;
% baseEq and expoHelper help us with reaching to the next iteration.
baseEq = (x + 1) / (x - 1);
expoHelper = ((x - 1) * (x - 1)) / ((x + 1) * (x + 1));
while it <= maxIterations
baseEq = baseEq * expoHelper;
z = (1 / currPower) * baseEq;
% adds the latest iteration to the main equation.
logVal = logVal + z;
currPower = currPower + 2; it = it + 1;
end
logVal = 2 * logVal;
end
To display information about the x limits to the user, you can do that using disp function, check out the following documentation to learn more about it,
For error handling you can use if-else statements or try-catch in MATLAB, learn more about them using the following link,
  1. Execute statements if condition is true - MATLAB if elseif else - MathWorks
  2. Execute statements and catch resulting errors - MATLAB try catch - MathWorks
Using warning or error messages seems like a good idea here, check out documentation on them by clicking on the following links,
I hope this helps, thanks!

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by