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


Messages In This Thread
Polynomial Regression + Mouse Interpolation ' QB64-PE Example - by ajax12 - 10-30-2025, 01:34 PM

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

Forum Jump:


Users browsing this thread: 1 Guest(s)