10-30-2025, 01:34 PM
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
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


![[Image: Can-Not-Change-Array2025-10-30.jpg]](https://i.ibb.co/1J7qB5J3/Can-Not-Change-Array2025-10-30.jpg)
![[Image: Can-Not-Change-Array2-2025-10-30.jpg]](https://i.ibb.co/HpTSSM1s/Can-Not-Change-Array2-2025-10-30.jpg)
thumbs up!