Solving lineair equations
Posted: Fri Apr 12, 2013 7:36 pm
rem solving a set of n lineair equations with n unknowns
rem
option base 1
dim mat(7,7),inv(7,7),x(7),rhs(7)
mat(1,1)=0
inv(1,1)=0
rhs(1)=0
x(1)=0
draw color 0,0,0
fill color .8,.8,.8
get_n:
input "Enter the number of equations (2 - 7)":n
if n<2 or n>7 then goto get_n
graphics
cls
orient:
if screen_width()<900 then
draw font size 40
draw text "please rotate to landscape" at 50,300
draw font size 20
goto orient
end if
cls
top=100
wide=99*n
hght=29+30*n
draw text "Enter the values of the coefficients" at 30,30
draw text "(press Return key to accept each value entry!)" at 30,60
win_open ("Coefficients",30,top,30+wide,top+hght)
draw text "x" at 35+wide,top+hght/2
win_open ("unknown",52+wide,top,160+wide,top+hght)
draw text "=" at 165+wide,top+hght/2
win_open ("RHS",182+wide,top,276+wide,top+hght)
mat_in (n,n,mat,35,top+30)
fill rect 30,30 to 500,50
draw text "Enter the values of the right hand side (RHS)" at 30,30
vec_in (n,rhs,187+wide,top+30)
rem End of user interface
mat_inv (n,mat,inv)
mat_vec (n,n,inv,rhs,x)
vec_out ("",n,x,55+wide,top+36,8,2)
end
def win_open (title$,xtop,ytop,xbot,ybot)
r=10
draw color 0,0,0
draw size 4
draw circle xtop,ytop to xtop+20,ytop+20
draw circle xbot-20,ytop to xbot,ytop+20
draw circle xtop,ybot-20 to xtop+20,ybot
draw circle xbot-20,ybot-20 to xbot,ybot
draw line xtop+r,ytop to xbot-r,ytop
draw line xtop+r,ybot to xbot-r,ybot
draw line xtop,ytop+r to xtop,ybot-r
draw line xbot,ytop+r to xbot,ybot-r
fill rect xtop+r,ytop+2 to xbot-r,ybot-2
fill rect xtop+2,ytop+r to xbot-2,ybot-r
if title$<>"" then
l=(xbot-xtop-12*len(title$))/2
draw line xtop,ytop+24 to xbot,ytop+24
draw color 0,0,1
draw text title$ at xtop+l,ytop-2
draw color 0,0,0
end if
end def
def n2a$(num,lang,dec)
b$=" "
fac=10^dec
num$=int(fac*num+.5)/fac
tot=lang-len(num$)
if tot<1 then tot=1
a$=substr$(b$,1,tot) & num$
n2a$=a$
end def
def vec_in (n,v(),x,y)
dim b$(20)
for i=1 to n
b$(i)=" a" & i
field b$(i) text b$(i) at x,y+30*(i-1) size 80,25
next i
som=0
loop:
for i=1 to n
if field_changed(b$(i)) then
v(i)=field_text$(b$(i))
field b$(i) delete
draw text n2a$(v(i),6,2) at x,y+30*(i-1)
som=som+1
end if
next i
if som<n then goto loop
end def
def vec_out (n$,n,v(),x,y,l,d)
ys=y-35
if n$<>"" then
draw text n$ at x,y
ys=ys+30
end if
for i=1 to n
a$=n2a$(v(i),l,d)
draw text a$ at x-12,ys+30*i
next i
end def
def mat_in (n,m,mat(,),x,y)
dim b$(100)
b$(1)=0
for i=1 to n
for j=1 to m
k=m*(i-1)+j
b$(k)=" a" & i & j
tt$=b$(k)
field tt$ text tt$ at x+100*(j-1),y+30*(i-1) size 80,25
next j
next i
som=0
loop1:
for i=1 to n
for j=1 to m
k=m*(i-1)+j
if field_changed(b$(k)) then
mat(i,j)=field_text$(b$(k))
field b$(k) delete
draw text n2a$(mat(i,j),6,2) at x+100*(j-1),y+30*(i-1)
som=som+1
end if
next j
next i
if som<n*m then goto loop1
end def
def mat_inv (nvar,a(,),ainv(,))
dim w(7,14)
for i=1 TO nvar
for j=1 TO nvar
w(i,j)=a(i,j)
w(i,j+nvar)=0
next j
w(i,i+nvar)=1
next i
for piv=1 TO nvar
fac=w(piv,piv)
for j=piv TO piv+nvar
w(piv,j)=w(piv,j)/fac
next j
for i=1 TO nvar
IF i<>piv then
fac=w(i,piv)
for j=piv TO piv+nvar
w(i,j)=w(i,j)-fac*w(piv,j)
next j
endif
next i
next piv
for i=1 TO nvar
for j=1 TO nvar
ainv(i,j)=w(i,j+nvar)
next j
next i
end def
def mat_vec (n,m,mat(,),vin(),vuit())
for i=1 to n
tot=0
for j=1 to m
tot=tot+mat( i,j)*vin(j)
next j
vuit(i)=tot
next i
end def
def cls()
fill rect 0,0 to screen_width(),screen_height()
end def
rem
option base 1
dim mat(7,7),inv(7,7),x(7),rhs(7)
mat(1,1)=0
inv(1,1)=0
rhs(1)=0
x(1)=0
draw color 0,0,0
fill color .8,.8,.8
get_n:
input "Enter the number of equations (2 - 7)":n
if n<2 or n>7 then goto get_n
graphics
cls
orient:
if screen_width()<900 then
draw font size 40
draw text "please rotate to landscape" at 50,300
draw font size 20
goto orient
end if
cls
top=100
wide=99*n
hght=29+30*n
draw text "Enter the values of the coefficients" at 30,30
draw text "(press Return key to accept each value entry!)" at 30,60
win_open ("Coefficients",30,top,30+wide,top+hght)
draw text "x" at 35+wide,top+hght/2
win_open ("unknown",52+wide,top,160+wide,top+hght)
draw text "=" at 165+wide,top+hght/2
win_open ("RHS",182+wide,top,276+wide,top+hght)
mat_in (n,n,mat,35,top+30)
fill rect 30,30 to 500,50
draw text "Enter the values of the right hand side (RHS)" at 30,30
vec_in (n,rhs,187+wide,top+30)
rem End of user interface
mat_inv (n,mat,inv)
mat_vec (n,n,inv,rhs,x)
vec_out ("",n,x,55+wide,top+36,8,2)
end
def win_open (title$,xtop,ytop,xbot,ybot)
r=10
draw color 0,0,0
draw size 4
draw circle xtop,ytop to xtop+20,ytop+20
draw circle xbot-20,ytop to xbot,ytop+20
draw circle xtop,ybot-20 to xtop+20,ybot
draw circle xbot-20,ybot-20 to xbot,ybot
draw line xtop+r,ytop to xbot-r,ytop
draw line xtop+r,ybot to xbot-r,ybot
draw line xtop,ytop+r to xtop,ybot-r
draw line xbot,ytop+r to xbot,ybot-r
fill rect xtop+r,ytop+2 to xbot-r,ybot-2
fill rect xtop+2,ytop+r to xbot-2,ybot-r
if title$<>"" then
l=(xbot-xtop-12*len(title$))/2
draw line xtop,ytop+24 to xbot,ytop+24
draw color 0,0,1
draw text title$ at xtop+l,ytop-2
draw color 0,0,0
end if
end def
def n2a$(num,lang,dec)
b$=" "
fac=10^dec
num$=int(fac*num+.5)/fac
tot=lang-len(num$)
if tot<1 then tot=1
a$=substr$(b$,1,tot) & num$
n2a$=a$
end def
def vec_in (n,v(),x,y)
dim b$(20)
for i=1 to n
b$(i)=" a" & i
field b$(i) text b$(i) at x,y+30*(i-1) size 80,25
next i
som=0
loop:
for i=1 to n
if field_changed(b$(i)) then
v(i)=field_text$(b$(i))
field b$(i) delete
draw text n2a$(v(i),6,2) at x,y+30*(i-1)
som=som+1
end if
next i
if som<n then goto loop
end def
def vec_out (n$,n,v(),x,y,l,d)
ys=y-35
if n$<>"" then
draw text n$ at x,y
ys=ys+30
end if
for i=1 to n
a$=n2a$(v(i),l,d)
draw text a$ at x-12,ys+30*i
next i
end def
def mat_in (n,m,mat(,),x,y)
dim b$(100)
b$(1)=0
for i=1 to n
for j=1 to m
k=m*(i-1)+j
b$(k)=" a" & i & j
tt$=b$(k)
field tt$ text tt$ at x+100*(j-1),y+30*(i-1) size 80,25
next j
next i
som=0
loop1:
for i=1 to n
for j=1 to m
k=m*(i-1)+j
if field_changed(b$(k)) then
mat(i,j)=field_text$(b$(k))
field b$(k) delete
draw text n2a$(mat(i,j),6,2) at x+100*(j-1),y+30*(i-1)
som=som+1
end if
next j
next i
if som<n*m then goto loop1
end def
def mat_inv (nvar,a(,),ainv(,))
dim w(7,14)
for i=1 TO nvar
for j=1 TO nvar
w(i,j)=a(i,j)
w(i,j+nvar)=0
next j
w(i,i+nvar)=1
next i
for piv=1 TO nvar
fac=w(piv,piv)
for j=piv TO piv+nvar
w(piv,j)=w(piv,j)/fac
next j
for i=1 TO nvar
IF i<>piv then
fac=w(i,piv)
for j=piv TO piv+nvar
w(i,j)=w(i,j)-fac*w(piv,j)
next j
endif
next i
next piv
for i=1 TO nvar
for j=1 TO nvar
ainv(i,j)=w(i,j+nvar)
next j
next i
end def
def mat_vec (n,m,mat(,),vin(),vuit())
for i=1 to n
tot=0
for j=1 to m
tot=tot+mat( i,j)*vin(j)
next j
vuit(i)=tot
next i
end def
def cls()
fill rect 0,0 to screen_width(),screen_height()
end def