Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Polynomial Regression + Mouse Interpolation ' QB64-PE Example
#1
The following code was generated by chatgpt (also file is attached). I get an error concerning the line with this code:  Yval = PolyEval(a(), degree, Xval).
I can't seemed to fix this. Any help would be greatly appreciated.

Thanks

'===========================
' Polynomial Regression + Mouse Interpolation
' QB64-PE Example
'===========================

Option _Explicit

Const nPoints = 6 ' number of data points
Const degree = 2 ' degree of polynomial regression

Dim Shared x(nPoints) As Double, y(nPoints) As Double
Dim Shared a(degree) As Double ' polynomial coefficients
Dim i, Xscr As Single
Dim PolyEval(1000) As Double
Screen _NewImage(800, 600, 32)
Rem COLOR  _RGB32(255, 255, 255)
Cls

'--- Define sample data (x, y) ---
x(1) = 1: y(1) = 2
x(2) = 2: y(2) = 3
x(3) = 3: y(3) = 5
x(4) = 4: y(4) = 4
x(5) = 5: y(5) = 6
x(6) = 6: y(6) = 7

'--- Compute polynomial regression coefficients ---
Call PolyFit(x(), y(), nPoints, degree, a())

'--- Draw data points ---
For i = 1 To nPoints
    PSet (x(i) * 100, 600 - y(i) * 100), _RGB32(255, 0, 0)
    Circle (x(i) * 100, 600 - y(i) * 100), 4, _RGB32(255, 0, 0)
Next

'--- Draw polynomial curve ---
For Xscr = 0 To 800
    Dim Xval As Double
    Xval = Xscr / 100
    Dim Yval As Double
    Yval = PolyEval(a(), degree, Xval)
    LINE -(Xscr, 600 - Yval * 100), _RGB32(0, 255, 0)
NEXT

_PRINTSTRING (10, 10), "Click anywhere to interpolate. ESC to quit."

'--- Mouse Interpolation Loop ---
DO
    DIM mx AS LONG, my AS LONG, mb AS LONG
    mb = _MOUSEBUTTON(1)
    mx = _MOUSEX
    my = _MOUSEY

    IF mb THEN
        DIM xclick AS DOUBLE
        xclick = mx / 100
        DIM ycalc AS DOUBLE
        ycalc = PolyEval(a(), degree, xclick)

        LINE (mx, my)-(mx, 600 - ycalc * 100), _RGB32(0, 0, 255)
        CIRCLE (mx, 600 - ycalc * 100), 4, _RGB32(0, 0, 255)
        _PRINTSTRING (mx + 10, 600 - ycalc * 100), "Y=" + STR$(ycalc)
        _DELAY 0.3
    END IF

    IF _KEYDOWN(27) THEN END
    _LIMIT 30
LOOP

'======================
' Polynomial Functions
'======================

SUB PolyFit (x() AS DOUBLE, y() AS DOUBLE, n AS INTEGER, m AS INTEGER, a() AS DOUBLE)
    ' Compute coefficients for polynomial regression using normal equations
    DIM X(2 * m) AS DOUBLE
    DIM B(m) AS DOUBLE
    DIM A(m, m) AS DOUBLE

    ' Calculate sums of powers of x
    FOR i = 0 TO 2 * m
        X(i) = 0
        FOR j = 1 TO n
            X(i) = X(i) + x(j) ^ i
        NEXT
    NEXT

    ' Calculate elements of the matrix and RHS
    FOR i = 0 TO m
        FOR j = 0 TO m
            A(i, j) = X(i + j)
        NEXT
        B(i) = 0
        FOR j = 1 TO n
            B(i) = B(i) + y(j) * x(j) ^ i
        NEXT


Attached Files
.bas   plot and mouse Rev 2.bas (Size: 2.47 KB / Downloads: 28)
Reply
#2
The array cannot be resized because it is static. Try using a dynamic array instead. The difference between static and dynamic arrays has been discussed extensively in the forum; please use the search function.


Code: (Select All)

'===========================
' Polynomial Regression + Mouse Interpolation
' QB64-PE Example
'===========================

Option _Explicit

Const nPoints = 6 ' number of data points
Const degree = 2 ' degree of polynomial regression

Dim Shared x(nPoints) As Double, y(nPoints) As Double
Dim Shared a(degree) As Double ' polynomial coefficients
Dim i, Xscr As Single
Dim PolyEval(1000) As Double
Screen _NewImage(800, 600, 32)
Rem COLOR  _RGB32(255, 255, 255)
Cls

'--- Define sample data (x, y) ---
x(1) = 1: y(1) = 2
x(2) = 2: y(2) = 3
x(3) = 3: y(3) = 5
x(4) = 4: y(4) = 4
x(5) = 5: y(5) = 6
x(6) = 6: y(6) = 7

'--- Compute polynomial regression coefficients ---
Call PolyFit(x(), y(), nPoints, degree, a())

'--- Draw data points ---
For i = 1 To nPoints
  PSet (x(i) * 100, 600 - y(i) * 100), _RGB32(255, 0, 0)
  Circle (x(i) * 100, 600 - y(i) * 100), 4, _RGB32(255, 0, 0)
Next

'--- Draw polynomial curve ---
For Xscr = 0 To 800
  Dim Xval As Double
  Xval = Xscr / 100
  Dim Yval As Double
    Yval = PolyEval(a(), degree, Xval)
    LINE -(Xscr, 600 - Yval * 100), _RGB32(0, 255, 0)
NEXT

_PRINTSTRING (10, 10), "Click anywhere to interpolate. ESC to quit."

'--- Mouse Interpolation Loop ---
DO
    DIM mx AS LONG, my AS LONG, mb AS LONG
    mb = _MOUSEBUTTON(1)
    mx = _MOUSEX
    my = _MOUSEY

    IF mb THEN
        DIM xclick AS DOUBLE
        xclick = mx / 100
        DIM ycalc AS DOUBLE
        ycalc = PolyEval(a(), degree, xclick)

        LINE (mx, my)-(mx, 600 - ycalc * 100), _RGB32(0, 0, 255)
        CIRCLE (mx, 600 - ycalc * 100), 4, _RGB32(0, 0, 255)
        _PRINTSTRING (mx + 10, 600 - ycalc * 100), "Y=" + STR$(ycalc)
        _DELAY 0.3
    END IF

    IF _KEYDOWN(27) THEN END
    _LIMIT 30
LOOP

'======================
' Polynomial Functions
'======================

SUB PolyFit (x() AS DOUBLE, y() AS DOUBLE, n AS INTEGER, m AS INTEGER, a() AS DOUBLE)
    ' Compute coefficients for polynomial regression using normal equations
    DIM X(2 * m) AS DOUBLE
    DIM B(m) AS DOUBLE
    DIM A(m, m) AS DOUBLE

    ' Calculate sums of powers of x
    FOR i = 0 TO 2 * m
        X(i) = 0
        FOR j = 1 TO n
            X(i) = X(i) + x(j) ^ i
        NEXT
    NEXT

    ' Calculate elements of the matrix and RHS
    FOR i = 0 TO m
        FOR j = 0 TO m
            A(i, j) = X(i + j)
        NEXT
        B(i) = 0
        FOR j = 1 TO n
            B(i) = B(i) + y(j) * x(j) ^ i
        NEXT

[Image: Can-Not-Change-Array2025-10-30.jpg]

PS:

[Image: Can-Not-Change-Array2-2025-10-30.jpg]
Reply
#3
Where and what is a()?

Code: (Select All)

Yval = PolyEval(a(), degree, Xval)
Reply
#4
The missing code might be in the attached file, downloaded 0 times so far.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#5
(10-30-2025, 03:31 PM)bplus Wrote: The missing code might be in the attached file, downloaded 0 times so far.
I just downloaded it, it's the same code, a() is missing. It was worth a try.
Reply
#6
Line 13 defines a(), no? Dim Shared a(degree) as Double
Reply
#7
(10-30-2025, 04:18 PM)NakedApe Wrote: Line 13 defines a(), no? Dim Shared a(degree) as Double
That's right, I overlooked that. Otherwise, I can't get rid of the error message.
Reply
#8
“PolyEval” is defined as an array of Doubles, but is called seemingly as a function with parameters on lines 42 and 59?!
Reply
#9
Ah NakedApe beat me to post time Smile  thumbs up!
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#10
I did not write this code. It was generated by chatGPT. I have limited programing skills, so I really need an exact solution. Also, there maybe other errors that will need to corrected. Regards and Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What do you guys like to use for mouse mapping? Pete 32 4,260 01-07-2025, 03:35 PM
Last Post: OldMoses
  Clearing the mouse and keyboard buffers Donald Foster 1 592 03-09-2024, 07:47 AM
Last Post: a740g
  IDE' mouse problem with 3.4.1 on macOS Fifi 8 1,833 01-13-2023, 01:06 PM
Last Post: Fifi

Forum Jump:


Users browsing this thread: 1 Guest(s)