Multiple regression: Difference between revisions

Added various BASIC dialects (BASIC256, Chipmunk Basic, QBasic, QB64, True BASIC and Yabasic)
m (→‎{{header|Wren}}: Minor tidy)
(Added various BASIC dialects (BASIC256, Chipmunk Basic, QBasic, QB64, True BASIC and Yabasic))
 
Line 574:
[128.812803581374, -143.162022866676, 61.9603254433538]
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|QB64}}
<syntaxhighlight lang="vbnet">arraybase 0
N = 14: M = 2: Q = 3 # number of points and M.R. polynom degree
 
dim X(N+1) # data points
X[0] = 1.47 : X[1] = 1.50 : X[2] = 1.52
X[3] = 1.55 : X[4] = 1.57 : X[5] = 1.60
X[6] = 1.63 : X[7] = 1.65 : X[8] = 1.68
X[9] = 1.70 : X[10] = 1.73 : X[11] = 1.75
X[12] = 1.78 : X[13] = 1.80 : X[14] = 1.83
dim Y(N+1) # data points
Y[0] = 52.21 : Y[1] = 53.12 : Y[2] = 54.48
Y[3] = 55.84 : Y[4] = 57.20 : Y[5] = 58.57
Y[6] = 59.93 : Y[7] = 61.29 : Y[8] = 63.11
Y[9] = 64.47 : Y[10] = 66.28 : Y[11] = 68.10
Y[12] = 69.92 : Y[13] = 72.19 : Y[14] = 74.46
dim S(N+1): dim T(N+1) # linear system coefficient
dim A(M+1, Q+1) # system to be solved
dim p(M+1, Q+1)
 
for k = 0 to 2 * M
S[k] = 0: T[k] = 0
for i = 0 to N
S[k] = S[k] + X[i] ^ k
if k <= M then T[k] = T[k] + Y[i] * X[i] ^ k
next i
next k
 
# build linear system
for fila = 0 to M
for columna = 0 to M
A[fila, columna] = S[fila + columna]
next columna
A[fila, columna] = T[fila]
next fila
 
print "Linear system coefficents:"
for i = 0 to M
for j = 0 to M + 1
print rjust(A[i, j], 14.1);
next j
print
next i
 
for j = 0 to M
for i = j to M
if A[i, j] <> 0 then exit for
next i
if i = M + 1 then print: print "SINGULAR MATRIX '" : end
for k = 0 to M + 1
p[j,k] = A[i,k] : A[i,k] = p[j,k] : A[j,k] = A[i,k]
next k
z = 1 / A[j, j]
for k = 0 to M + 1
A[j, k] = z * A[j, k]
next k
for i = 0 to M
if i <> j then
z = -A[i, j]
for k = 0 to M + 1
A[i, k] = A[i, k] + z * A[j, k]
next k
end if
next i
next j
 
print: print "Solutions:"
for i = 0 to M
print rjust(A[i, M + 1], 16.7);
next i
end</syntaxhighlight>
{{out}}
<pre>Linear system coefficents:
15.0 24.76 41.0532 931.17
24.76 41.0532 68.367934 1548.2453
41.0532 68.367934 114.34828728 2585.543151
 
Solutions:
128.81280358 -143.162022867 61.9603254432</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 cls
110 n = 14 : m = 2 : q = 3 ' number of points and M.R. polynom degree
120 dim x(n)' data points
130 data 1.47,1.5,1.52,1.55,1.57,1.6,1.63,1.65,1.68,1.7,1.73,1.75,1.78,1.8,1.83
140 for c = 0 to n
150 read x(c)
160 next c
170 dim y(n)' data points
180 data 52.21,53.12,54.48,55.84,57.2,58.57,59.93,61.29,63.11,64.47,66.28,68.1,69.92,72.19,74.46
190 for c = 0 to n
200 read y(c)
210 next c
220 dim s(n) : dim t(n)' linear system coefficient
230 dim a(m,q)' sistem to be solved
240 dim p(m,q)
250 for k = 0 to 2*m
260 s(k) = 0 : t(k) = 0
270 for i = 0 to n
280 s(k) = s(k)+x(i)^k
290 if k <= m then t(k) = t(k)+y(i)*x(i)^k
300 next i
310 next k
320 ' build linear system
330 for fila = 0 to m
340 for columna = 0 to m
350 a(fila,columna) = s(fila+columna)
360 next columna
370 a(fila,columna) = t(fila)
380 next fila
390 print "Linear system coefficents:"
400 for i = 0 to m
410 for j = 0 to m+1
420 print using "######.#";a(i,j);
430 next j
440 print
450 next i
460 for j = 0 to m
470 for i = j to m
480 if a(i,j) <> 0 then goto 500
490 next i
500 if i = m+1 then
510 print : print "SINGULAR MATRIX '"
520 end
530 endif
540 for k = 0 to m+1
560 p(j,k) = a(i,k)
570 a(i,k) = p(j,k)
580 a(j,k) = a(i,k)
590 next k
600 z = 1/a(j,j)
610 for k = 0 to m+1
620 a(j,k) = z*a(j,k)
630 next k
640 for i = 0 to m
650 if i <> j then
660 z = -a(i,j)
670 for k = 0 to m+1
680 a(i,k) = a(i,k)+z*a(j,k)
690 next k
700 endif
710 next i
720 next j
730 print : print "Solutions:"
740 for i = 0 to m
750 print using " #####.#######";a(i,m+1);
760 next i
770 print
780 end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
The [[#QB64|QB64]] solution works without any changes.
 
==={{header|QB64}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Const N = 14, M = 2, Q = 3 ' number of points and M.R. polynom degree
 
Dim X(N) As Double ' data points
Data 1.47,1.50,1.52,1.55,1.57,1.60,1.63,1.65,1.68,1.70,1.73,1.75,1.78,1.80,1.83
For c = LBound(X) To UBound(X)
Read X(c)
Next c
Dim As Double Y(N) ' data points
Data 52.21,53.12,54.48,55.84,57.20,58.57,59.93,61.29,63.11,64.47,66.28,68.10,69.92,72.19,74.46
For c = LBound(Y) To UBound(Y)
Read Y(c)
Next c
Dim As Double S(N), T(N) ' linear system coefficient
Dim As Double A(M, Q) ' system to be solved
Dim As Integer i, k, j, fila, columna
Dim As Double z
 
For k = 0 To 2 * M
S(k) = 0: T(k) = 0
For i = 0 To N
S(k) = S(k) + X(i) ^ k
If k <= M Then T(k) = T(k) + Y(i) * X(i) ^ k
Next i
Next k
 
' build linear system
For fila = 0 To M
For columna = 0 To M
A(fila, columna) = S(fila + columna)
Next columna
A(fila, columna) = T(fila)
Next fila
 
Print "Linear system coefficents:"
For i = 0 To M
For j = 0 To M + 1
Print Using "######.#"; A(i, j);
Next j
Print
Next i
 
For j = 0 To M
For i = j To M
If A(i, j) <> 0 Then Exit For
Next i
If i = M + 1 Then
Print: Print "SINGULAR MATRIX '"
End
End If
For k = 0 To M + 1
Swap A(j, k), A(i, k)
Next k
z = 1 / A(j, j)
For k = 0 To M + 1
A(j, k) = z * A(j, k)
Next k
For i = 0 To M
If i <> j Then
z = -A(i, j)
For k = 0 To M + 1
A(i, k) = A(i, k) + z * A(j, k)
Next k
End If
Next i
Next j
 
Print: Print "Solutions:"
For i = 0 To M
Print Using " #####.#######"; A(i, M + 1);
Next i
End</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|True BASIC}}===
{{trans|QB64}}
<syntaxhighlight lang="qbasic">OPTION BASE 0
LET n = 14 ! number of points and M.R. polynom degree
LET m = 2
LET q = 3
DIM x(0) ! data points
MAT REDIM x(n)
DATA 1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83
FOR c = LBOUND(x) TO UBOUND(x)
READ x(c)
NEXT c
DIM y(0) ! data points
MAT REDIM y(n)
DATA 52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46
FOR c = LBOUND(y) TO UBOUND(y)
READ y(c)
NEXT c
DIM s(0) ! linear system coefficient
MAT REDIM s(n)
DIM t(0)
MAT REDIM t(n)
DIM a(0,0) ! system to be solved
MAT REDIM a(m, q)
DIM p(0,0)
MAT REDIM p(m, q)
 
FOR k = 0 TO 2*m
LET s(k) = 0
LET t(k) = 0
FOR i = 0 TO n
LET s(k) = s(k)+x(i)^k
IF k <= m THEN LET t(k) = t(k)+y(i)*x(i)^k
NEXT i
NEXT k
! build linear system
FOR fila = 0 TO m
FOR columna = 0 TO m
LET a(fila, columna) = s(fila+columna)
NEXT columna
LET a(fila, columna) = t(fila)
NEXT fila
PRINT "Linear system coefficents:"
FOR i = 0 TO m
FOR j = 0 TO m+1
PRINT USING "######.#": a(i, j);
NEXT j
PRINT
NEXT i
FOR j = 0 TO m
FOR i = j TO m
IF a(i, j) <> 0 THEN EXIT FOR
NEXT i
IF i = m+1 THEN
PRINT
PRINT "SINGULAR MATRIX '"
STOP
END IF
FOR k = 0 TO m+1
LET p(j, k) = a(i, k)
LET a(i, k) = p(j, k)
LET a(j, k) = a(i, k)
NEXT k
LET z = 1/a(j, j)
FOR k = 0 TO m+1
LET a(j, k) = z*a(j, k)
NEXT k
FOR i = 0 TO m
IF i <> j THEN
LET z = -a(i, j)
FOR k = 0 TO m+1
LET a(i, k) = a(i, k)+z*a(j, k)
NEXT k
END IF
NEXT i
NEXT j
PRINT
PRINT "Solutions:"
FOR i = 0 TO m
PRINT USING " #####.#######": a(i, m+1);
NEXT i
END</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|QB64}}
<syntaxhighlight lang="vbnet">// number of points and M.R. polynom degree
N = 14
M = 2
Q = 3
 
dim X(N) // data points
X(0) = 1.47 : X(1) = 1.50 : X(2) = 1.52
X(3) = 1.55 : X(4) = 1.57 : X(5) = 1.60
X(6) = 1.63 : X(7) = 1.65 : X(8) = 1.68
X(9) = 1.70 : X(10) = 1.73 : X(11) = 1.75
X(12) = 1.78 : X(13) = 1.80 : X(14) = 1.83
dim Y(N) // data points
Y(0) = 52.21 : Y(1) = 53.12 : Y(2) = 54.48
Y(3) = 55.84 : Y(4) = 57.20 : Y(5) = 58.57
Y(6) = 59.93 : Y(7) = 61.29 : Y(8) = 63.11
Y(9) = 64.47 : Y(10) = 66.28 : Y(11) = 68.10
Y(12) = 69.92 : Y(13) = 72.19 : Y(14) = 74.46
 
dim S(N), T(N) // linear system coefficient
dim A(M, Q) // sistem to be solved
dim p(M, Q)
 
for k = 0 to 2*M
S(k) = 0 : T(k) = 0
for i = 0 to N
S(k) = S(k) + X(i) ^ k
if k <= M T(k) = T(k) + Y(i) * X(i) ^ k
next i
next k
 
// build linear system
for fila = 0 to M
for columna = 0 to M
A(fila, columna) = S(fila+columna)
next columna
A(fila, columna) = T(fila)
next fila
 
print "Linear system coefficents:"
for i = 0 to M
for j = 0 to M+1
print A(i,j) using "#####.#";
next j
print
next i
 
for j = 0 to M
for i = j to M
if A(i,j) <> 0 break
next i
if i = M+1 then
print "\nSINGULAR MATRIX '"
end
end if
for k = 0 to M+1
p(j,k) = A(i,k) : A(i,k) = p(j,k) : A(j,k) = A(i,k)
next k
z = 1 / A(j,j)
for k = 0 to M+1
A(j,k) = z * A(j,k)
next k
for i = 0 to M
if i <> j then
z = -A(i,j)
for k = 0 to M+1
A(i,k) = A(i,k) + z * A(j,k)
next k
end if
next i
next j
 
print "\nSolutions:"
for i = 0 to M
print A(i,M+1) using "#######.#######";
next i
print
end</syntaxhighlight>
 
=={{header|BBC BASIC}}==
2,169

edits