Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using one of the most beautiful formulas of mathematics, <a href="http://en.wikipedia.org/wiki/Euler%27s_formula" rel="nofollow noreferrer">Euler's formula</a><br> <code>exp(i*x) = cos(x) + i*sin(x)</code>,</p> <p>substituting <code>x := n * phi</code>:</p> <p><code>cos(n*phi) = Re( exp(i*n*phi) )</code><br> <code>sin(n*phi) = Im( exp(i*n*phi) )</code></p> <p><code>exp(i*n*phi) = exp(i*phi) ^ n</code></p> <p>Power <code>^n</code> is <code>n</code> repeated multiplications. Therefore you can calculate <code>cos(n*phi)</code> and simultaneously <code>sin(n*phi)</code> by repeated complex multiplication by <code>exp(i*phi)</code> starting with <code>(1+i*0)</code>.</p> <p>Code examples:</p> <p>Python:</p> <pre><code>from math import * DEG2RAD = pi/180.0 # conversion factor degrees --&gt; radians phi = 10*DEG2RAD # constant e.g. 10 degrees c = cos(phi)+1j*sin(phi) # = exp(1j*phi) h=1+0j for i in range(1,10): h = h*c print "%d %8.3f"%(i,h.real) </code></pre> <p>or C:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;math.h&gt; // numer of values to calculate: #define N 10 // conversion factor degrees --&gt; radians: #define DEG2RAD (3.14159265/180.0) // e.g. constant is 10 degrees: #define PHI (10*DEG2RAD) typedef struct { double re,im; } complex_t; int main(int argc, char **argv) { complex_t c; complex_t h[N]; int index; c.re=cos(PHI); c.im=sin(PHI); h[0].re=1.0; h[0].im=0.0; for(index=1; index&lt;N; index++) { // complex multiplication h[index] = h[index-1] * c; h[index].re=h[index-1].re*c.re - h[index-1].im*c.im; h[index].im=h[index-1].re*c.im + h[index-1].im*c.re; printf("%d: %8.3f\n",index,h[index].re); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload