Lineair and Exponential curves

Everything else that doesn't fall into one of the other PB categories.
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Lineair and Exponential curves

Post 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
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

Exponential function: f(x)=a*b^x
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Post 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...
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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


eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Post by eriansa »

@Trond : in this case X will never reach EndValue (E)
I wish it was that simple... :?
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post 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 :)
Mat
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Post by eriansa »

That's it! MisterMath. :P
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

MrMat, nice piece of code. :wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

eriansa wrote:MisterMath.
:D

But you're right - or MisterMathMaster!
@}--`--,-- A rose by any other name ..
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

hehe after 7 years of maths at uni that's some good names :lol:
Mat
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Post 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?
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post 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.
Mat
Post Reply