How to write the Taylor Series approximation for arctan?

7 views (last 30 days)
Here's the Taylor Series approximation:
arctan(x) = x - (1/3)x^3 + (1/5)x^5 - (1/7)x^7...
I want to write a function with two inputs (x as seen in the equation above, n is the number of terms) and two outputs (vec as a row vector where each individual term is stored, and approx as the final approximation).
Here's the beginning of my code. It doesn't work and I'm stuck.
function [vec,approx] = taninv(x,n)
for i = -1:2:n
approx = ((-1).^(i+1)).*((x^(2.*i-1))./(2.*i-1));
end

Answers (1)

Roger Stafford
Roger Stafford on 13 Nov 2014
Edited: Roger Stafford on 13 Nov 2014
You are starting i at -1 which would make the first term
x^(-3)/(-3)
You had better start i at +1. Also since the i's are all odd as you have set it up, then the exponents of the x terms will all be multiples of 4 plus 1. That isn't the right Taylor series. Better think it out again.
Advice: After writing your code, try manually executing it on paper for a small number of terms to see that the terms are being computed properly.

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!