Cosine(A/3)

From testwiki
Jump to navigation Jump to search

Template:RoundBoxTop

File:1008Cos3A.png
Figure 1: Graph of cos(3A)=4cos3(A)3cos(A)
This formula and/or graph are usually used to calculate cos(A) if cos(3A) is given.

The cosine triple angle formula is: cos(3θ)=4cos3θ3cosθ. This formula, of form y=4x33x, permits cos(3θ) to be calculated if cosθ is known.

If cos(3θ) is known and the value of cosθ is desired, this identity becomes: 4cos3θ3cosθcos(3θ)=0. cosθ is the solution of this cubic equation.

In fact this equation has three solutions, the other two being cos(θ±120).

cos(3(θ±120))=cos(3θ±360)=cos(3θ).

Perhaps the simplest solution of the cubic equation with three real roots depends on the calculation of cosA3. The two are mutually dependent.

This page contains a method for calculating cosθ from cos(3θ) that is not dependent on:

  • calculus,
  • the solution of a cubic equation, or
  • either value of θ or 3θ.

Template:RoundBoxBottom

Background

Template:RoundBoxTop The value 13 can be approximated by the sequence 14+142+143+144+

For example 14+142+143++1420 equals 0.33333333333303017 Template:RoundBoxTop Testing with sequences of different lengths indicates that, if sequence contains N terms, result is accurate to 0.6N places of decimals.

In this example the sequence contains 20 terms and result is accurate to 12 places of decimals. Template:RoundBoxBottom cosA3=cos(A(14+142+143+144+)) =cos(A4+A42+A43+A44+) Template:RoundBoxBottom

Implementation

Template:RoundBoxTop

# python code
from decimal import *
getcontext().prec = 100

def cosQuarterAngle (cos4A) :
    cos4A = Decimal(str(cos4A))
    cos2A = ((cos4A+1)/2).sqrt()
    return ((cos2A+1)/2).sqrt()

def cosAplusB (cosA,cosB) :
    cosA,cosB = [ Decimal(str(v)) for v in (cosA,cosB) ]
    sinA = (1 - cosA*cosA).sqrt()
    sinB = (1 - cosB*cosB).sqrt()
    v1 = cosA*cosB
    v2 = sinA*sinB
    return v1 - v2, v1 + v2

def cosAfrom_cos3A(cos3A) :
    cos3A = Decimal(str(cos3A))
    if 1 >= cos3A >= -1 : pass
    else :
        print ('cosAfrom_cos3A(cos3A) : cos3A not in valid range.')
        return None
    if cos3A == 0 :
        # 3A = 90 , return cos30
        # 3A = 90 + 360 = 450 , return cos150, 30+120
        # 3A = 90 + 720 = 810 , return cos270, 30-120 = -90
        cos30 = Decimal(3).sqrt()/2
        return cos30, -cos30, Decimal(0)
    cos60 = Decimal('0.5')
    if cos3A == 1 :
        # 3A = 0 , return cos0
        # 3A = 360 , return cos120, 0+120
        # 3A = 720 , return cos240, 0-120
        return Decimal(1), -cos60, -cos60
    if cos3A == -1 :
        # 3A = 180 , return cos60
        # 3A = 180 + 360 = 540 , return cos180, 60+120
        # 3A = 180 + 720 = 900 , return cos300, 60-120 = -60
        return cos60, Decimal(-1), cos60

    cosA = cosQuarterAngle (cos3A)
    next = cosQuarterAngle (cosA)
    count = 0
    while(next != 1):
        cosA = cosAplusB (cosA,next) [0]
        next = cosQuarterAngle (next)
        count += 1
    print ('count =',count)
    cos120 = -cos60
    return (cosA,) + cosAplusB (cosA,cos120)

Result is accurate to about half the precision. For example, if precision is set to 100, result is accurate to approximately 50 places of decimals, and is achieved with about 81 passes through loop. Template:RoundBoxBottom