Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Help converting this math formula into code, please.

  1. #1
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Help converting this math formula into code, please.

    I have no idea how to take a math formula like this and make it workable code. I sure can use some help.

    I am interested in:
    Two-dimensional Gaussian function. There is a screenshot that shows the shape.

    http://en.wikipedia.org/wiki/Gaussian_function

    Thanks for any help!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  2. #2

  3. #3
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    Thanks Mike, but that doesn't help me understand how to figure out such conversions. The wikipedia page also had some code written in a language named Octave, but I couldn't match how they came up with that code looking at the mathematic formula.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  4. #4
    Hi Kent,

    I would deconstruct the first formula in the Wiki then apply it to x and z dimensions to get the y.

    'Gaussian hump
    
    dim as double a,b,c,e,g,hx,x,y,z
    
    e=2.718281828 'Euler's number e
    
    'In a single dimension x
    
    assuming centre b is 0
    
    g=-0.5*(x*x)/(c*c)
    hx=a*e^g
    
    'in x and z dimensions (using pythagoras to get distance from centre)
    
    g=-0.5*(x*x+z*z)/(c*c)
    y=a*e^g
    
    You can then iterate on x and z to get the corresponding y.

    Charles

    PS correction to my previous by applying pythagoras.
    Last edited by Charles Pegge; 02-12-2010 at 20:18.

  5. #5
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    Thanks Charles will try it out now and study. I will post when I get it to do what I want, thanks again!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  6. #6
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152
    Here is a thinBasic script which "plots" (numerically) to the console, the two dimensional Gaussian Function, i.e., the equation at the top of the following web page, ==>

    http://en.wikipedia.org/wiki/Gaussian_function

    It makes the plot twice, for two values of the parameter, "c", which determines the sharpness of the curve's "peak".


    Dan

    '-----------------------------------------------------------------------------------------------
    ' Empty console script created on 12-02-2010 15:37:59 by  (ThinAIR)
    '-----------------------------------------------------------------------------------------------
    ' FILE = "Gauss2D.tbasic"
    '-----------------------------------------------------------------------------------------------
    
    Uses "console"
    
    ' "c" should not be set equal to 0.
    
    ' "a" is the "y" value when "x" equals "b".
    ' "a" is the maximum (or minimum for negative values of "a") "y" value.
    ' If "a" is negative, then the curve will be upside down (the "peak" will point down).
    
    ' "b" is the "x" value at the center of the symmetric curve.
    ' "b" is also the "x" value corresponding to the maximum "y" value.
    
    ' "c" determines the width of the peak.
    ' The curve will look exactly the same for "c" equals "z", or for "c" equals negative "z" (because the formula only uses "c" squared).
    ' For instance, if "c" equals 1, the curve will look narrower, than if "c" equals 10.
    
    '-----------------------------------------------------------------------------------------------
    
    Function TBMain()
    Local i As Ext
    Local x, y, w, a, b, c As Ext
    Local s As String
    
    '------------------------------------------------------------
    
    ' Let the "plot" extend from negative "w" to positive "w".
    ' Arbitrarily, set "w" equal to 10.
    w = 10
    
    ' Put the center of the curve at "x" equals 0.
    b = 0
    
    ' Make the maximum value of the curve equal to 1000.
    a = 1000
    
    ' First, try "c" equals 1.
    c = 1
    
    '------------------------------------------------------------
    
    For x = -w To w Step w / 50
    y = Gauss2D(x, a, b, c)
    s = Format$(x, "+00.000;-00.000") 
    Console_WriteLine(s, " ", y)
    Next
    
    '------------------------------------------------------------
    Console_WriteLine("")
    '------------------------------------------------------------
    
    ' Now, try "c" equals 10.
    c = 10
    
    For x = -w To w Step w / 50
    y = Gauss2D(x, a, b, c)
    s = Format$(x, "+00.000;-00.000") 
    Console_WriteLine(s, " ", y)
    Next
    '------------------------------------------------------------
    
    WaitKey      
    End Function
    
    '-----------------------------------------------------------------------------------------------
    
    Function Gauss2D(x As Ext, a As Ext , b As Ext, c As Ext) As Ext
    Return a * Exp(-((x - b)^2/2/c^2))
    End Function
    
    '-----------------------------------------------------------------------------------------------
    
    Attached Files Attached Files
    Last edited by danbaron; 03-12-2010 at 03:17.
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

  7. #7
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    Thanks Dan, came back to check the forums after banging my head against the wall and still not getting it. Now with this new Energy Code Bar you gave me, feel energized and will see if I can do what I wanted. Thanks again!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  8. #8
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    Thanks for the help Dan, Charles and Mike.

    I see now that this method will not help me meet my goal, but it is nice to have forums like this and a language like thinBasic to test out things like this quickly!

    '
    ' The most basic skeleton for TBGL
    ' started on 12-02-2010   
    ' Thanks Dan and Charles for code and help!
    
    Uses "TBGL" 
    
    Function TBMain()    
     
        Local i As Ext
        Local x, y, w, a, b, c As Ext
        'Local s As String
        
        '------------------------------------------------------------
        
        ' Let the "plot" extend from negative "w" to positive "w".
        ' Arbitrarily, set "w" equal to 10.
        w = 20
        
        ' Put the center of the curve at "x" equals 0.
        b = 0
        
        ' Make the maximum value of the curve equal to 1000.
        a = 15
        
        ' First, try "c" equals 1.
        c = 5
        
        '------------------------------------------------------------
    
      Local hWnd      As DWord
      Local FrameRate As Double
      
      ' -- Create and show window
      hWnd = TBGL_CreateWindowEx("Roswell Effect: Failed Attempt One - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX) 
      TBGL_ShowWindow 
    
      
        %Gauss2D = 1 
        TBGL_NewList %Gauss2D
         TBGL_PushMatrix
            'draw red lines first      
            TBGL_Color(255, 0, 0)
            TBGL_BeginPoly %GL_LINES 
                For x = -w To w Step w / 50
                    y = Gauss2D(x, a, b, c)
                    TBGL_Vertex x,y            
                Next
            TBGL_EndPoly 
            
            'draw yellow points
            TBGL_PointSize 1       
            TBGL_Color(255, 255, 0)
            TBGL_BeginPoly %GL_POINTS 
                For x = -w To w Step w / 50
                    y = Gauss2D(x, a, b, c)
                    TBGL_Vertex x,y            
                Next
            TBGL_EndPoly
         TBGL_PopMatrix  
        TBGL_EndList 
     
      ' -- Resets status of all keys 
      TBGL_ResetKeyState()
    
      ' -- Main loop
      While TBGL_IsWindow(hWnd) 
        FrameRate = TBGL_GetFrameRate
    
        TBGL_ClearFrame 
          TBGL_Camera(0, 30, 30, 0, 0, 0)
    
          For x = 1 To 15
            TBGL_Rotate 24, 0,1,0 
            TBGL_CallList %Gauss2D
          Next
    
        TBGL_DrawFrame 
    
        ' -- ESCAPE key to exit application
        If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While 
    
      Wend 
    
      TBGL_DestroyWindow
    End Function 
                         
                         
    '-----------------------------------------------------------------------------------------------
    
    Function Gauss2D(x As Ext, a As Ext , b As Ext, c As Ext) As Ext
    Return a * Exp(-((x - b)^2/2/c^2))
    End Function
    
    '-----------------------------------------------------------------------------------------------
    
    Attached Images Attached Images
    Attached Files Attached Files
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  9. #9
    I dropped the Gauss form into the Oxygen OpengGL 4 port demo:
    You can try other functions by editing the procedure below:

    Charles

    '====================================================================
    ' DrawShape() - Draw a solid shape (use a display list for the model)
    '====================================================================
    
    sub DrawShape
    
      static as single e,g,x,y,z,nx,ny,nz,g
      static as long shape_list, i,j,k
      
      
      if not shape_list
        '
        'Record the plot list
        '--------------------
        '
        shape_list = glGenLists 1
        glNewList( shape_list, GL_COMPILE_AND_EXECUTE )
        '
        'Draw the shape
        '
        e=2.718281828 'Euler's number
        z=2.5
        for i = 1 to 20
          '
          x=-2.5
          glBegin GL_QUAD_STRIP
          '
          for j = 1 to 20
            '
            '
            'PLOT
            '----
            '
            g=-.5*(x*x+Z*Z)
            y=-1+2*pow(e,g)
            'y = 2.5-sqr(x*x+Z*Z)
            nx = x
            ny = y
            nz = z
            '
            glNormal3f nx, ny, nz
            glVertex3f x, y, z
            '
            z-=.25
            '
            g=-.5*(x*x+Z*Z)
            y=-1+2*pow(e,g)
            'y = 2.5-sqr(x*x+Z*Z)
            nx = x
            ny = y
            nz = z
            '
            glNormal3f nx, ny, nz
            glVertex3f x, y, z
            '
            z+=.25
            x+=.25
            '
          next
          '
          glEnd()
          z-=.25
          '
        next
        '
        glEndList()
        '
      else
        '  
        'Playback displaylist
        '
        glCallList( shape_list )
      end if
    end sub
    
    Attached Images Attached Images
    Attached Files Attached Files

  10. #10
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152
    Previously, I gave you f(x).

    If, you want f(x, y), then, there are two equations at the bottom of the web page ( http://en.wikipedia.org/wiki/Gaussian_function ) (the plot labeled, "Gaussian curve with a 2-dimensional domain").

    I'm guessing that you don't need the second equation. It is for rotating the elliptical plot in the x-y plane.

    Here is the function for the first equation (in which the major axis of the ellipse is parallel to one global axis (x or y), and the minor axis of the ellipse is parallel to the other global axis.)

    Function Gauss3D(x As Ext, y As Ext, a As Ext,  x0 As Ext, xs As Ext, y0 As Ext, ys As Ext) As Ext
    ' The "s" in xs means "sigma". That is the Greek character that appears  in one of the denominators of the equation's exponent.
    ' The lowercase Greek character sigma, looks like "o". 
    ' The "s" in ys means "sigma". That is the Greek character that appears  in the other denominator of the equation's exponent. 
    
    ' You would call this function like this.
    ' z = Gauss3D(x, y, a, x0, xs, y0, ys)
    
    ' (x, y) is the point in the x-y plane at which you want the value of  "z". 
    ' "a" is the maximum (or minimum for negative "a") value of the  function.
    ' (xo, yo) is the point at the function's center (at z = a = f(x0, y0, a, x0, xs, y0, ys)).
    
    ' The value of xs determines the spread of the function in the x  direction. 
    ' The value of ys determines the spread of the function in the y  direction.
    
    ' For a circular plot, set ys = xs. 
    
    
    Return a * Exp(-((x - x0)^2/2/xs^2 + (y - y0)^2/2/ys^2))  
    End Function
    
    

    The Octave code is for using the second equation. That is used if you want to rotate the plot in the x-y plane (like the three plots shown at the bottom of the web page). If you need that, then, say so. It is not very hard.

    The Octave code uses sigma_x = 1, and, sigma_y = 2. That means that before the plot is rotated, the ellipse appears like it extends twice as far in the y direction, as in the x direction. The Octave code rotates the ellipse by pi (180 degrees).

    In the Octave code, theta is the angle (the uppercase Greek character theta, looks like a zero with a horizontal line through the middle) of rotation in the x-y plane. When theta equals 0, the plot has not been rotated. When theta equals pi/2, the plot has been rotated 90 degrees. When theta equals pi, the plot has been rotated 180 degrees.

    (I guess I misunderstood what you meant by "two-dimensional", in your original post. A plot, f(x), is two dimensional in the sense that it is a plane curve, so it has two dimensions (unless it is a line). But, it is one dimensional in the sense that it has one independent variable, x. Likewise, a plot, f(x, y), is three dimensional in the sense that it is a curved surface, so it has three dimensions (unless it is a plane). But, it is two dimensional in the sense that it has two independent variables, x and y.)

    (The "failed" plot you made, looks nice, like Christmas lights.)

    Last edited by danbaron; 03-12-2010 at 08:48.
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

Page 1 of 3 123 LastLast

Similar Threads

  1. Simple formula to simulate complex real life situations
    By marcuslee in forum thinBasic General
    Replies: 13
    Last Post: 11-11-2010, 09:14
  2. checkerboard floor - formula???
    By Lionheart008 in forum TBGL General
    Replies: 3
    Last Post: 03-12-2008, 22:36
  3. 2D game math
    By kryton9 in forum Gaming
    Replies: 4
    Last Post: 29-04-2008, 21:59

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •