How to write the Taylor Series approximation for arctan?

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

Antworten (1)

Roger Stafford
Roger Stafford am 13 Nov. 2014
Bearbeitet: Roger Stafford am 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.

Kategorien

Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange

Tags

Noch keine Tags eingegeben.

Gefragt:

n
n
am 13 Nov. 2014

Bearbeitet:

am 13 Nov. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by