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:
- It only works between -1 < x < 1 (which isn’t going to work for your use case.)
- 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 = (x + 1) / (x - 1);
expoHelper = ((x - 1) * (x - 1)) / ((x + 1) * (x + 1));
while it <= maxIterations
baseEq = baseEq * expoHelper;
z = (1 / currPower) * baseEq;
currPower = currPower + 2; it = it + 1;
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,
- Execute statements if condition is true - MATLAB if elseif else - MathWorks
- 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!