# Making a program to convert K(1+cos(nQ-phi)) to a power series

1. Jun 22, 2012

### shitij

Hi all !!

I am a summer trainee and am currently making (modifying: I am changing the way the program takes input) a program in Fortran, and my math is a little shaky.
What I have is an input file with 3 parameters in it for the equation (which is the equation for calculating torsional potential):

K (1+cos(nQ-phi))

The parameters given to me are: K, n and phi.

Now the program takes 7 parameters for this equation which are the prefactors for the corresponding power series (it does it that way because calculating double differential is easier then).
So, I have to convert the given 3 parameters to the 7 parameters that the programs wants.
How do I do that?

I looked at the taylor series expansion on wikipedia, but I am not sure what I should expand this against. Is power series = maclaurin series? So I have to expand this around a=0?

Thanks !!

2. Jun 22, 2012

### chiro

Hey garbageij and welcome to the forums.

The major use for having different points to expand around in taylor series relates to having convergence properties in some neighborhood of the point you are expanding around.

If you know the function is defined everywhere, converges everywhere and is differentiable everywhere, then you can use any point you want.

In terms of the neighbourhood, the reason why people might want to use different points of expansion is because you may want to get an approximation to a point 'close' to the one you are expanding around and because it's close, a lot of the derivative terms (especially the higher ones) can be ignored to give a good approximation.

Since the cos function is defined everywhere, converges everywhere, and is differentiable everywhere, you can use whatever point of expansion you wish including a = 0. In fact, using a = 0 makes things a lot easier in terms of the algebra and calculation (i.e. you have x^n instead of something like (x-a)^n for some non-zero a).

3. Jun 22, 2012

### shitij

Hmm...actually I asked my prof that "From what I understand, power series have infinite terms, so how many terms shall I consider?" (where should I truncate it)
He replied "Not necessarily. In many cases (like this one) it has finite terms and rest of the terms are just 0" Then he gave me an example of cos(2x)=2cos^2(x)-1. (I dont remember exactly, but I think this is somewhat what he said)

The thing is, that when I am expanding it around Q=0, I am not getting finite terms (like he said I should). Like I am getting terms like:
0 + n*K*sin(phi)*Q/1! - n2*K*cos(-phi)*Q2/2!-n3*K*sin(phi)*Q3/3! + ............

This will go on forever, right? Am I doing something wrong? I feel there should be some correlation between "n" and the number of non-zero terms in the power series. Also he always mentioned the term power series, is mclaurin series=power series???

Thanks !!

4. Jun 22, 2012

### chiro

You should get an infinite power series expansion for your nQ term (I'm assuming you are expanding the series around -phi, so this is OK because the trig functions are infinite-power series and in general, can not be reduced down to polynomials.

I'm guessing (and you would have to check this) is that you want n terms (in your case possibly seven) which is a good enough approximation to work with so that you will get a seventh degree polynomial for Q which you can solve using numerical techniques. You can then take this Q and simply apply it your application (i.e. torsion).

The equation to find f(Q) (i.e the taylor series approximation you calculated) = 0 can be solved using what is known as a root finding algorithm like Newton-Rhapson, which requires the derivative. Given that you have a polynomial approximation, the derivative is easy to find using term by term differentiation.