Page 1 of 1

Lineair and Exponential curves

Posted: Thu May 04, 2006 8:57 pm
by eriansa
First : I am an math-idiot!

Ok :
I have a beginvalue (B) and an endvalue (E). I want a variable (X) to go from beginvalue to endvalue in a variable number of steps (S).

Lineair :
StepValue= (E-B) / S
X=0
For i = 1 to S
X+StepValue
Next

Simple!

But : I also need ohter curves. Exponential UP and Exponential DOWN. AND I want to control the slope (steepness)

I know that instead of adding the StepValue I need to multiplcate/divide. I also know it has something to do with Pow(y,(1/z))...and now I am completely stuck.

I did search google, visited all kinds of Math sides, still don't understand how to do it.

For someone with a mathematical brain it should be quite simple.

Please help me!


Image

Posted: Thu May 04, 2006 9:07 pm
by Bonne_den_kule
Exponential function: f(x)=a*b^x

Posted: Thu May 04, 2006 9:21 pm
by eriansa
That I know, but I want to implement it in this form :

X=0
For i = 1 to S
X*StepValue
Next

OR

For i = 1 to S
X+(B*StepValue)
Next


And also I want to control the steepness of the curve...

Posted: Thu May 04, 2006 9:29 pm
by Trond

Code: Select all


OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
Img = CreateImage(-1, 512, 384)



StartDrawing(ImageOutput(Img))
  X.d = 1
  Y.d = 1
  For I = 1 To 512
    X * 1.03
    Y + 1
    Debug X
    Plot(X, Y, #Red)
  Next

StopDrawing()

ImageGadget(0, 0, 0, 512, 384, ImageID(Img))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver



Posted: Thu May 04, 2006 10:19 pm
by eriansa
@Trond : in this case X will never reach EndValue (E)
I wish it was that simple... :?

Posted: Thu May 04, 2006 10:45 pm
by MrMat
Have a play with this:

Code: Select all

#Image = 0
#Gadget_TrackBarLeft = 0
#Gadget_TrackBarRight = 1
#Gadget_TrackBarBottom = 2
#Gadget_Image = 3

#exp = 2.71828183

Procedure UpdateGraph(imageno.l, type.l, y1.d, y2.d, b.d)
    width.l = ImageWidth(imageno)
    height.l = ImageHeight(imageno)
    StartDrawing(ImageOutput(imageno))
    Box(0, 0, width, height)
    col.l = RGB(0, 255, 0)
    If b = 0
        type = 0
    EndIf
    y.d = y1
    prevy.d = y
    Select type
        Case 0
            stepy.d = (y2 - y1) / width
            For x = 1 To width - 1
                y + stepy
                LineXY(x - 1, prevy, x, y, col)
                prevy = y
            Next
        Case 1
            stepy.d = (y2 - y1) / width
            a.d = (y2 - y1) / (Pow(#exp, b * width) - 1)
            d.d = y1 - a
            mult.d = Pow(#exp, b)
            For x = 1 To width - 1
                y = (prevy - d) * mult + d
                LineXY(x - 1, prevy, x, y, col)
                prevy = y
            Next
    EndSelect
    StopDrawing()
EndProcedure

Procedure UpdateImage()
    height = ImageHeight(#Image)
    UpdateGraph(#Image, 1, height - GetGadgetState(#Gadget_TrackBarLeft), height - GetGadgetState(#Gadget_TrackBarRight), (GetGadgetState(#Gadget_TrackBarBottom) - 50) / 1000)
    SetGadgetState(#Gadget_Image, ImageID(#Image))
EndProcedure

imwidth.l = 400
imheight.l = 240
CreateImage(#Image, imwidth, imheight)

OpenWindow(0, 0, 0, imwidth + 56, imheight + 44, "Graph test", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

CreateGadgetList(WindowID(0))
TrackBarGadget(#Gadget_TrackBarLeft, 4, 4, 20, imheight + 16, 0, imheight, #PB_TrackBar_Vertical) 
TrackBarGadget(#Gadget_TrackBarRight, imwidth + 32, 4, 20, imheight + 16, 0, imheight, #PB_TrackBar_Vertical) 
TrackBarGadget(#Gadget_TrackBarBottom, 4, imheight + 16, imwidth + 48, 20, 0, 100)

SetGadgetState(#Gadget_TrackBarLeft, 0)
SetGadgetState(#Gadget_TrackBarRight, imheight)
SetGadgetState(#Gadget_TrackBarBottom, 50)

ImageGadget(#Gadget_Image, 28, 12, imwidth, imheight, ImageID(#Image))

UpdateImage()
Repeat
    event = WaitWindowEvent()
    eventgadget = EventGadget()
    
    Select event
        Case #PB_Event_Gadget
            If eventgadget = #Gadget_TrackBarLeft Or eventgadget = #Gadget_TrackBarRight Or eventgadget = #Gadget_TrackBarBottom
                UpdateImage()
            EndIf
    EndSelect
Until event = #PB_Event_CloseWindow
I hope it's what you want :)

Posted: Thu May 04, 2006 11:23 pm
by eriansa
That's it! MisterMath. :P

Posted: Fri May 05, 2006 4:22 pm
by Psychophanta
MrMat, nice piece of code. :wink:

Posted: Fri May 05, 2006 4:27 pm
by Dare2
eriansa wrote:MisterMath.
:D

But you're right - or MisterMathMaster!

Posted: Fri May 05, 2006 4:30 pm
by MrMat
hehe after 7 years of maths at uni that's some good names :lol:

Posted: Fri May 05, 2006 10:27 pm
by eriansa
This #exp = 2.71828183 : isn't that the natural log?
(I remember something from school)


Care to comment on how this number is calculated or is this way to deep into the math-mysterious-caves?

Posted: Fri May 05, 2006 11:04 pm
by Psychophanta
it is called 'e' number.
You can get it like that for example:

Code: Select all

Debug Pow(2,1/Log(2))
However, in the MrMat code you could replace it by any other positive number, not too big. The curve would be a little different.

Posted: Sat May 06, 2006 12:24 am
by MrMat
Yes it is e, the base of natural logarithms. Wikipedia has some good info:
http://en.wikipedia.org/wiki/Exponential_function
Different values can be used in the code above instead of #exp but the curves that will be produced can be generated using #exp with a scaled parameter b so it's not gaining anything.