home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
MBUG
/
MBUG058.ARC
/
CURVFIT.BAS
< prev
next >
Wrap
BASIC Source File
|
1979-12-31
|
7KB
|
243 lines
10 REM
20 REM POLYNOMIAL CURVFIT
30 REM By Ralph Johnson 1-1982
35 REM
40 REM MAIN PROGRAM
100 GOSUB 1000 REM SET UP ARRAYS
110 GOSUB 1500 REM INPUT DATA
120 GOSUB 2000 REM CLEAR AND LOAD ARRAYS
130 GOSUB 2500 REM SIMUL. EQS. SOLUTION
135 GOSUB 3000 REM SUM OF ERRORS SQRD
140 GOSUB 3500 REM OUTPUT
150 GOTO 4000 REM DECISIONS
160 GOSUB 4500 REM EVALUATE
170 GOSUB 5000 REM PLOT
180 GOTO 4000
190 REM END MAIN
200 REM
210 REM SET UP ARRAY MEMORY
220 REM
1000 DIM A(20,20),B(20),C(20),X(20),Y(20)
1010 DIM XX(120),YY(120),Q(45),YZ(20)
1020 REM GREETING
1025 PRINT CHR$(&H1A)
1030 PRINT "******* POLYNOMIAL CURVE FIT *******"
1040 PRINT:PRINT
1050 PRINT" THIS PROGRAM PERFORMS A LEAST SQUARED"
1060 PRINT"CURVE FIT FOR A GENERAL POLYNOMIAL IN X AND Y"
1070 PRINT"IT WILL ALSO EVALUATE AND PLOT THE FUNCTION"
1080 PRINT"IF YOU WISH"
1090 PRINT:PRINT:PRINT
1100 RETURN
1110 REM END SETUP MEMORY
1500 REM INPUT SUBROUTINE
1510 REM RESET ARRAYS
1520 FOR I%=0 TO 20
1530 B(I%)=0
1540 Q(I%)=0
1560 NEXT I%
1565 IF Z% =2 THEN GOTO 1670
1570 INPUT"HOW MANY DATA PTS. ARE THERE";DATAPTS%
1575 PRINT CHR$(&H1A)
1580 FOR I%=1 TO 5
1590 PRINT
1600 NEXT I%
1610 PRINT"NOW INPUT THE DATA.":PRINT:PRINT
1620 FOR I%=1 TO DATAPTS%
1630 PRINT"X(";I%;"), Y(";I%;")";
1640 INPUT X(I%),Y(I%)
1650 NEXT I%
1660 REM
1670 INPUT"WHAT ORDER FIT WOULD YOU LIKE";ORDER%
1680 N%=ORDER%+1
1690 REM
1700 REM VALIDITY CHECK
1710 REM
1720 IF ORDER%>=DATAPTS% THEN PRINT"NOT ENOUGH DATA FOR THAT ORDER FIT": GOTO 1670
1730 PRINT CHR$(&H1A)
1750 RETURN
1760 REM END INPUT
1970 REM MATRIX LOAD
1980 REM GENERATE VALUES FOR ARRAY
2000 FOR I%=0 TO ORDER%*2
2010 FOR J%=1 TO DATAPTS%
2020 Q(I%)=X(J%)^I%+Q(I%)
2030 NEXT J%
2040 Q(I%)=2*Q(I%)
2050 NEXT I%
2060 REM LOAD
2070 FOR I%=0 TO ORDER%
2080 FOR J%=0 TO ORDER%
2090 A(I%+1,J%+1)=Q(I%+J%)
2100 NEXT J%
2110 NEXT I%
2120 FOR I%=0 TO ORDER%
2130 FOR J%=1 TO DATAPTS%
2140 B(I%+1)=2*X(J%)^I%*Y(J%)+B(I%+1)
2150 NEXT J%
2160 NEXT I%
2170 RETURN
2180 REM END MATRIX LOAD
2500 REM SIMULTANEOUS EQU SOLN
2510 FLAG%=0
2520 IF N%<>0 THEN GOTO 2560
2530 IF A(1,1)=0 THEN FLAG%=1: RETURN
2540 X(1)=B(1)/A(1,1)
2550 RETURN
2560 M%=N%-1
2570 FOR I%=1 TO M%
2580 H=ABS(A(I%,I%))
2590 L%=I%
2600 I1%=I%+1
2610 FOR J%=I1% TO N%
2620 IF ABS(A(J%,I%)) < H THEN 2650
2630 H=ABS(A(J%,I%))
2640 L%=J%
2650 NEXT J%
2660 IF H=0 THEN FLAG%=1: RETURN
2670 IF L%=I% THEN 2760
2680 FOR J%=1 TO N%
2690 G=A(L%,J%)
2700 A(L%,J%)=A(I%,J%)
2710 A(I%,J%)=G
2720 NEXT J%
2730 G=B(L%)
2740 B(L%)=B(I%)
2750 B(I%)=G
2760 FOR J%=I1% TO N%
2770 T=A(J%,I%)/A(I%,I%)
2780 FOR K%=I1% TO N%
2790 A(J%,K%)=A(J%,K%)-T*A(I%,K%)
2800 NEXT K%
2810 B(J%)=B(J%)-T*B(I%)
2820 NEXT J%
2830 NEXT I%
2840 IF A(N%,N%)=0 THEN FLAG%=1: RETURN
2850 C(N%)=B(N%)/A(N%,N%)
2860 I%=N%-1
2870 S=0
2880 I1%=I%+1
2890 FOR J%=I1% TO N%
2900 S=S+A(I%,J%)*C(J%)
2910 NEXT J%
2920 C(I%)=(B(I%)-S)/A(I%,I%)
2930 I%=I%-1
2940 IF I%>0 THEN 2870
2950 RETURN
2960 REM END SIMULTANEOUS EQU
3000 REM SUM OF ERRORS SQRD
3010 FOR I%=1 TO DATAPTS%
3020 YZ(I%)=0
3030 NEXT I%
3040 E=0
3050 FOR I%=1 TO DATAPTS%
3060 FOR J%=1 TO N%
3070 YZ(I%)=C(J%)*X(I%)^(J%-1)+YZ(I%)
3080 NEXT J%
3090 E=E+(Y(I%)-YZ(I%))^2
3100 NEXT I%
3110 RETURN
3120 REM END SUM ERRORS SQRD
3500 REM OUTPUT STATEMENTS
3510 IF FLAG%=1 THEN PRINT "AMBIGUOUS DATA. NO SOLUTION FOUND":RETURN
3520 PRINT"LIST OF COEFFICIENTS":PRINT
3530 PRINT"POWER OF X","COEFFICIENT":PRINT
3540 FOR I%=N% TO 1 STEP -1
3550 PRINT "X^";I%-1,C(I%)
3560 NEXT I%
3570 PRINT "SUM OF ERRORS SQRD=";E
3580 PRINT
3590 FOR I%=1 TO DATAPTS%
3600 PRINT"X=";X(I%),"Y=";YZ(I%),"DESIRED Y:";Y(I%)
3610 NEXT I%
3620 REM Printer
3630 PRINT:PRINT
3700 INPUT "Do you want a print out of this (y/n)";PR$
3710 IF PR$<>"y" AND PR$<>"Y" THEN GOTO 3830
3720 LPRINT "LIST OF COEFFICIENTS": LPRINT " "
3730 LPRINT " ": LPRINT "POWER OF X", "COEFFICIENT": LPRINT" "
3740 FOR I%=N% TO 1 STEP -1
3750 LPRINT "X^";I%-1,C(I%)
3760 NEXT I%
3770 LPRINT : LPRINT "SUM OF ERRORS SQRD=";E
3780 LPRINT" "
3790 FOR I%=1 TO DATAPTS%
3800 LPRINT "x=";X(I%),"y=";YZ(I%),"Desired y:";Y(I%)
3810 NEXT I%
3820 RETURN
3830 REM end output
4000 REM OPTIONS
4010 REM
4020 INPUT"HIT ENTER TO CONTINUE";A$
4030 PRINT CHR$(&H1A): PRINT: PRINT
4200 PRINT"Enter the desired option": PRINT: PRINT
4210 PRINT" 1)EVALUATE THE FUNCTION"
4220 PRINT" 2)TRY A DIFFERENT ORDER FIT"
4230 PRINT" 3)TRY A NEW DATA SET"
4235 PRINT" 4)EXIT THE PROGRAM"
4240 PRINT:INPUT"Enter your choice";Z%
4250 ON Z% GOTO 160,110,110,4260
4260 STOP
4270 REM END OPTIONS
4500 REM EVALUATE
4520 INPUT"SELECT THE RANGE YOU WANT TO EVALUATE min,max";XINIT,XFIN
4530 INCR=(XFIN-XINIT)/120
4540 ZO=0
4550 FOR I%=0 TO 120
4560 XX(I%)=XINIT+INCR*I%
4570 YY(I%)=0
4580 FOR J%=1 TO N%
4590 YY(I%)=C(J%)*XX(I%)^(J%-1)+YY(I%)
4600 NEXT J%
4610 IF I%=0 THEN YMIN=YY(0):YMAX=YMIN
4620 IF YY(I%)>YMAX THEN YMAX=YY(I%)
4630 IF YY(I%)<YMIN THEN YMIN=YY(I%)
4640 NEXT I%
4650 REM output evaluate
4680 PRINT CHR$(&H1A)
4690 PRINT"X AND Y VALUES":PRINT
4700 PRINT"X","Y","X","Y"
4710 FOR I%=0 TO 60 STEP 6
4720 PRINT XX(I%),YY(I%),XX(I%+60),YY(I%+60)
4730 NEXT I%
4740 PRINT
4750 PRINT"Y MAX.:";YMAX,"Y MIN.:";YMIN
4760 INPUT "DO YOU WANT THIS PRINTED OUT (y/n)";PR$
4770 IF PR$<>"Y" AND PR$<>"y" THEN RETURN
4790 LPRINT"X and Y values" : LPRINT " "
4800 LPRINT"X","Y","X","Y"
4810 FOR I%=0 TO 60 STEP 6
4820 LPRINT XX(I%),YY(I%),XX(I%+60),YY(I%+60)
4830 NEXT I%
4840 LPRINT" "
4850 LPRINT "Y MIN=";YMIN,"Y MAX=";YMAX
4860 RETURN
5000 REM PLOT
5010 INPUT"DO YOU WANT A PLOT OF THE FUNCTION (y/n)";PLOT$
5020 IF PLOT$="N" OR PLOT$="n" THEN RETURN
5030 INPUT"SELECT THE RANGE OF Y (ymin,ymax)";
YMIN,YMAX
5040 INPUT"SELECT THE COLUMN WIDTH";WDTH
5045 YRANGE=YMAX-YMIN
5050 ZERO=(0-YMIN)*WDTH/YRANGE+.5
5060 YRANGE=YMAX-YMIN
5070 FOR I%=0 TO 119 STEP 2
5080 PLOT$=" "
5090 YPLOT1%=INT((YY(I%)-YMIN)*WDTH/YRANGE+.5)
5100 YPLOT2%=INT((YY(I%+1)-YMIN)*WDTH/YRANGE+.5)
5110 FOR J%= 1 TO WDTH
5120 IF YPLOT1%=J% AND YPLOT2%=J% THEN
PLOT$=PLOT$+";": GOTO 5160
5130 IF YPLOT1%=J% THEN PLOT$=PLOT$+"'":
GOTO 5160
5140 IF YPLOT2%=J% THEN PLOT$=PLOT$+",":
GOTO 5160
5150 IF INT(ZERO)=J% THEN PLOT$=PLOT$+"|":
GOTO 5160
5155 PLOT$=PLOT$+" "
5160 NEXT J%
5170 PRINT PLOT$
5175 LPRINT PLOT$
5180 NEXT I%
5190 RETURN