Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Sine Approximation
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Catalyst




PostPosted: Sun Jan 04, 2004 2:19 am   Post subject: Sine Approximation

code:

var factorialHold : int := 0
const maxPres := 100
var ZeroSineDx : array 0 .. maxPres of real
for i : 0 .. maxPres - 4 by 4
    ZeroSineDx (i) := 0
    ZeroSineDx (i + 1) := 1
    ZeroSineDx (i + 2) := 0
    ZeroSineDx (i + 3) := -1
end for
var SineHold, SineN : real := 0
var sinePres : int := 9

function sinE (n : real) : real
    result sin(n)
end sinE

function Factorial (n : int) : int
    if (n = 1) or (n = 0) then
        result 1
    else
        factorialHold := 1
        for i : 1 .. n
            factorialHold *= i
        end for
        result factorialHold
    end if
end Factorial

function Sine (n : real) : real
    SineHold := 0
    if (n = 0) then
        result 0
    end if
    for i : 0 .. sinePres
        SineHold += (ZeroSineDx (i) * (n ** i)) / (Factorial (i))
    end for
    result SineHold
end Sine



View.Set ("nobuttonbar,graphics:512;512,position:300;300,offscreenonly")
for k : 1 .. 12 by 2
    drawfillbox (0, 0, maxx, maxy, 7)
    sinePres := k
    drawline (maxx div 2, 0, maxx div 2, maxy, 10)
    drawline (0, maxy div 2, maxx, maxy div 2, 10)
    for i : -maxx .. maxx
        drawdot (i + maxx div 2, round (sinE (i / maxx * 10) * 50) + maxx div 2, 103)
        drawdot (i + maxx div 2, round (Sine (i / maxx * 10) * 50) + maxx div 2, 42)
    end for
    View.Update
    delay (200)
end for
Sponsor
Sponsor
Sponsor
sponsor
Homer_simpson




PostPosted: Sun Jan 04, 2004 4:12 pm   Post subject: (No subject)

nice code...
+50
gigaman




PostPosted: Mon Oct 25, 2004 3:20 pm   Post subject: (No subject)

nice pretty cool program but what does it show?
Delos




PostPosted: Mon Oct 25, 2004 4:39 pm   Post subject: (No subject)

As the title infers...Catalyst has used some crazy maths there (factorials, possibly some level of calculus as well) so as to approximate the value of sine on a graph, starting with the function y = x.

Of course I can't tell you exactly how this works, or the applications, but I can tell you that if you exchange the assignemnts at the start of the programme thusly:
code:

    ZeroSineDx (i + 1) := -1
    ZeroSineDx (i + 2) := 0
    ZeroSineDx (i + 3) := 1

You get cosine. This is interesting as the graph you start off with is y = -x.
Catalyst




PostPosted: Mon Oct 25, 2004 9:19 pm   Post subject: (No subject)

http://mathworld.wolfram.com/MaclaurinSeries.html
Very Happy
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: