一百個例題 (37 ~ 40)
Ching-Tang Tseng
Hamilton, New Zealand
5 September 2024
\ (37)複數係數一元二次方程式的根.f \ Two roots of complex coefficient quadratic equation \ 2011-04-12 Ching-Tang Tseng, Hamilton NZ \ Equation: F(x)=A*x^2+B*x+C=0 roots: x1,x2 \ Renww date: 2014-08-08 8 complexs A B C Disc x1 x2 F(x1) F(x2) : Reset ( -- ) BASIC 10 let [ A = 0 + 0 i ] :: [ B = 0 + 0 i ] :: [ C = 0 + 0 i ] :: [ DISC = 0 + 0 i ] :: [ X1 = 0 + 0 i ] :: [ X2 = 0 + 0 i ] 20 end ; : QUAD ( -- ) BASIC 10 let [ Disc = SQRT ( B * B - ( 4 + 0 i ) * A * C ) ] :: [ x1 = ( ( NEGATE ( B ) + Disc ) ) / ( ( 2 + 0 i ) * A ) ] :: [ x2 = ( ( NEGATE ( B ) - Disc ) ) / ( ( 2 + 0 i ) * A ) ] :: [ F(x1) = A * x1 * x1 + B * x1 + C ] :: [ F(x2) = A * x2 * x2 + B * x2 + C ] 20 end ; : EnterCoefficients ( -- ) BASIC 10 print " Enter coefficient A, ( 3 - 5 i ) must type: 3 -5 " 20 InputZ A 30 run CR 40 print " Enter coefficient B, ( -2.5 + 0 i ) must type: -2.5 0 " 50 InputZ B 60 run CR 70 print " Enter coefficient C, ( 0 + 6.3 i ) must type: 0 6.3 " 80 InputZ C 90 run CR 100 end ; : Main ( -- ) BASIC 10 run CR 20 Print " Quadratic Eq.: A*x^2+B*x+C=0 " 30 run CR => Reset => EnterCoefficients => QUAD => CR 40 print [ " x1 = " ; x1 ] 50 print [ " F(x1)= " ; F(x1) ] 60 run CR 70 print [ " x2 = " ; x2 ] 80 print [ " F(x2)= " ; F(x2) ] 90 run CR CR 100 end ; \s main Quadratic Eq.: A*x^2+B*x+C=0 Enter coefficient A, ( 3 - 5 i ) must type: 3 -5 ? 3 -5 Enter coefficient B, ( -2.5 + 0 i ) must type: -2.5 0 ? -2.5 0 Enter coefficient C, ( 0 + 6.3 i ) must type: 0 6.3 ? 0 6.3 x1 = -.876151 + .445031 i F(x1)= -4.44089E-16 + 3.55271E-15 i x2 = 1.09674 - .077384 i F(x2)= -1.33227E-15 + 4.44089E-15 i
\ ABC FORTH demo code: Inverse Matrix \ Author: Ching-Tang Tseng, Hamilton, New Zealand \ Date: 2009-06-14 (renewed for c.l.f posting on 2014-12-18) \ Contact: ilikeforth@gmail.com \ Reference: F.R.Rukdeschel,"BASIC Scientific Subroutines Vol.1', \ BYTE/McGRAW-HILL, 1980, p.95, Matrix inversion subroutine (MATINV) \ Gauss-Jordan Elimination \ Matrix A is input. Matrix C is ouptput 20 VALUE D \ Unique setting in this program for maximum matrix Dimension D D MATRIX U D D MATRIX V D D MATRIX W D D MATRIX A D D 2 * MATRIX B D D MATRIX C 7 INTEGERS I J K N I+N J+N M 1 REALS B1 (( : InputData ( -- ) [[ N = 5 ]] {{ A ( 1 1 ) = 1 }} {{ A ( 1 2 ) = 0 }} {{ A ( 1 3 ) = 0 }} {{ A ( 1 4 ) = 0 }} {{ A ( 1 5 ) = 0 }} {{ A ( 2 1 ) = 1 }} {{ A ( 2 2 ) = 1 }} {{ A ( 2 3 ) = 0 }} {{ A ( 2 4 ) = 0 }} {{ A ( 2 5 ) = 0 }} {{ A ( 3 1 ) = 1 }} {{ A ( 3 2 ) = 1 }} {{ A ( 3 3 ) = 1 }} {{ A ( 3 4 ) = 0 }} {{ A ( 3 5 ) = 0 }} {{ A ( 4 1 ) = 1 }} {{ A ( 4 2 ) = 1 }} {{ A ( 4 3 ) = 1 }} {{ A ( 4 4 ) = 1 }} {{ A ( 4 5 ) = 0 }} {{ A ( 5 1 ) = 1 }} {{ A ( 5 2 ) = 1 }} {{ A ( 5 3 ) = 1 }} {{ A ( 5 4 ) = 1 }} {{ A ( 5 5 ) = 1 }} ; )) : InputData ( -- ) [[ N = 5 ]] {{ A ( 1 1 ) = 1 }} {{ A ( 1 2 ) = 3 }} {{ A ( 1 3 ) = 2 }} {{ A ( 1 4 ) = 6 }} {{ A ( 1 5 ) = 4 }} {{ A ( 2 1 ) = 3 }} {{ A ( 2 2 ) = 5 }} {{ A ( 2 3 ) = 4 }} {{ A ( 2 4 ) = 2 }} {{ A ( 2 5 ) = 6 }} {{ A ( 3 1 ) = 3 }} {{ A ( 3 2 ) = 8 }} {{ A ( 3 3 ) = 6 }} {{ A ( 3 4 ) = 7 }} {{ A ( 3 5 ) = 5 }} {{ A ( 4 1 ) = 2 }} {{ A ( 4 2 ) = 5 }} {{ A ( 4 3 ) = 3 }} {{ A ( 4 4 ) = 4 }} {{ A ( 4 5 ) = 4 }} {{ A ( 5 1 ) = 7 }} {{ A ( 5 2 ) = 6 }} {{ A ( 5 3 ) = 8 }} {{ A ( 5 4 ) = 7 }} {{ A ( 5 5 ) = 6 }} ; : MatrixInverse ( -- ) BASIC 10 FOR I = 1 TO N 20 FOR J = 1 TO N 30 LET J+N = J + N 40 LET { B ( I J+N ) = 0 } 50 LET { B ( I J ) = A ( I J ) } 60 NEXT J 70 LET I+N = I + N 80 LET { B ( I I+N ) = 1 } 90 NEXT I \ Perform row oeiented operations to convert the left hand side of B \ to the identity matrix. The inverse of A will then be on the right. 100 FOR K = 1 TO N 110 IF K = N THEN 240 120 LET M = K \ Find maximum element 130 FOR I = K + 1 TO N 140 IF { ABS ( B ( I K ) ) > ABS ( B ( M K ) ) } THEN 160 150 GOTO 170 160 LET M = I 170 NEXT I 180 IF M = K THEN 240 190 FOR J = K TO 2 * N 200 LET { B1 = B ( K J ) } 210 LET { B ( K J ) = B ( M J ) } 220 LET { B ( M J ) = B1 } 230 NEXT J \ Divide row K 240 FOR J = K + 1 TO 2 * N 250 LET { B ( K J ) = B ( K J ) / B ( K K ) } 260 NEXT J 270 IF K = 1 THEN 340 280 FOR I = 1 TO K - 1 290 FOR J = K + 1 TO 2 * N 300 LET { B ( I J ) = B ( I J ) - B ( I K ) * B ( K J ) } 310 NEXT J 320 NEXT I 330 IF K = N THEN 350 \ Beware of this branch will be out of K loop. 340 GOTO 370 350 RUN 2drop 360 GOTO 430 370 FOR I = K + 1 TO N 380 FOR J = K + 1 TO 2 * N 390 LET { B ( I J ) = B ( I J ) - B ( I K ) * B ( K J ) } 400 NEXT J 410 NEXT I 420 NEXT K \ Retrieve inverse from the right side of B 430 FOR I = 1 TO N 440 FOR J = 1 TO N 450 LET J+N = J + N 460 LET { B ( I J ) = B ( I J+N ) } 470 NEXT J 480 NEXT I \ B is not a square matrix so transfer to square matrix C 490 FOR I = 1 TO N 500 FOR J = 1 TO N 510 LET { C ( I J ) = B ( I J ) } 520 NEXT J 530 NEXT I 490 END ; : PrintMatrixW ( -- ) BASIC 10 RUN CR 20 FOR I = 1 TO N 30 FOR J = 1 TO N 40 LET { B1 = W ( I J ) } \ Based on W matrix 40 RUN B1 (F,) \ Right justified printing 50 NEXT J 60 RUN CR 70 NEXT I 80 END ; : TotalAmount ( -- ) D 1+ DUP * 3 CELLS * ; : MoveAtoW ( -- ) \ address-of A 0 0 FMatrix \ address-of W 0 0 FMatrix {{ A 0 0 }} {{ W 0 0 }} \ or TotalAmount CMOVE ; : MoveCtoW ( -- ) address-of C 0 0 FMatrix address-of W 0 0 FMatrix TotalAmount CMOVE ; : MoveAtoU ( -- ) address-of A 0 0 FMatrix address-of U 0 0 FMatrix TotalAmount CMOVE ; : MoveCtoV ( -- ) address-of C 0 0 FMatrix address-of V 0 0 FMatrix TotalAmount CMOVE ; : Matrix* ( -- ) BASIC \ Square Matrix Multiplication \ W = U x V 10 FOR I = 1 TO N 20 FOR J = 1 TO N 30 LET { W ( I J ) = 0 } 40 FOR K = 1 TO N 50 LET { W ( I J ) = W ( I J ) + U ( I K ) * V ( K J ) } 60 NEXT K 70 NEXT J 80 NEXT I 90 END ; : Checking ( -- ) BASIC 10 RUN MoveAtoU 20 RUN MoveCtoV 30 RUN Matrix* 40 RUN PrintMatrixW 50 END ; : Once ( -- ) BASIC 10 PRINT " Matrix A = " 20 RUN MoveAtoW 30 RUN PrintMatrixW 40 RUN MatrixInverse 50 PRINT " Inverse of A = " 60 RUN MoveCtoW 70 RUN PrintMatrixW 80 PRINT " Matrix A times inverse of A = Idendity Matrix " 90 RUN Checking 100 END ; : test ( -- ) BASIC 10 RUN InputData 20 RUN Once 30 END ; : hi ( -- ) BASIC 10 PRINT " Input dimension of Matrix to be inverted " 20 INPUTI N 30 FOR I = 1 TO N 40 PRINT " Input row " ; I 50 FOR J = 1 TO N 60 RUN CR 70 INPUTR B1 80 LET { A ( I J ) = B1 } 90 NEXT J 100 NEXT I 110 RUN Once 120 END ; : ReadData ( -- ) BASIC 10 RUN S" (38-1)MatInvData01.f" Get-File 20 RUN GetOneLineData 30 LET N = INT ( BRA ( 1 ) ) 40 FOR I = 1 TO N 50 RUN GetOneLineData 60 FOR J = 1 TO N 70 LET { A ( I J ) = BRA ( J ) } 80 NEXT J 90 NEXT I 100 END ; : main ( -- ) BASIC 10 RUN ReadData 20 RUN Once 30 END ; page .( Usage: ) cr .( 1. test : for fixed input data ) cr .( 2. hi : for interactive input data ) cr .( 3. main : for data input from file ) cr \S Usage: 1. test : for fixed input data 2. hi : for interactive input data ok test Matrix A = 1.00000 3.00000 2.00000 6.00000 4.00000 3.00000 5.00000 4.00000 2.00000 6.00000 3.00000 8.00000 6.00000 7.00000 5.00000 2.00000 5.00000 3.00000 4.00000 4.00000 7.00000 6.00000 8.00000 7.00000 6.00000 Inverse of A = -.262431 -.367403 -.602210 1.17680 .259669 -.279006 -.190608 -.071823 .745856 -.060773 .135359 .389503 .668508 -1.48066 -.049724 .157459 -.179558 -.038674 .093923 .044199 .220994 .309392 -.071823 -.254144 -.060773 Matrix A times inverse of A = Idendity Matrix 1.00000 -2.22045E-16 -1.11022E-16 -2.22045E-16 -2.77556E-17 2.22045E-16 1.00000 -2.77556E-16 -2.22045E-16 5.55112E-17 0.00000E0 -2.22045E-16 1.00000 4.44089E-16 5.55112E-17 1.11022E-16 -2.22045E-16 -3.33067E-16 1.00000 -5.55112E-17 2.22045E-16 -8.88178E-16 -1.11022E-16 8.88178E-16 1.00000 ok hi Input dimension of Matrix to be inverted ? 3 Input row 1 ? 1 ? 0 ? 0 Input row 2 ? 0 ? 2 ? 0 Input row 3 ? 0 ? 0 ? 3 Matrix A = 1.00000 0.00000E0 0.00000E0 0.00000E0 2.00000 0.00000E0 0.00000E0 0.00000E0 3.00000 Inverse of A = 1.00000 0.00000E0 0.00000E0 0.00000E0 .500000 0.00000E0 0.00000E0 0.00000E0 .333333 Matrix A times inverse of A = Idendity Matrix 1.00000 0.00000E0 0.00000E0 0.00000E0 1.00000 0.00000E0 0.00000E0 0.00000E0 1.00000 ok 3 sigdigits ! ok main Fileid is: 1212 139 Bytes are read into the Fadr RAM buffer! Matrix A = 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 1.00 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 1.00 1.00 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 1.00 1.00 1.00 1.00 0.00E0 0.00E0 0.00E0 0.00E0 1.00 1.00 1.00 1.00 1.00 0.00E0 0.00E0 0.00E0 1.00 1.00 1.00 1.00 1.00 1.00 0.00E0 0.00E0 1.00 1.00 1.00 1.00 1.00 1.00 1.00 0.00E0 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 Inverse of A = 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 -1.00 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 -1.00 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 -1.00 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 -1.00 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 -1.00 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 -1.00 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 -1.00 1.00 Matrix A times inverse of A = Idendity Matrix 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 1.00 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 0.00E0 1.00 ok The format of data to be stored in MatInvData01.f is as folloing: 8 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1
\ PulleyBeltLength.f (( Theme : The length of pulley's belt Author : Ching-Tang Tseng, Hamilton, New Zealand Date : 2015-01-18 Reference : Frank Ayers, JR."Plane and spherical Trigonometry", Schaum publishing Co., 1954, p.115 Description : Find the length (1)L1 and (2)L2 of (1)an open driving belt (2)a crossed driving belt running around two pulleys of radii B, and s, respectively if the distance between the centres of the wheels is d The formula are: Theta T1 = Arcsin((B-s)/d), then Length L1 = (B+s)*pi + 2*(B-s)*T1 + 2*d*cos(T1) e.g. B=15, s=5, d=30 then L1=126.2 Theta T2 = Arcsin((B+s)/d), then Length L2 = (B+s)*(pi+2*T2) + 2*(B-s)*T2 + 2*d*cos(T2) e.g. B=10, s=5, d=30 then L2=114.8 )) 7 Reals B s d T1 L1 T2 L2 : hi ( -- ) basic \ QAEnter data 10 print " Enter Big R, Small r, Distance d ( B s d )=" 20 inputr B , s , d \ Enter data rational checking 30 if { d >= ( B + s ) } then 60 40 print " Warning! d must >= (B+s), Re-Enter Please." 50 goto -10 \ Calculation by formula 60 let { T1 = ASIN ( ( B - s ) / d ) } 70 let { L1 = ( B + s ) * fpi + 2 * ( B - s ) * T1 + 2 * d * cos ( T1 ) } 80 let { T2 = ASIN ( ( B + s ) / d ) } 90 let { L2 = ( B + s ) * ( fpi + 2 * T2 ) + 2 * d * cos ( T2 ) } \ Results output 100 print " The length of an open driving belt is : " ; { L1 } 110 print " The length of a crossed driving belt is : " ; { L2 } 120 end ; \S hi Enter Big R, Small r, Distance d ( B s d )=? 15 5 30 The length of an open driving belt is : 126.197 The length of a crossed driving belt is : 136.742 ok hi Enter Big R, Small r, Distance d ( B s d )=? 10 5 30 The length of an open driving belt is : 107.959 The length of a crossed driving belt is : 114.793 ok
\ (40)LinearInterpolation.f (( Theme : Linear interpolation Author : Ching-Tang Tseng, Hamilton, New Zealand Date : 2015-01-25 Reference : NAN Description : Yi = Y1+(Xi-X1)*(Y2-Y1)/(X2-X1), X1<X2 )) 6 Reals X1 Y1 X2 Y2 Xi Yi : RESET {{ X1 = 0 }} {{ Y1 = 0 }} {{ X2 = 0 }} {{ Y2 = 0 }} {{ Xi = 0 }} {{ Yi = 0 }} ; : QAEnter BASIC 10 RUN PAGE CR 20 PRINT " 線性內插程式,根據兩組已知之座標值,計算出位於其間的內插值。 " 30 PRINT " 本程式要求輸入之兩點座標之值,X1 必須小於 X2,請注意 X1<X2! " 40 RUN CR CR 50 PRINT " 請輸入第一組已知數據 X1 , Y1 : " 60 InputR X1 , Y1 70 PRINT " 請輸入第二組已知數據 X2 , Y2 : " 80 InputR X2 , Y2 90 END ; Integer K : hi BASIC 10 RUN RESET 20 RUN QAEnter 30 PRINT { " 待計算插值之 Xi 座標,被限制為介於 " ; X1 ; " 與 " ; X2 ; " 之間 " } 40 PRINT " 請輸入 Xi 之值 " 50 InputR Xi 60 IF { ( Xi >= X1 ) AND ( Xi <= X2 ) } THEN 90 70 PRINT " Xi 超出範圍,請重新輸入! " 80 GOTO -30 90 LET { Yi = Y1 + ( Xi - X1 ) * ( Y2 - Y1 ) / ( X2 - X1 ) } 100 PRINT { " 當 Xi = " ; Xi ; " 時 " ; " Yi = " ; Yi } 110 PRINT CR " 繼續計算 Xi 之 Yi 值,Repeat(Y/N)? " 120 LET K = KEY 130 IF ( K = 89 ) OR ( K = 121 ) THEN -40 140 END ;
沒有留言:
張貼留言