Date: 8/02/2016 11:49:01
From: mollwollfumble
ID: 843589
Subject: Quick trig question (in Excel)

Given x and y, where x=cos(theta) and y=sin(theta).
What’s the simplest formula (eg. in Excel) for finding theta (in range 0 to 360 degrees) from x and y?

I can’t simply use acos(x)*180/pi() for instance because that gives an answer between -90 and 90, not between 0 and 360.

Reply Quote

Date: 8/02/2016 12:01:04
From: mollwollfumble
ID: 843607
Subject: re: Quick trig question (in Excel)

mollwollfumble said:


Given x and y, where x=cos(theta) and y=sin(theta).
What’s the simplest formula (eg. in Excel) for finding theta (in range 0 to 360 degrees) from x and y?

I can’t simply use acos(x)*180/pi() for instance because that gives an answer between -90 and 90, not between 0 and 360.


Ah, now I see my mistake, acos(x)*180/pi() gives an answer between 0 and 180, so all I need is
if(y>=0, acos(x)*180/pi(), 360-acos(x)*180/pi())

As you were.

Reply Quote

Date: 8/02/2016 12:03:01
From: The Rev Dodgson
ID: 843612
Subject: re: Quick trig question (in Excel)

mollwollfumble said:


Given x and y, where x=cos(theta) and y=sin(theta).
What’s the simplest formula (eg. in Excel) for finding theta (in range 0 to 360 degrees) from x and y?

I can’t simply use acos(x)*180/pi() for instance because that gives an answer between -90 and 90, not between 0 and 360.

=Degrees(ATAN2)

of *180/pi() if you prefer.

Reply Quote

Date: 8/02/2016 12:10:08
From: The Rev Dodgson
ID: 843622
Subject: re: Quick trig question (in Excel)

mollwollfumble said:


mollwollfumble said:

Given x and y, where x=cos(theta) and y=sin(theta).
What’s the simplest formula (eg. in Excel) for finding theta (in range 0 to 360 degrees) from x and y?

I can’t simply use acos(x)*180/pi() for instance because that gives an answer between -90 and 90, not between 0 and 360.


Ah, now I see my mistake, acos(x)*180/pi() gives an answer between 0 and 180, so all I need is
if(y>=0, acos(x)*180/pi(), 360-acos(x)*180/pi())

As you were.

OK, but ATAN2 gives you the answer without thinking about it (other than remembering it is ATAN2, rather than the other way round).

Reply Quote

Date: 8/02/2016 12:13:52
From: The Rev Dodgson
ID: 843623
Subject: re: Quick trig question (in Excel)

Should be:

(other than remembering it is ATAN2 (x, y), rather than the other way round).
Reply Quote

Date: 8/02/2016 12:15:58
From: The Rev Dodgson
ID: 843625
Subject: re: Quick trig question (in Excel)

The Rev Dodgson said:


mollwollfumble said:

Given x and y, where x=cos(theta) and y=sin(theta).
What’s the simplest formula (eg. in Excel) for finding theta (in range 0 to 360 degrees) from x and y?

I can’t simply use acos(x)*180/pi() for instance because that gives an answer between -90 and 90, not between 0 and 360.

=Degrees(ATAN2)

of *180/pi() if you prefer.

Just noticed that’s lost the stuff inside brackets as well.

=Degrees(ATAN2 (x, y))

but without the space between 2 and (

Reply Quote

Date: 8/02/2016 13:27:03
From: mollwollfumble
ID: 843662
Subject: re: Quick trig question (in Excel)

PS, I seem to have solved the numerical sign problem in quantum mechanics.

The problem is that integrals such as
\int_{-\infty}^\infty g(x) e^{i f(x)} dx
where x is a vector in n-D, are highly oscillatory because the real part of e^{i theta} is cos(theta) which misbehaves as theta tends to infinity.

To overcome this, in lattice QCD they solve integrals such as
\int_{-\infty}^\infty g(x) e^{-f(x)} dx
because e^{-y} is non-oscillatory. They do this because Wick rotation (treating the time axis as an imaginary space axis) converts the first integral into the second. But sometimes Wick rotation can’t be used, hence the problem.

To solve
\int_{-\infty}\infty g(x) e^{i f(x)} dx
I solve the real and imaginary components separately, and I reduce the n-D problem to a Riemann sum of 1-D problems along radial lines from 0 to infinity. Each 1-D integral is then
(S_n) m \int_0^\infty y^{n-1} g(y) cos(f(y)) dy
where S_n is the surface area of a unit ball in n dimensions
and m is the number of 1-D vectors used in the Riemann sum.

The 1-D problems are even more oscillatory than the original problem in n-D, which is I suppose why nobody has used this in the past. I split the 1-D integral into an inner part f(y)<pi and="" an="" outer="" part="" f(y)="">pi.
The inner part is evaluated directly using standard Gauss quadrature.

For the outer part I greatly reduce the oscillations by a change of variable from y to f=f(y) and repeated integration by parts. The change of variable (after dividing the constant) gives
\int_\pi^\infty y^(0) cos(f) df
where y^(0)=y^{n-1} g(y) / (df/dy) is found analytically. Differentiate y^(0) numerically four times to get y^(1), y^(2), y^(3), y^(4).

Then after four integrations by parts
\int_\pi^\infty y^(0) cos(f) df = y^(1) – y^(3) + \int_\pi^\infty y^(4) cos(f) df
the oscillations (on my test problem in 7-D) are so suppressed that infinity can be replaced by 4 times pi, and the resulting integral is evaluated using standard Gauss quadrature.

That’s all there is to it, apart from minor tweaks that reduce numerical errors and increase computing speed.

Reply Quote

Date: 8/02/2016 13:33:54
From: mollwollfumble
ID: 843663
Subject: re: Quick trig question (in Excel)

A few minor bug fixes in the above.

mollwollfumble said:



Each 1-D integral is then
(S_n)/m \int_0^\infty y^{n-1} g(y) cos(f(y)) dy
where S_n is the surface area of a unit ball in n dimensions
and m is the number of 1-D vectors used in the Riemann sum.

The 1-D problems are even more oscillatory than the original problem in n-D, which is I suppose why nobody has used this in the past. I split the 1-D integral into an inner part f(y) less than pi and an outer part f(y) greater than pi.

Reply Quote

Date: 8/02/2016 13:46:59
From: The Rev Dodgson
ID: 843672
Subject: re: Quick trig question (in Excel)

If you are doing numerical integration over a semi-infinite range (with or without oscillations) you might like to have a look at:

https://newtonexcelbach.wordpress.com/2016/02/06/tanh-sinh-quadrature-via-f2py-part-3/

and the preceding 2 posts.

Reply Quote

Date: 8/02/2016 14:06:45
From: mollwollfumble
ID: 843686
Subject: re: Quick trig question (in Excel)

The Rev Dodgson said:


If you are doing numerical integration over a semi-infinite range (with or without oscillations) you might like to have a look at:

https://newtonexcelbach.wordpress.com/2016/02/06/tanh-sinh-quadrature-via-f2py-part-3/

and the preceding 2 posts.


I did have alook at this :-) It’s quite interesting, but it actually makes oscillations more violent, resulting in a more spectacular failure.

One software collection includes a variant of tanh-sinh quadrature that is intended for use specifically on oscillatory functions, lets see if I can spot it on the web. I don’t see it. I haven’t tried it, because what I’ve derived from first principles works exceedingly well.

If I wrote up my method, would you be interested in adding it to newtonexcelbach?

Reply Quote

Date: 8/02/2016 14:13:20
From: The Rev Dodgson
ID: 843690
Subject: re: Quick trig question (in Excel)

mollwollfumble said:


The Rev Dodgson said:

If you are doing numerical integration over a semi-infinite range (with or without oscillations) you might like to have a look at:

https://newtonexcelbach.wordpress.com/2016/02/06/tanh-sinh-quadrature-via-f2py-part-3/

and the preceding 2 posts.


I did have alook at this :-) It’s quite interesting, but it actually makes oscillations more violent, resulting in a more spectacular failure.

One software collection includes a variant of tanh-sinh quadrature that is intended for use specifically on oscillatory functions, lets see if I can spot it on the web. I don’t see it. I haven’t tried it, because what I’ve derived from first principles works exceedingly well.

That’s included in the download. The Quad_TSO function is specifically for oscillatory functions, and seems to work well. There is also a link to the original Fortran code, if you don’t want to mess around with Excel and Python.

mollwollfumble said:


If I wrote up my method, would you be interested in adding it to newtonexcelbach?

Sure, if I can understand it :)

Reply Quote

Date: 8/02/2016 15:09:57
From: mollwollfumble
ID: 843743
Subject: re: Quick trig question (in Excel)

The Rev Dodgson said:


mollwollfumble said:

If I wrote up my method, would you be interested in adding it to newtonexcelbach?

Sure, if I can understand it :)


My full equations are on Page 2 of Physics Forum Numerical sign problem software? with some introductory information on Page 1.

but that’s probably too messy for you. I guess you’d be much more interested in the part that relates specifically to 1-D integrals that previously have been impossible to evaluate.

Reply Quote

Date: 8/02/2016 15:44:03
From: The Rev Dodgson
ID: 843756
Subject: re: Quick trig question (in Excel)

mollwollfumble said:


The Rev Dodgson said:

mollwollfumble said:

If I wrote up my method, would you be interested in adding it to newtonexcelbach?

Sure, if I can understand it :)


My full equations are on Page 2 of Physics Forum Numerical sign problem software? with some introductory information on Page 1.

but that’s probably too messy for you. I guess you’d be much more interested in the part that relates specifically to 1-D integrals that previously have been impossible to evaluate.

I’ll have a look

Reply Quote

Date: 8/02/2016 16:04:17
From: The Rev Dodgson
ID: 843776
Subject: re: Quick trig question (in Excel)

The Rev Dodgson said:

I’ll have a look

Fairly heavy going :)

If you can get something together that I can code as a VBA UDF I’d certainly be interested in that.

Reply Quote

Date: 9/02/2016 15:25:33
From: mollwollfumble
ID: 844287
Subject: re: Quick trig question (in Excel)

The Rev Dodgson said:


The Rev Dodgson said:

I’ll have a look

Fairly heavy going :)

If you can get something together that I can code as a VBA UDF I’d certainly be interested in that.

Can you read Fortran? As in old fashioned Fortran 77 with vax extensions (such as endif and with tab replacing the leading blanks).

Reply Quote

Date: 9/02/2016 15:41:56
From: The Rev Dodgson
ID: 844300
Subject: re: Quick trig question (in Excel)

mollwollfumble said:


The Rev Dodgson said:

The Rev Dodgson said:

I’ll have a look

Fairly heavy going :)

If you can get something together that I can code as a VBA UDF I’d certainly be interested in that.

Can you read Fortran? As in old fashioned Fortran 77 with vax extensions (such as endif and with tab replacing the leading blanks).

My Fortran is pretty rusty, but F2Py reads it just fine.

Reply Quote

Date: 10/02/2016 19:54:38
From: mollwollfumble
ID: 844878
Subject: re: Quick trig question (in Excel)

The Rev Dodgson said:


mollwollfumble said:

The Rev Dodgson said:

Fairly heavy going :)

If you can get something together that I can code as a VBA UDF I’d certainly be interested in that.

Can you read Fortran? As in old fashioned Fortran 77 with vax extensions (such as endif and with tab replacing the leading blanks).

My Fortran is pretty rusty, but F2Py reads it just fine.

Here it is. The computer program is http://freepages.misc.rootsweb.com/~hallsofjamaica/intgcosf.f
The users guide is http://freepages.misc.rootsweb.com/~hallsofjamaica/Intgcosf.pdf

Reply Quote

Date: 10/02/2016 22:22:15
From: The Rev Dodgson
ID: 845004
Subject: re: Quick trig question (in Excel)

mollwollfumble said:


The Rev Dodgson said:

mollwollfumble said:

Can you read Fortran? As in old fashioned Fortran 77 with vax extensions (such as endif and with tab replacing the leading blanks).

My Fortran is pretty rusty, but F2Py reads it just fine.

Here it is. The computer program is http://freepages.misc.rootsweb.com/~hallsofjamaica/intgcosf.f
The users guide is http://freepages.misc.rootsweb.com/~hallsofjamaica/Intgcosf.pdf

Thanks for that. Downloaded with no problem.

I’m a bit busy at the moment, but I’ll post in this thread when I have had a look.

Reply Quote

Date: 11/02/2016 18:34:49
From: mollwollfumble
ID: 845268
Subject: re: Quick trig question (in Excel)

The Rev Dodgson said:


mollwollfumble said:

The Rev Dodgson said:

My Fortran is pretty rusty, but F2Py reads it just fine.

Here it is. The computer program is http://freepages.misc.rootsweb.com/~hallsofjamaica/intgcosf.f
The users guide is http://freepages.misc.rootsweb.com/~hallsofjamaica/Intgcosf.pdf

Thanks for that. Downloaded with no problem.

I’m a bit busy at the moment, but I’ll post in this thread when I have had a look.


A table of integrals from intgcosf(), with help for the near-zero component out to pi from a calculator on the web.

Reply Quote

Date: 13/02/2016 16:45:31
From: The Rev Dodgson
ID: 846140
Subject: re: Quick trig question (in Excel)

mollwollfumble said:


The Rev Dodgson said:

mollwollfumble said:

Here it is. The computer program is http://freepages.misc.rootsweb.com/~hallsofjamaica/intgcosf.f
The users guide is http://freepages.misc.rootsweb.com/~hallsofjamaica/Intgcosf.pdf

Thanks for that. Downloaded with no problem.

I’m a bit busy at the moment, but I’ll post in this thread when I have had a look.


A table of integrals from intgcosf(), with help for the near-zero component out to pi from a calculator on the web.

I have now compiled the Fortran code to run under Python, using F2Py, and also converted it to VBA to run from Excel. For F2Py:

- I converted the file name extension to f90, which removes the line length limit.

- As a result I had to convert the comment marker from ‘c’ to ‘!”. I also converted the variable c to !, which was not a good idea.

- I updated the Gauss integration coefficients to double precision

- I added real*8 before function names, and also replaced dimension statements with real*8 (not sure if that was essential or not)

After a fair bit of fiddling around I got it to compile, and it works from Python, but I haven’t linked that to Excel yet.

For playing with different integration functions I thought it would be easier to convert the whole thing to VBA, which actually wasn’t too hard (a lot quicker than getting it to compile).

Having done that, there are a couple of issues:

- Where x has a negative exponent the calculated integral is too small, because the integration from 0 to Pi is done in a single step, so the approximation of the infinity at x=0 is pretty crude.

- Where x has a positive exponent (especially for x^3) the result changes depending on the value of nint. I actually don’t understand how these functions can have a finite integral when they diverge as x increases, but presumably the mathematicians do.

Did you have a look at the double-exponential oscillating function integrators in the Tanh-Sinh workbook. They seem to work pretty well (and match the results in the table). On my blog there is an all VBA version (by Grame Dennes), and an Excel-Python version (by me).

For my code of your functions, I can copy them to Onedrive and give you a link, or if you would prefer e-mail (or somewhere else), let me know.

Reply Quote

Date: 24/02/2016 09:11:08
From: The Rev Dodgson
ID: 850762
Subject: re: Quick trig question (in Excel)

Bump for mollwollfumble, if you’re still around.

The Rev Dodgson said:


mollwollfumble said:

The Rev Dodgson said:

Thanks for that. Downloaded with no problem.

I’m a bit busy at the moment, but I’ll post in this thread when I have had a look.


A table of integrals from intgcosf(), with help for the near-zero component out to pi from a calculator on the web.

I have now compiled the Fortran code to run under Python, using F2Py, and also converted it to VBA to run from Excel. For F2Py:

- I converted the file name extension to f90, which removes the line length limit.

- As a result I had to convert the comment marker from ‘c’ to ‘!”. I also converted the variable c to !, which was not a good idea.

- I updated the Gauss integration coefficients to double precision

- I added real*8 before function names, and also replaced dimension statements with real*8 (not sure if that was essential or not)

After a fair bit of fiddling around I got it to compile, and it works from Python, but I haven’t linked that to Excel yet.

For playing with different integration functions I thought it would be easier to convert the whole thing to VBA, which actually wasn’t too hard (a lot quicker than getting it to compile).

Having done that, there are a couple of issues:

- Where x has a negative exponent the calculated integral is too small, because the integration from 0 to Pi is done in a single step, so the approximation of the infinity at x=0 is pretty crude.

- Where x has a positive exponent (especially for x^3) the result changes depending on the value of nint. I actually don’t understand how these functions can have a finite integral when they diverge as x increases, but presumably the mathematicians do.

Did you have a look at the double-exponential oscillating function integrators in the Tanh-Sinh workbook. They seem to work pretty well (and match the results in the table). On my blog there is an all VBA version (by Grame Dennes), and an Excel-Python version (by me).

For my code of your functions, I can copy them to Onedrive and give you a link, or if you would prefer e-mail (or somewhere else), let me know.

Reply Quote