GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — smallbasic.github.io/samples/node/113.bas at master · smallbasic/smallbasic.github.io Expand file tree/ Copy path113.basMore file actions 589 lines (512 loc) · 12.1 KB / Copy path113.basFile metadata and controls589 lines (512 loc) · 12.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 '''This program get: ''Power regression y= aX^b ''Exponential regression y=ae^(bx) ''Polynomial regression Ao +A1x + A2x^2... ''by least squares'' method ''Too, calculate interpolation. Given a x value get a y value ''Put data on subroutine ReadData ''Adapted of Numerical repices in C, chapter 15.4: http://www.library.cornell.edu/nr/bookcpdf.html ''by Adolfo Leon Sepulveda ''Sep-30-2004 ''Version 1.0 option base 1 ''arrays init on 1 cls menu end ''**************** sub menu() dim x(50),y(50), a(20,20),b(20) local cx,cy cx=0 cy=0 While true cls locate 1,5:print "Least Squares" locate 2,1 : print "1. Power regression y= aX^b" locate 3,1 : print "2. Exponential regression y=ae^(bx)" locate 4,1 : print "3. Polynomial regression Ao +A1x + A2x^2..." locate 5,1 : print "4. Quit" locate 6,1 : print "put tap on option" WaitTap cx, cy If cy>10 and cy<20 and cx >1 and cx<150 then Power elseif cy>21 and cy<30 and cx >1 and cx<150 Then expon elseif cy>31 and cy<40 and cx >1 and cx<150 Then poly elseif cy>41 and cy<50 and cx >1 and cx<150 Then Exit loop else locate 9,1: print "put tap on option" pause 1 locate 9,1: print spc(32) endif wend end ''**************** sub ReadData(byref x(), byref y(), byref NumData, opt) if opt = 1 then ''power regression data restore ReadDataopt1 ExecRead x,y,NumData label ReadDataopt1 data 5 ''num data data 28,30,33,35,38 ''x data data 2410,3033,3895,4491,5717 ''y data ''fit: y=0.238801069 x^2.771866158 elseif opt = 2 then ''exponential regression data restore ReadDataopt2 ExecRead x,y,NumData label ReadDataopt2 data 5 data 6.9,12.9,19.8,26.7,35.1 data 21.4,15.7,12.1,8.5,5.2 ''fit y=30.497587426 exp(-0.049203708x) elseif opt = 3 then '' polynomial regression data restore ReadDataopt3 ExecRead x,y,NumData label ReadDataopt3 data 6 data 20,30,40,50,60,70 data 54,90,138,206,292,396 ''fit to poly order 2: y= 41.771428571 - 1.095714286x + 0.087857143x^2 endif end sub ExecRead(byref x(), byref y(), byref NumData) read NumData for i=1 to NumData read x(i) next i for i=1 to NumData read y(i) next i end ''***************** sub power() local i,j,k dim a(20,20),b(20),x(20),y(20) ReadData x,y,NumData,1 for i=1 to NumData y(i) = log(y(i)) x(i) = log(x(i)) next i BuildMat x,y,NumData,a,b,2 gaussj a,b,2 PrintPow b,NumData EvaluatePow b end ''************* sub PrintPow(b(),NumData) local c,s,i,z c = exp(b(1)) print " " print "Power regression y= aX^b" print "y="; c; " x^";b(2) print " " '' return original points for i = 1 to NumData y(i) = exp(y(i)) x(i) = exp(x(i)) next i print " " s=0 print " x y Fit" for i = 1 to NumData z = c*x(i)^b(2) s = s + (y(i)-z)^2 print format("###,###.000",x(i)); " "; format("###,###.000",y(i)); " "; format("###,###.000",z) next i print "Chi2: "; s end ''************ sub EvaluatePow(b()) local x,y,s while true s="" print print "Interpolation (enter x value or ''e'' to exit) "; input "x= ";s if s IN "Ee" then exit sub endif x=val(s) y = exp(b(1)) * x ^ b(2) print "y= ";y wend end ''***************** sub expon() local i,NumData dim a(20,20),b(20),x(20),y(20) ReadData x,y,NumData,2 for i=1 to NumData y(i) = Log(y(i)) next i BuildMat x,y,NumData,a, b, 2 gaussj a,b,2 PrintExpon b,NumData EvaluateExpon b end ''******************* sub PrintExpon(b(),NumData) local c,s,i,z c = exp(b(1)) print " " print "Exponential regression y=ae^(bx):" print "y="; c; " exp(";b(2);"x)" print " " '' return to original y data for i = 1 to NumData y(i) = exp(y(i)) next i print " " print " x y Fit" s=0 for i = 1 to NumData z = c*exp(b(2)*x(i)) s = s + (y(i)-z)^2 print format("###,###.000",x(i)); " "; format("###,###.000",y(i)); " "; format("###,###.000",z) next i print "Chi2: "; s end ''********** sub EvaluateExpon(b()) local x,y,s while true s="" print print "Interpolation (enter x value or ''e'' to exit) "; input "x= ";s if s IN "Ee" then exit sub endif x=val(s) y = exp(b(1)) * exp(x * b(2)) print "y= ";y wend end ''************** sub poly local chisq, i,j,NumData,Degree,StdErr,r2 dim b(20) dim covar(20,20) dim x(20),y(20),FitTerm(20),sig(20) const spread=1 Input "Enter degree of poly to fit"; Degree ReadData x,y,NumData,3 FOR i = 1 to NumData sig(i) = spread next j for i=1 to Degree+1 FitTerm(i)=1 next i lfit x,y,sig,NumData,b,FitTerm,Degree,covar,chisq,r2,StdErr PrintPoly b,Degree,NumData PrintData x,y,NumData, b, Degree print "chi2="; chisq print "coeff of determination (r^2)= ";r2 print "coeff of correlation= ";r2^.5 print "standard error estimate= ";StdErr Print print "Covariance Matrix" PrintMat covar,Degree+1 EvaluatePoly b, Degree+1 end ''**************** sub Lfit(x(),y(), sig(),ndata, byref b(), FitTerm(), Degree, byref covar()(), byref chisq, byref r2, byref StdErr) ''Given a set of data points x(1..ndata), y(1..ndata) with individual standard deviations ''sig(1..ndata), use chi2 minimization to fit for some or all of the coefficients b(1..Degree+1) of ''a function that depends linearly on b, y = SUMi bi AFUNCi(X). The input array FitTerm(1..Degree+1) ''indicates by nonzero entries those components of a that should be fitted for, and by zero entries ''those components that should be held fixed at their input values. The program returns values ''for b(1..Degree+1), chi2 (chisq), coeff of determination (r2), Standard Error (StdErr) '' and the covariance matrix covar(l..Degree+1, l..Degree+1). ''(Parameters held fixed will return zero covariances.) The routine funcs (x, afunc,Degree+1) ''returns the Degree+1 basis of polynomial evaluated at x in the array afunc(1..Degree+1) ''http://www.library.cornell.edu/nr/bookcpdf/c15-1.pdf local k,kk,j,i,h,Sumy2,z,v,p,q local ym,wt,suma,sig2i, NumTerms dim bx(20), afunc(20),bxOrig(20), covarOrig(20,20) NumTerms = Degree + 1 mfit=0 for j=1 to NumTerms if FitTerm(j) then mfit = mfit + 1 next j if mfit = 0 then print "lfit: no parameters to be fitted" input xx exit sub endif FOR j = 1 TO NumTerms FOR k = 1 TO NumTerms covar(j,k) = 0.0 next k bx(j) = 0.0 next j FOR i = 1 TO ndata funcs x(i),afunc,NumTerms ym = y(i) if mfit < NumTerms then for j=1 to NumTerms if !FitTerms[j] then ym = ym - b(j)*afunc(j) next j endif sig2i = 1.0/sig(i)^2 j=0 FOR h = 1 TO NumTerms if FitTerm(h) then wt = afunc(h)*sig2i j=j+1 k=0 FOR m = 1 TO h k=k+1 if FitTerm(m) then covar(j,k) = covar(j,k)+wt*afunc(m) next m bx(j) = bx(j)+ym*wt endif next h next i FOR j = 2 TO mfit FOR k = 1 TO j-1 covar(k,j) = covar(j,k) next k next j bxOrig = bx covarOrig = covar gaussj covar,bx,mfit j=0 FOR h = 1 TO NumTerms if FitTerm(h) then j = j + 1 b(h) = bx(j) endif next h chisq = 0.0 Sumy2=0 FOR i = 1 TO ndata funcs x(i),afunc,NumTerms suma = 0.0 FOR j = 1 TO NumTerms suma = suma+b(j)*afunc(j) next j chisq = chisq + ((y(i)-suma)/sig(i))^2 Sumy2 = Sumy2 + y(i)^2 next i p=0 for j=2 to Degree+1 p= p + b(j) * (bxOrig(j) - covarOrig(j,1)*bxOrig(1) / nData) next j q = Sumy2 - bxOrig(1)^2/nData z=q-p v = nData - Degree - 1 r2 = p/q if v > 0 then StdErr = (z/v)^.5 else StdErr = 0 covsrt covar, NumTerms, FitTerm, mfit END ''************** sub funcs(x, byref afunc(), NumTerms) ''generate array afunc(1) = 1, afunc(2) = x, afunc(3) = x^2, afunc(4)= x^3,... '' evalute on x local i afunc(1) = 1.0 FOR i = 2 to NumTerms afunc(i) = x*afunc(i-1) next i END ''**************** sub covsrt(covar()(), NumTerms, FitTerm(), mfit) ''Expand in storage the covariance matrix covar, so as to take into account parameters that ''are being held fixed. (For the latter, return zero covariances.) local i,j,k,h for i=mfit+1 to NumTerms for j=1 to i covar(i,j) = 0.0 covar(j,i) = 0.0 next j next i k=mfit for h = 1 to NumTerms j = NumTerms - h + 1 if FitTerm(j) then for i=1 to NumTerms SWAP covar(i,k),covar(i,j) next i for i=1 to NumTerms SWAP covar(k,i),covar(j,i) next i k = k -1 endif next h end ''*********** sub gaussj(byref a()(), byref b(), m) local big,dum,pivinv,i,icol,irow,j,k,h,f dim indxc(50),indxr(50),ipiv(50) FOR j = 1 TO m ipiv(j) = 0 next j FOR i = 1 TO m big = 0.0 FOR j = 1 TO m IF ipiv(j) <> 1 THEN FOR k = 1 TO m IF ipiv(k) = 0 THEN IF abs(a(j,k)) >= big THEN big = abs(a(j,k)) irow = j icol = k ENDIF ELSEIF ipiv(k) > 1 THEN print "pause 1 in GAUSSJ - singular matrix" input xx exit sub ENDIF next k ENDIF next j ipiv(icol) = ipiv(icol)+1 IF irow <> icol THEN FOR h = 1 TO m dum = a(irow,h) a(irow,h) = a(icol,h) a(icol,h) = dum next h dum = b(irow) b(irow) = b(icol) b(icol) = dum ENDIF indxr(i) = irow indxc(i) = icol IF a(icol,icol) = 0.0 THEN print "pause 2 in GAUSSJ - singular matrix" input xx exit sub ENDIF pivinv = 1.0/a(icol,icol) a(icol,icol) = 1.0 FOR h = 1 TO m a(icol,h) = a(icol,h)*pivinv next h b(icol) = b(icol)*pivinv FOR f = 1 TO m IF f <> icol THEN dum = a(f,icol) a(f,icol) = 0.0 FOR h = 1 TO m a(f,h) = a(f,h)-a(icol,h)*dum next h b(f) = b(f)-b(icol)*dum ENDIF next f next i FOR i = 1 TO m h = m - i + 1 IF indxr(h) <> indxc(h) THEN FOR k = 1 TO m dum = a(k,indxr(h)) a(k,indxr(h)) = a(k,indxc(h)) a(k,indxc(h)) = dum next k ENDIF next i END ''**************** sub PrintPoly(b(),n,NumData) local i print print "Polynomial regression Ao +A1x + A2x^2..." if b(2) >= 0 then print "y= "; b(1);" + ";b(2);"x"; else print "y= "; b(1);" - ";abs(b(2));"x"; endif if n+1 > 2 then for i = 3 to n+1 if b(i) >= 0 then print " + ";b(i);"x^";i-1; else print " - ";abs(b(i));"x^";i-1; endif next i endif print print end ''**************** sub PrintData(x(),y(),NumData, b(), Degree) local s,i,z s=0 print "Data: " print " x y Estimate" for i= 1 to NumData z = b(1) for j= 2 to Degree+1 z = z + b(j) * x(i)^(j-1) next j s = s + (y(i) - z)^2 print format("###,###.000",x(i)); " "; format("###,###.000",y(i)); " "; format("###,###.000",z) next i print end ''**************** sub EvaluatePoly(b(),NumTerms) local x,y,s dim a(20) while true s="" print print "Interpolation (enter x value or ''e'' to exit) "; input "x= ";s if s IN "Ee" then cls exit sub end if x=val(s) funcs x, a, NumTerms y = 0 FOR j = 1 TO NumTerms y = y + b(j) * a(j) next j print "y= ";y wend end ''*************** sub BuildMat(x(),y(),NumData, byref a()(), byref b(), n) local i,j,k for i=1 to n for j=1 to n if i+j > 2 then for k = 1 to NumData a(i,j) = a(i,j)+ x(k)^(i+j-2) next k else a(i,j) = NumData endif next j for k = 1 to NumData if i > 1 then b(i) = b(i) + y(k) * x(k)^(i-1) else b(i) = b(i) + y(k) endif next k next i end ''***************** Sub PrintMat(a()(),n) for i=1 to n for j=1 to n print using "#,###,###.00000"; a(i,j); next j print Next i End ''************ sub WaitTap(byref cx, byref cy) pen on repeat until pen(0) <> 0 cx=pen(4) cy=pen(5) pen off end ' |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |