home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Loadstar 12
/
012.d81
/
gauss
(
.txt
)
< prev
next >
Wrap
Commodore BASIC
|
2022-08-26
|
3KB
|
98 lines
10 rem **********************
11 rem * program gauss, version i, by shlomo ginsburg, may 1984
12 rem * this solves a system of n linear equations with n unknowns
13 rem * it notifies the user when there is no solution
14 rem * variables:
15 rem * n = number of equations
16 rem * a(i,j) = elements of the coefficient matrix i,j=1,2,...,n
17 rem * b(i) = elements of the right hand side (constants)
18 rem * k = step indicator (a total of n-1 steps is rtrequired)
19 rem * r = diagonal element by which equations are divided
20 rem * t = temporary storage for row interchange
21 rem **********************
22 rem *
23 rem *
24 poke 53280,11:poke 53281,0
25 print"[147] a system of n linear equations ";
26 print" gauss elimination [146]"
30 rem * beginning of program - input
40 input " [158]number of equations (n[158])";n
45 ifn<1orn>80thenprint"try a better number!":fordl=1to500:next:goto40
46 print" "
50 dim a(n,n),b(n)
60 print" input the coefficients of a[158]"
70 for i=1 to n
80 for j=1 to n
90 print " a("i","j")";
100 input "";a(i,j)
110 next j:next i
120 print" input the coefficients of [150]b[158]"
130 for i=1 to n
140 print " b("i")";
150 input b(i)
160 next i
170 if n=1 then 540: rem single equation
180 for k=1 to n-1: rem step counter
190 r=a(k,k)
200 rem * check for zero diagonal. instead of zero we use 1/1000000
210 if abs(r)>.000001 then 390: rem no need for interchange
220 rem * interchange rows
230 for j=(k+1) to n
240 if abs(a(j,k))>.000001 then 260: rem found the row for interchange
250 goto 350: rem keep looking for row
260 for l=k to n: rem interchange row j with row k - a's
270 t=a(k,l)
280 a(k,l)=a(j,l)
290 a(j,l)=t
300 next l
310 t=b(k): rem interchange b's
320 b(k)=b(j)
330 b(j)=t
340 goto 390
350 next j
360 print " no solution ! "
370 goto 700
380 rem * dividing row by diagonal element a(k,k)
390 r=a(k,k)
400 for j=(k+1) to n
410 a(k,j)=a(k,j)/r
420 next j
430 b(k)=b(k)/r
440 rem * elimination of x(k) from rows k+1, k+2, ... , n
450 for i=(k+1) to n
460 r=a(i,k)
470 for j=(k+1) to n
480 a(i,j)=a(i,j)-r*a(k,j)
490 next j
500 b(i)=b(i)-r*b(k)
510 next i
520 next k: rem end of steps
530 rem * last equation for a(n,n)
540 if abs(a(n,n))>.000001 then 570
550 print " no solution ! "
560 goto 700
570 b(n)=b(n)/a(n,n)
580 rem * backsubstitution
590 for i=1 to (n-1)
600 k=n-i
610 for j=(k+1) to n
620 b(k)=b(k)-a(k,j)*b(j)
630 next j:next i
640 print"[147] results "
650 for i=1 to n
660 print" touch any key to continue"
670 print" x("i") =";b(i)
680 get a$:if a$="" then 680
690 next i
700 goto60000
800 :
60000 fordl=1to500:nextdl
60001 print"[147]would you like to try another?";
60002 poke198,0:wait198,1:geta$:ifa$<>"y"anda$<>"n"thenpoke53280,rnd(1)*15:goto60002
60004 ifa$="y"thenclr:restore:goto21
60006 goto63000
60010 :
63000 rem connect back to l.s.
63002 print"[147]load"chr$(34)"payload"chr$(34)",8":print"run"
63004 poke198,0:poke631,13:poke632,13:poke198,2:end