Circles

General FreeBASIC programming questions.
Locked
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Post by albert »


Hey Richard and everybody else check this out!!!!


I got ten trillion on the mag and its still a perfect dot in the middle of the error plotter.

I cheated and dialed in the 50 multipliers and put them into an array.

Without the sqr() normalization, it doesn't make a circle but the degrees are correct. With the normalization it corrects it to a perfect circle.

Uncomment the atan2(s,c) below the pset's and check out the degree output.

Code: Select all

'================================================================
' Albert's .0018 in green
'  approximations measured relative to the reference circle ... blue
Const As Double mag = 10000000000000  ' magnification of errors
'================================================================
Dim As Double t, tt, grad, q, s, c, dx, dy, x, y, r = 1.1
Dim As Double sqr_root

Screen 19
Dim As Integer w, h, depth
Screeninfo w, h, depth
Window (-r * w / h, -r) - (r * w / h, r)  ' maintain pixel aspect

'----------------------------------------------------------------
Line(-c, 0)-(c, 0), 1   ' x axis
Line(0, -c)-(0, c), 1   ' y axis
Const As Double Pion2 = 2 * Atn(1)
For t = 0 To 4 * Pion2 Step Pion2 / 1000
    x = Cos(t)
     y = Sin(t)
    Pset(x, y), 1       ' reference circle
    Pset(x*.1, y*.1), 1 ' one tenth of circle at centre
Next t

'================================================================
'Albert's .0018 formula for a 200 degree pi
dim as double sarc,carc,sgrad2,cgrad2,inc,mul,array(50)
dim as double steps = 1
dim as double deg = atn(1)/100
array(0) = 0
array(1) = .00181483069342619
array(2) = .001812809472848158
array(3) = .001810836982114657
array(4) = .0018089127416807663671
array(5) = .001807036286436822
array(6) = .001805207165387777
array(7) = .001803424941344635
array(8) = .001801689190627545
array(9) = .001799999502780157
array(10)= .001798355480294862 '3
array(11)= .00179675673834854
array(12)= .001795202904548505
array(13)= .00179369361868824
array(14)= .001792228532512706
array(15)= .00179080730949286
array(16)= .001789429624609087
array(17)= .001788095164143335
array(18)= .00178680362547963
array(19)= .001785554716912746
array(20)= .001784348157464822
array(21)= .00178318367670963
array(22)= .0017820610146043725
array(23)= .001780979921328732
array(24)= .00177994015713099275
array(25)= .0017789414921811045
array(26)= .001777983706430438
array(27)= .001777066589478105
array(28)= .00177618994044369
array(29)= .001775353567846257
array(30)= .0017745572894894167
array(31)= .0017738009323524395
array(32)= .001773084332487164
array(33)= .001772407334920695
array(34)= .00177176979356369825
array(35)= .00177117157112423
array(36)= .00177061253902698
array(37)= .001770092577337868
array(38)= .00176961157469387
array(39)= .00176916942823804
array(40)= .001768766043559595
array(41)= .00176840133463906
array(42)= .001768075223798385
array(43)= .00176778764165597
array(44)= .00176753852708653
array(45)= .001767327827185835
array(46)= .001767155497240195
array(47)= .001767021500700705
array(48)= .00176692580916219
array(49)= .00176686840234694
array(50)= .0017668
for  grad = 0 To 100 Step steps

    if grad <= 50 then mul = array(grad) else mul = array(100-grad)
    inc = grad * 2

    sgrad2 = inc
    cgrad2 = 200 - inc
    
    sarc = deg * inc
    carc = deg * (200-inc)
    
    s = sarc * ((1- (sgrad2 * mul ) ) * .01)
    c = carc * ((1- (cgrad2 * mul ) ) * .01)
    
    sqr_root=sqr(c*c+s*s)
    s=s/sqr_root
    c=c/sqr_root
'----------------------------------------
    r = 1 + mag * (Sqr(c*c + s*s) - 1)   ' magnify radius error
    x = c * r
    y = s * r
    Pset (+x, -y), 10
    Pset (+x, +y), 10
    Pset (-x, -y), 10
    Pset (-x, +y), 10
    'line(0,0)-(+x,-y),14
    'line(0,0)-(+x, y),14
    'line(0,0)-(-x,-y),14
    'line(0,0)-(-x, y),14
    'print atan2(s,c)*100/pion2'(deg*200)
    t = grad * Pion2 / 100
    dx = c - Cos(t)
    dy = s - Sin(t)
    Circle(mag * dx, mag * dy), 0.01, 10   ' plot the scatter
Next grad

'================================================================
Sleep
'================================================================ 

Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

Albert has a table of 50 values that he folds to make 100 good points.
He should have asked himself what happens to values that are not in the table.
Spot the difference.

Code: Select all

'================================================================
' Albert's .0018 in green
'  approximations measured relative to the reference circle ... blue
Const As Double mag = 10000  ' magnification of errors
'================================================================
Dim As Double t, tt, grad, q, s, c, dx, dy, x, y, r = 1.1
Dim As Double sqr_root

Screen 19
Dim As Integer w, h, depth
Screeninfo w, h, depth
Window (-r * w / h, -r) - (r * w / h, r)  ' maintain pixel aspect

'----------------------------------------------------------------
Line(-c, 0)-(c, 0), 1   ' x axis
Line(0, -c)-(0, c), 1   ' y axis
Const As Double Pion2 = 2 * Atn(1)
For t = 0 To 4 * Pion2 Step Pion2 / 1000
    x = Cos(t)
     y = Sin(t)
    Pset(x, y), 1       ' reference circle
    Pset(x*.1, y*.1), 1 ' one tenth of circle at centre
Next t

'================================================================
'Albert's .0018 formula for a 200 degree pi
Dim As Double sarc,carc,sgrad2,cgrad2,inc,mul,array(50)
Dim As Double steps = 1
Dim As Double deg = Atn(1)/100
array(0) = 0
array(1) = .00181483069342619
array(2) = .001812809472848158
array(3) = .001810836982114657
array(4) = .0018089127416807663671
array(5) = .001807036286436822
array(6) = .001805207165387777
array(7) = .001803424941344635
array(8) = .001801689190627545
array(9) = .001799999502780157
array(10)= .001798355480294862 '3
array(11)= .00179675673834854
array(12)= .001795202904548505
array(13)= .00179369361868824
array(14)= .001792228532512706
array(15)= .00179080730949286
array(16)= .001789429624609087
array(17)= .001788095164143335
array(18)= .00178680362547963
array(19)= .001785554716912746
array(20)= .001784348157464822
array(21)= .00178318367670963
array(22)= .0017820610146043725
array(23)= .001780979921328732
array(24)= .00177994015713099275
array(25)= .0017789414921811045
array(26)= .001777983706430438
array(27)= .001777066589478105
array(28)= .00177618994044369
array(29)= .001775353567846257
array(30)= .0017745572894894167
array(31)= .0017738009323524395
array(32)= .001773084332487164
array(33)= .001772407334920695
array(34)= .00177176979356369825
array(35)= .00177117157112423
array(36)= .00177061253902698
array(37)= .001770092577337868
array(38)= .00176961157469387
array(39)= .00176916942823804
array(40)= .001768766043559595
array(41)= .00176840133463906
array(42)= .001768075223798385
array(43)= .00176778764165597
array(44)= .00176753852708653
array(45)= .001767327827185835
array(46)= .001767155497240195
array(47)= .001767021500700705
array(48)= .00176692580916219
array(49)= .00176686840234694
array(50)= .0017668
For  grad = 0.5 To 100 Step steps

    If grad <= 50 Then mul = array(grad) Else mul = array(100-grad)
    inc = grad * 2

    sgrad2 = inc
    cgrad2 = 200 - inc
   
    sarc = deg * inc
    carc = deg * (200-inc)
   
    s = sarc * ((1- (sgrad2 * mul ) ) * .01)
    c = carc * ((1- (cgrad2 * mul ) ) * .01)
   
    sqr_root=Sqr(c*c+s*s)
    s=s/sqr_root
    c=c/sqr_root
'----------------------------------------
    r = 1 + mag * (Sqr(c*c + s*s) - 1)   ' magnify radius error
    x = c * r
    y = s * r
    Pset (+x, -y), 10
    Pset (+x, +y), 10
    Pset (-x, -y), 10
    Pset (-x, +y), 10
    'line(0,0)-(+x,-y),14
    'line(0,0)-(+x, y),14
    'line(0,0)-(-x,-y),14
    'line(0,0)-(-x, y),14
    'print atan2(s,c)*100/pion2'(deg*200)
    t = grad * Pion2 / 100
    dx = c - Cos(t)
    dy = s - Sin(t)
    Circle(mag * dx, mag * dy), 0.01, 10   ' plot the scatter
Next grad
Print "Magnification ="; mag

'================================================================
Sleep
'================================================================
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Post by albert »

Any sugetsions Richard?

The goal hasn't changed, I'm still trying to obsolete sine and cosines.

Your loopy version is accurat to 14 places my above one is accurat to 15 but it can't do inbetween degrees, the sine,cosine function are good to 100 trillion , thats 16 places.

Once we get to 16 places accuracy we can only guess after that.

Is there a way to modify your loopy version to get more places?
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

Circles on a Cartesian screen require that you work in both polar(r,theta) and rectangular(x,y) coordinate systems, and you must be able to translate between the two. That needs two functions each way, four functions total. Those functions are sine, cosine, square root and arctangent. Denial of their right to exist as the incredibly elegant and useful functions that they are is irrational.

Sine and cosine exist because they are needed to do a particular job in numerical computation. You can eliminate them simply by using a different branch of the family tree of mathematics. If you change to the Fourier frequency domain then a sine or cosine will simply becomes a pair of constants, one specifies period and the other amplitude. Likewise, any number n can simply be written as 1 by changing to a base n number system. Complexity is a function of your viewpoint. If you do not like the view then it is easier to look away or move than it is to change reality.

“Come the Revolution things will be different - Not better, just different.” If you must have a revolution then I want the outcome to be better.

If you want more than 15 digits you must go from double to quad or octal precision.

If you want a look up table for sine and cosine then the following technique gives maximum absolute errors less than 30*n^3. When n = 100 it gives maximum errors of 1 in 30 million. If you need more accuracy use a bigger table size n.

Code: Select all

'================================================================
' Sine and cosine by a Look Up Table
Const As Integer n = 70     ' size of the Look Up Table
' This method exploits the characteristic that the successive
'  derivatives of sin are cos, -sin, -cos, sin and on round and round 
'================================================================
' things used to setup and plot error functions on the screen
Const As Double Pion2 = 2 * Atn(1)
Dim As Double t, tt, grad, q, s, c, dx, dy, x, y, r = 1.1
Screen 19
Window (-0.1, -1.1) - (Pion2 + 0.1, 1.1)
Line(-1, 0)-(2, 0), 1       ' x axis
Line(0, 1)-(Pion2, 1), 1    ' top
Line(0, -1)-(Pion2, -1), 1  ' bottom
Line(0, -1)-(0, 1), 1       ' y axis
Line(Pion2, -1)-(Pion2,1),1 ' pi/2 axis
Const As Double mag = 30 * n^3  ' magnification of errors
Color  7 : Print Using " Magnification############,###  "; mag;
Color 13 : Print "      Sine error,  ";
Color 14 : Print "      Cosine error."
draw string (-.05, +1), "+1", 7
draw string (-.05,  0), "0", 7
draw string (-.05, -1), "-1", 7
draw string (PiOn2, 0), " Pi/2", 7

'================================================================
' things used by the table method that can be precomputed once
Const As Double pn = Pion2 / n
Const As Double np = n / Pion2
Const As Double ks = 1 - 1 / (9.72 * n * n) ' 9.72 is smoothing fudge factor
Dim As Integer i
Dim As Double LUT(0 To n)       ' look up table
For i = 0 To n
    Line (i*pn,-1)-(i*pn,1),1   ' centre of cells in of table
    LUT(i) = Sin(i * pn)        ' set up the table
Next i

'================================================================
Dim As Double a, b, d, dd, sy, cy
For t = 0 To pion2 Step Pion2 / (n * 1000)
    '----------------------------------------
    ' evaluate sine and cosine from the table
    i = Cint(t * np)    ' closest index to t
    a = LUT(i)          ' both sine and the cos first derivative
    b = LUT(n - i)      ' mirror value is cos and sine first derivative
    d = t - (i * pn)    ' offset from index
    dd = d * d * 0.5    ' used for curvature correction
    d = d * ks          ' smoothing factor joins adjacent cells, ks~0.9999
    s = a + b*d - a*dd  ' sine approx
    c = b - a*d - b*dd  ' cosine approx
    '----------------------------------------
    ' plot the error function
    sy = mag * (Sin(t)-s)
    cy = mag * (Cos(t)-c)
    Pset (t, sy), 13    ' magenta
    Pset (t, cy), 14    ' yellow
Next t

'================================================================
Sleep
'================================================================
Alternatively you can get absolute errors less than 1 part in one billion from this 10th order solution.

Code: Select all

'================================================================
' Richard's 10th Order (yellow)
'  approximations measured relative to the reference circle ... blue
Const As Double mag = 1e8   ' magnification of errors
'================================================================
Dim As Double t, tt, grad, q, s, c, dx, dy, x, y, r = 1.1
Screen 20
Dim As Integer w, h, depth
Screeninfo w, h, depth
Window (-r * w / h, -r) - (r * w / h, r)  ' maintain pixel aspect
Color 7
Print "Magnification ="; mag
'----------------------------------------------------------------
Line(-c, 0)-(c, 0), 1   ' x axis
Line(0, -c)-(0, c), 1   ' y axis
Const As Double Pion2 = 2 * Atn(1)
For t = 0 To 4 * Pion2 Step Pion2 / 1000
    x = Cos(t)
    y = Sin(t)
    Pset(x, y), 1       ' reference circle
    Pset(x*.1, y*.1), 1 ' one tenth of circle at centre
Next t

'================================================================
' Richard's 10th order even, with constant parameters, in yellow
'================================================================
Const As Double c0 = +1.0 ' Abs(error) < 5.e-10
Const As Double c1 = -0.4999999964081511
Const As Double c2 = +4.166664187001062e-2
Const As Double c3 = -1.388839736758714e-3
Const As Double c4 = +2.476084612500093e-5
Const As Double c5 = -2.60499108983714e-7

For t = 0 To Pion2 Step pion2 / 100
    '----------------------------------------
    tt = t * t
    c = tt * (tt * (tt * (tt * (tt * c5 + c4) + c3) + c2) + c1) + c0
    ' s = sqr(1 - c * c)
    s = Pion2 - t
    tt = s * s
    s = c0 + tt * (c1 + tt * (c2 + tt * (c3 + tt * (c4 + tt * c5))))
    '----------------------------------------
    r = 1 + mag * (Sqr(c*c + s*s) - 1)   ' magnify radius error
    x = c * r
    y = s * r
    Pset (+x, -y), 14
    Pset (+x, +y), 14
    Pset (-x, -y), 14
    Pset (-x, +y), 14
    dx = c - Cos(t)
    dy = s - Sin(t)
    Circle(mag * dx, mag * dy), 0.01, 14   ' plot the scatter
Next t

'================================================================
Sleep
'================================================================
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Post by albert »

I remember my old method!

Where i'm figuring 200 degrees in a triangle instead of 180,this makes 90 degrees into 100 degrees.

The opposite angle is 200-deg/2 or 180-deg/2

if you take 1 deg the opposite angle is 99.5 which gives a cosine of .5 degrees , the opposite of that angle is 99.75 deg which makes .25 deg angle on your sine at a scale of .5 degrees.

The sine is 1 deg minus the height of .25 degrees at a distance of .5 degrees
The cosine is 1 minus the height of .5 degees at a distance of .9975 degrees.


if you draw a line from 1 deg at a .25 deg angle and draw a line from 0 at a .5 deg angle the place where the two angled lines meet is the x,y coords for 1 deg.

Since degrees are perfectly scalable 1 deg at 1 is 1 deg, at .5 its half the height at .25 its a quarter the height.

The sine decrements by .25 deg each deg and the cosine decrements by .5 deg each deg.

I think i used the tan() to get the scale if i remember correct. (which would be cheating and only accurate to 16 places.)
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

As many viewers will have noticed, Albert has got himself thoroughly tangled in this thread. Reading his above post demonstrates confusion of degrees with grads and angle with length. This has arisen because he does not understand the fundamental language of mathematics or the principles of numerical computation.

Once Albert got it into his head that a radius vector could be forced to a length of one, (normalised), by using a square root and a couple of divisions he insisted that it be used in his solution. This is a disaster because not only are the square root and division the slowest arithmetic operators available but they both display inherent numerical noise, noise that is not attenuated by following operations. Albert then applied the normalisation twice thereby demonstrating clearly that he had no understanding of how or why that code block worked. That was the same as assigning 1 to r, twice. r = 1 : r = 1.

Albert now finds himself in the position of wanting to fix the angle precisely so he can then normalise the radius. Neither of these processes is actually required if the sine and cosine functions are used because they will immediately give the coordinates of a point that lies on the circle. Unfortunately, one logical way to set the angle is to use the tangent function. Theoretically that use of the tangent is OK, but as a power series it does not numerically converge in a well behaved way. The tangent function is a bastard to calculate to many places unless derived by first calculating the sine or cosine. That is because it uses the somewhat erratic and difficult to compute Bernoulli numbers in it's power series. 14 year old mathematics students know that tan(x) = sin(x) / cos(x) and that the tangent function is more knotty than either sine or cosine, which is what you would expect when you divide one infinite power series by another, that's where the Bernoulli numbers come from. Albert has seen Bernoulli's 300 year old first footsteps in this field, but Albert is so far behind that he thinks he is first.

Here is the slow and inefficient code that Albert expects to discover a faster, accurate replacement for.

Code: Select all

 For t = 0 To Pion2 Step Pion2 / 100
    ' set the exact phase angle
    x = 1
    y = Tan(t)
    ' normalize the radius
    r = Sqr(x*x + y*y)
    c = x / r
    s = y / r
Next t
It is clear that any attempt to “peg the degrees” as Albert puts it, has an equivalent complexity to solving the tangent function. So it has just transferred the problem to more boggy ground near the top of a cliff face, the danger is realised with the final computation of Tan(Pion2) = infinity. It could have been easily avoided by sticking with the sine and cosine functions as shown here.

Code: Select all

Const As Double Pion2 = 2 * Atn(1)
Dim As Double t, r, y, c, s

' setting angle, followed by normalization
For t = 0 To Pion2 Step Pion2 / 10
    y = Tan(t)  ' x = 1
    r = Sqr(1 + y*y)
    c = 1 / r
    s = y * c   ' avoids the second division
    Print t, c, s
Next t
Print

' using cosine and computing sine from cosine
For t = 0 To Pion2 Step Pion2 / 10
    c = Cos(t)
    s = Sqr(1 - c*c)    ' when the root is faster than s = Sin(t)
    Print t, c, s
Next t
It seems that Albert's ideas are on a mystery tour, he wants others to watch him chase an undefined and invisible wild goose. He is proud of his failures which is why he posts them on the forum. This behaviour is more like an entertaining clown or slap-stick comedian than a village bright, it is quite unlike the court jester who only plays the fool to air inconvenient truths. A stand-up comedian does not have to understand the economy to make jokes about it, likewise Albert does not need to understand mathematics, he only needs to misapply mathematics to draw the attention of others.

Albert does not take the advice he is given and does not answer the questions he is asked. If he understood the advice or answered the questions, the attention would be gone because the problem could be resolved. This explains why the goal posts keep jumping sideways, or to the other end of the field. When the problem becomes dangerously close to resolution Albert re-defines the goal to avoid a solution.

Without the focus and ability to trust in and to study mathematics, the prognosis can not be good. Unfortunately the focus needed to concentrate on the study of mathematics is countered by Albert's impulsiveness and lack of mental discipline. Any holistic approach to the solution of the mathematical problems that Albert has presented in this thread require a change in the way that Albert cerebrates before they will be resolved to his satisfaction.

So what can be learnt from Albert's unique approach.

A solid mathematical foundation is essential. It is like having a fence along the top of a cliff face to prevent naive children and the blind from walking over the edge. If you do not know where the pitfalls are, then you are in danger without fences. The young impulsively do risky things, they fall of cliffs after climbing the security fence and they drive cars into trees and lamp posts, many die, a few survive to get medals and be called heroes. Charging forwards in mathematics with no regard for reason or safety is just foolish. In mathematics you start inside a small tightly fenced region and then move the fence outwards only after you have proven that the area to be enclosed is quite safe. The heroes are those who prove and safely fence the largest areas. Dancing around outside the fence with your eyes shut is a recipe for disaster.

Computational noise is generated by floating point instructions as they are executed. For any particular floating point definition, the choice of algorithm and instruction order decides the final accumulated error. Computational time is important. Unless the programmer understands the approximate internal cycle count of every part of each expression in the program there can be no certainty of optimising any trade between speed and accuracy. Preferentially use constants rather than variables, + - * rather than divide, avoid square roots wherever possible and keep well clear of the ^ operator if you want speed. Where there is a faster way to solve a problem to the required accuracy, that faster way should be used. The required accuracy must be be explicitly specified.

A good start to function approximation is to define the function over a range of 0 to 1, or for some circular functions 0 to a rational multiple of Pi. When this is done many constants disappear and the equations simplify to their minimum computational complexity. Albert insists on writing equations with 100 “degrees” in a quadrant. This not only makes it difficult to follow the equations, but it also makes for slow code as the constant 100 repeatedly returns to increase the complexity. Addition or subtraction can lead to loss of accuracy in floating point computations. Avoid introducing things that often need to be divided by a constant or be offset by a constant before they are useful. Keep it simple unless you want to generate computational noise and appear to be stupid.

If a line looks straight when you put your ruler up against it then you can only say that the line is as bent as the ruler. You cannot say that it is a straight line unless you flip the ruler to reverse any rule curvature. Likewise, comparing good approximations of sine and cosine to the internal FB circular functions cannot tell you which is the more correct, FB or your implementation. The FB internal functions are very good approximations, correct to better than about 15 digits. If you are interested in measuring errors beyond about 13 digits you must get a better rule than the FB internal double precision functions.

When drawing to the screen you can use a ruler with half pixel bumps in it. The Nyquist–Shannon sampling theorem states that you must sample with a resolution at least twice that of the detail you are interested in. If you have 1000 pixels across the screen then you can only resolve your image to a resolution of 1 in 500 or 0.2% in position. The sampling theorem works backwards also. When drawing to the screen there is no gain in being more precise than half a pixel. For this reason graphics routines need only about 1 part in 2000 accuracy. This makes significant increases possible in graphics speed if you are prepared to find fast approximations. Most people will not notice when a circle is drawn on the screen with 60 straight lines. No ones eye will notice if you use 120 straight lines to make a circle. You cannot judge the accuracy of a circle by looking at it drawn on the screen because eyes are not calibrated. Either it looks like a circle or it does not.

Differential calculus describes the slope, the rate of change of slope, and the rate of change of the rate of change of slope, and on ad infinitum. The slope of any function is the derivative of that function. The slope of the sine function is the cosine function while the slope of the cosine function is the negative sine function. Because of this, no method of differences will simplify the approximation of sine and cosine, it will always lead back to the same problem, sine or cosine. This principle also applies to the exponential function, as Exp(x) is its own derivative.
If the derivative was a simple constant then the function would be a parabola. If the derivative was a parabola then the function would be a cubic function etc. None of these can ever exactly equal the infinite power series expansions that define the sine or cosine or exponential functions. Simple difference tables cannot therefore define sine, cosine or exponentials accurately. Each function has the characteristics of it's family. Understanding those family characteristics makes it possible to quickly implement the function. Not understanding the characteristics of the functions family will waste your effort.

Define your goal precisely. With a clear specification of what you are attempting to achieve you can tell when you have finished. It is also possible to sometimes prove that the goal is impossible to achieve. Without an aim or goal you might wander for ever in the wilderness, not knowing which way to walk and not recognising the target as you walk through it. A scientific experiment formally specifies an aim because without a known aim a rigorous discipline cannot be applied that guarantees reaching the targeted destination. Likewise mathematical software needs to be specified precisely. You are lost if you have not defined your goal.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

now i become speechless :-)
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

@Richard

Truely intriguing and informative.

-Vince

PS: I was wondering how long you were going to engage in this thread before popping a gasket! ;)
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Post by albert »

Richard your

For t = 0 To Pion2 Step Pion2 / 100
' set the exact phase angle
x = 1
y = Tan(t)
' normalize the radius
r = Sqr(x*x + y*y)
c = x / r
s = y / r
next t

code fails at a mag of one.

It draws a 90 degree arc to the left with the error level???????

Code: Select all

'================================================================
'  approximations measured relative to the reference circle ... blue
Const As Double mag = 1  ' magnification of errors
'================================================================
Dim As Double t, tt, grad, q, s, c, dx, dy, x, y, r = 1.1
Dim As Double sqrt

Screen 19
Dim As Integer w, h, depth
Screeninfo w, h, depth
Window (-r * w / h, -r) - (r * w / h, r)  ' maintain pixel aspect

'----------------------------------------------------------------
Line(-c, 0)-(c, 0), 1   ' x axis
Line(0, -c)-(0, c), 1   ' y axis
Const As Double Pion2 = 2 * Atn(1)
For t = 0 To 4 * Pion2 Step Pion2 / 1000
    x = Cos(t)
    y = Sin(t)
    Pset(x, y), 1       ' reference circle
    Pset(x*.1, y*.1), 1 ' one tenth of circle at centre
Next t

For grad = 0 To Pion2 Step Pion2 / 100
    ' set the exact phase angle
    x = 1
    y = Tan(grad)
    ' normalize the radius
    sqrt = Sqr(x*x + y*y)
    c = x / sqrt
    s = y / sqrt
'----------------------------------------
    r = 1 + mag * (Sqr(c*c + s*s) - 1)   ' magnify radius error
    x = c * r
    y = s * r
    Pset (+x, -y), 10
    Pset (+x, +y), 10
    Pset (-x, -y), 10
    Pset (-x, +y), 10
    'line(0,0)-(+x,-y),14
     line(0,0)-(+x, y),14
    'line(0,0)-(-x,-y),14
    'line(0,0)-(-x, y),14
    'print atan2(s,c)*100/(deg*200)
    t = grad * Pion2 / 100
    dx = c - Cos(t)
    dy = s - Sin(t)
    Circle(mag * dx, mag * dy), 0.01, 10   ' plot the scatter
Next grad

'================================================================
Sleep
'================================================================ 

What i'm trying to accomplish is something similar to other people who came up with the existing formulas, newton,taylor,mclauren because thier formulas work doesn't mean there isn't another way to arrive at the same answers.

if you take a square and draw a line through it from corner to corner top right to bottom left and draw a line at 23.5 degrees from bottom right, it crosses the 45 degree line at 2.92894 from the right and .707106 from the left.
(180 - 45 = 135 * .5 = 67.5 | 90 - 67.5 = 23.5)

There has to be another way to figure the intersection other than powering and dividing by the factorial. The formula just hasn't been invented yet. (I'm working on inventing it!)
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

@Albert. My code works perfectly. It was only when you introduced;
For grad = 0 to Pion2 step Pion2/100
that you confused yourself by mixing grads with Radians.
I have fixed your code here.

Code: Select all

'================================================================
'  approximations measured relative to the reference circle ... blue
Const As Double mag = 1  ' magnification of errors
'================================================================
Dim As Double t, tt, grad, q, s, c, dx, dy, x, y, r = 1.1
Dim As Double sqrt

Screen 19
Dim As Integer w, h, depth
Screeninfo w, h, depth
Window (-r * w / h, -r) - (r * w / h, r)  ' maintain pixel aspect

'----------------------------------------------------------------
Line(-c, 0)-(c, 0), 1   ' x axis
Line(0, -c)-(0, c), 1   ' y axis
Const As Double Pion2 = 2 * Atn(1)
For t = 0 To 4 * Pion2 Step Pion2 / 1000
    x = Cos(t)
    y = Sin(t)
    Pset(x, y), 1       ' reference circle
    Pset(x*.1, y*.1), 1 ' one tenth of circle at centre
Next t

For t = 0 To Pion2 Step Pion2 / 100
    ' set the exact phase angle
    x = 1
    y = Tan(t)
    ' normalize the radius
    sqrt = Sqr(x*x + y*y)
    c = x / sqrt
    s = y / sqrt
'----------------------------------------
    r = 1 + mag * (Sqr(c*c + s*s) - 1)   ' magnify radius error
    x = c * r
    y = s * r
    Pset (+x, -y), 10
    Pset (+x, +y), 10
    Pset (-x, -y), 10
    Pset (-x, +y), 10
    dx = c - Cos(t)
    dy = s - Sin(t)
    Circle(mag * dx, mag * dy), 0.01, 10   ' plot the scatter
Next t

'================================================================
Sleep
'================================================================
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Post by albert »

Thanks again, Richard its good to 10 trillion.
however the print atan2(s,c)*100/pion2
gives me some 7's in the last column and some 83's in some cases.

And if you step pion2 / 1 it goes to -99.999999 degrees.

===============================================
Richard is there a way to use the tan() to get the intersection of the vectors?

I need to get more than 16 place accuracy and if I colud use a 16 place function as a pointer to another opperation i could get more than 16 place accuracy.

Could i use a tan() function to point to a distance on a grid and then use sqr() or normal math to find the intersection?


==============================================
I could use your loopy version in my calculator, but the k=k / sqr(1+aa*aa) in the setup loop would take 55 minutes (it takes about 1 min. to extract a 1000 place sqr root.
after that its just adding and subtracting the atn() table to determine wether or not to add or subtract from the sine and cosine.
That loop would take about 1-2 minutes in my calculator to resolve the sin and cos. which is somewhat bearable.
Is there another way to set the k variable to the desired value?
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

I think k=k / sqr(1+aa*aa) would be the least of your problems. If you want 100 digits from CORDIC, then you will need 335 values of arctan() for the table, each of which needs to be accurate to at least 100 digits. Where do you think they will come from?
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Post by albert »

I tried to get the tan() circle to do 90 degrees and it wouldn't close at exactly pion2/90 so i had to add to it a little.

It makes a nice looking circle, better than 100 degrees but there is still a gap on each side of 45 degrees around 35 and 55 or so.

Code: Select all

'================================================================
'  approximations measured relative to the reference circle ... blue
Const As Double mag = 10000000000000  ' magnification of errors
'================================================================
Dim As Double t, tt, grad, q, s, c, dx, dy, x, y, r = 1.1
Dim As Double sqrt

Screen 19
Dim As Integer w, h, depth
Screeninfo w, h, depth
Window (-r * w / h, -r) - (r * w / h, r)  ' maintain pixel aspect

'----------------------------------------------------------------
Line(-c, 0)-(c, 0), 1   ' x axis
Line(0, -c)-(0, c), 1   ' y axis
Const As Double Pion2 = 2 * Atn(1)
For t = 0 To 4 * Pion2 Step Pion2 / 1000
    x = Cos(t)
    y = Sin(t)
    Pset(x, y), 1       ' reference circle
    Pset(x*.1, y*.1), 1 ' one tenth of circle at centre
Next t

'============================================================
'============================================================
For grad = 0 To Pion2 Step Pion2 / 90.0000000000001493   ' wouldn't close at 90????????
    ' set the exact phase angle
    x = 1
    y = Tan(grad)
    ' normalize the radius
    sqrt = Sqr(x*x + y*y)
    c = x / sqrt
    s = y / sqrt
'----------------------------------------
    r = 1 + mag * (Sqr(c*c + s*s) - 1)   ' magnify radius error
    x = c * r
    y = s * r
    Pset (+x, -y), 10
    Pset (+x, +y), 10
    Pset (-x, -y), 10
    Pset (-x, +y), 10
    line(0,0)-(+x, +y), 10
    'line(0,0)-(+x, -y), 10
    'line(0,0)-(-x, +y), 10
    'line(0,0)-(-x, -y), 10
    'print atan2(s,c)*90/pion2
    'print s,c
    dx = c - Cos(grad)
    dy = s - Sin(grad)
    Circle(mag * dx, mag * dy), 0.01, 10   ' plot the scatter
Next grad
'================================================================
Sleep
'================================================================

albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Post by albert »

Richard the normalization doesn't work too good, I tried the sine and cosine to 10 digits and then tried to normalize them with the;
s,c = s,c / sqr( c*c + s*s )

And the degrees are off quite a bit.
I was only able to get 10 million on the magnification level.

At 18 digits its the full sine and cosine.(good to 10 trillion)
The most visually appealing circle is at 15 or 16 places (no gaps)
I think i'll use 15 or 16 place precision in my calculator and normalize it from there.

Code: Select all

'================================================================
'  approximations measured relative to the reference circle ... blue
Const As Double mag = 10000000  ' magnification of errors
'================================================================
Dim As Double t, tt, grad, q, s, c, dx, dy, x, y, r = 1.1
Dim As Double sqrt

Screen 19
Dim As Integer w, h, depth
Screeninfo w, h, depth
Window (-r * w / h, -r) - (r * w / h, r)  ' maintain pixel aspect

'----------------------------------------------------------------
Line(-c, 0)-(c, 0), 1   ' x axis
Line(0, -c)-(0, c), 1   ' y axis
Const As Double Pion2 = 2 * Atn(1)
For t = 0 To 4 * Pion2 Step Pion2 / 1000
    x = Cos(t)
    y = Sin(t)
    Pset(x, y), 1       ' reference circle
    Pset(x*.1, y*.1), 1 ' one tenth of circle at centre
Next t

'============================================================
'============================================================
dim as string sine,cosine
For t = 0 To 90 step 5
    
    c = cos(t*(pion2/90))
    s = sin(t*(pion2/90))
    
    '------s and c go negative at 0 and 90
    if c<0 then c=0
    if s<0 then s=0
    
    '----15 here gives the most visually appealing circle with the lines.
    cosine = left(str(c),10)
    sine =   left(str(s),10)
    
    c=val(cosine)
    s=val(sine)

    sqrt=sqr(c*c+s*s)
    c=c/sqrt
    s=s/sqrt
    
'----------------------------------------
    r = 1 + mag * (Sqr(c*c + s*s) - 1)   ' magnify radius error
    x = c * r
    y = s * r
    Pset (+x, -y), 10
    Pset (+x, +y), 10
    Pset (-x, -y), 10
    Pset (-x, +y), 10
    line(0,0)-(+x, +y), 10
    'line(0,0)-(+x, -y), 10
    'line(0,0)-(-x, +y), 10
    'line(0,0)-(-x, -y), 10
    print atan2(s,c)*90/pion2
    'print s,c
    dx = c - Cos(t*(pion2/90))
    dy = s - Sin(t*(pion2/90))
    Circle(mag * dx, mag * dy), 0.01, 10   ' plot the scatter
Next t
'================================================================
Sleep
'================================================================

Last edited by albert on Dec 06, 2009 23:13, edited 1 time in total.
bfuller
Posts: 362
Joined: Jun 02, 2007 12:35
Location: Sydney, Australia

Post by bfuller »

This has to be one of the most entertaining threads on the forum.

@ richard.
Your dissertation on Dec 05, 2009 15:39 is truly wonderful----brilliant even. Reminds me of the Cheshire Cat in Alice in Wonderland quote "If you don't know where you are going, any road will take you there".

In your post of Nov 29, 2009 14:27, where you take the intermediate angles, the errors are (dare I say it) sinusiodal in form and draw a nice flower shape. Surprise, surprise.

@Joshy and Vince------------I am amazed that you have been able to keep out of this for so long---------incredible restraint LOL.

@Albert, keep at it mate, a day is never wasted if you learn something.

(and if you keep messing with Tan, please remember that as the angle approaches a right angle (90 or 100 or PI/2 or whatever you want to call it) then Tan approaches infinity as sin divided by cos is the same as 1 divided by zero and no computer or any mathematician will ever give you a good answer to a divide by zero situation)

Best Regards
Locked