Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Math Function Plot
#1
Working (playing) with the "PSET hat" program, I wanted to see some math functions.  So another QB64 program was needed.

I know there are online function plotting and other QBPE programs, but I like simplicity, and I like my own.  So here is what I use for plotting functions.  Since you have to compile it to put in your functions, I put in comments to document the use of it.  I hope it is clear.  This version has a variety of functions for demonstration.  I reserve the right to edit this to later versions.
Code: (Select All)
_Title "FunctionPlot" ' V1.0 dcromley 2022
Option _Explicit
DefSng A-Z: DefInt I-N: DefStr S
Screen _NewImage(1000, 750, 256)
Cls 0, 15: Color 0, 15
Dim Shared ulo, uhi, vlo, vhi, xlo, xhi, ylo, yhi
Dim Shared u, v, x, y, c, nfunc, xold, yold
ulo = 10: uhi = 989: vlo = 739: vhi = 10 ' screen limits

xyinit -8, 8, 1, -6, 6, 1 ' xlo xhi xdel; ylo yhi ydel =#= world limits
' the above line means x from -8 to +8 and grid lines every 1; 0 would be no grid lines

For nfunc = 1 To 9
  c = 0 ' default color
  For u = ulo To uhi: x = zxu(u) ' x
    If nfunc = 1 Then y = 1 - x ^ 2 / 2
    If nfunc = 2 Then y = Cos(x)
    If nfunc = 3 Then c = 2: y = Sin(x)
    If nfunc = 4 Then GoTo continue1 ' skip
    If nfunc = 5 Then y = func5(x)
    If nfunc = 6 Then y = func6(x, 1)
    If nfunc = 7 Then GoTo continue1
    If nfunc = 8 Then GoTo continue1
    If nfunc = 9 Then GoTo continue1
    If u > ulo Then zline xold, yold, x, y, c ' the plot lines
    xold = x: yold = y
    continue1:
  Next u
Next nfunc
zSystem ' end

Function func5 (x) '
  If x < 0 Then func5 = 0: Exit Function
  c = 1 ' blue
  Dim t: t = Sqr(x)
  func5 = Sqr(x) ' Sin(t) + .4 * Sin(3 * x)
End Function

Function func6 (x, N) ' 2 options in this function
  c = 4 ' red
  If N = 1 Then func6 = 8 * (1 / 2 * Cos(x) - 1 / 7 * Cos(3 * x) + 1 / 8 * Cos(5 * x))
  If N = 2 Then func6 = 8 * (.5 * Cos(x) - .125 * Cos(3 * x) + .07 * Cos(5 * x))
End Function

Sub xyinit (xxlo, xxhi, xdel, yylo, yyhi, ydel)
  xlo = xxlo: xhi = xxhi: ylo = yylo: yhi = yyhi ' save to globals
  If xdel > 0 Then ' vertial grid lines
    zline 0, ylo, 0, yhi, c ' y axis
    For x = xdel To zMax(Abs(xlo), Abs(xhi)) Step xdel ' other
      If zBetween(x, xlo, xhi) Then zline x, ylo, x, yhi, 7
      If zBetween(-x, xlo, xhi) Then zline -x, ylo, -x, yhi, 7
    Next x
  End If
  If ydel > 0 Then ' horizontal grid lines
    zline xlo, 0, xhi, 0, c ' x axis
    For y = ydel To zMax(Abs(ylo), Abs(yhi)) Step ydel ' other
      If zBetween(y, ylo, yhi) Then zline xlo, y, xhi, y, 7
      If zBetween(-y, ylo, yhi) Then zline xlo, -y, xhi, -y, 7
    Next y
  End If
End Sub

Sub zline (x1, y1, x2, y2, c)
  Line (zux(x1), zvy(y1))-(zux(x2), zvy(y2)), c
End Sub

Function zux (x)
  zux = zLerplh(ulo, uhi, x, xlo, xhi)
End Function

Function zvy (y)
  zvy = zLerplh(vlo, vhi, y, ylo, yhi)
End Function

Function zxu (u)
  zxu = zLerplh(xlo, xhi, u, ulo, uhi)
End Function

Function zyv (y)
  zyv = zLerplh(ylo, yhi, v, vlo, vhi)
End Function

Function zLerplh (ylo, yhi, x, xlo, xhi)
  zLerplh = ylo + (x - xlo) / (xhi - xlo) * (yhi - ylo)
End Function

Function zBetween (x, a, b)
  If x >= a And x <= b Then zBetween = 1
End Function

Function zMax (a, b)
  If a > b Then zMax = a Else zMax = b
End Function

Function zMin (a, b)
  If a < b Then zMin = a Else zMin = b
End Function

Sub zSystem
  While InKey$ = "": _Limit 60: Wend
  System
End Sub
Reply


Messages In This Thread
Math Function Plot - by dcromley - 04-28-2022, 03:46 PM



Users browsing this thread: 1 Guest(s)