现在的位置: 首页 > 综合 > 正文

解线性方程组

2013年01月03日 ⁄ 综合 ⁄ 共 3569字 ⁄ 字号 评论关闭

<HTML>
<HEAD>
<title>线性方程组</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
input
{
 border:1px solid #618fff;
 width:40;
 overflow:visible;
}
button
{
 border:1px solid  #618fff;
 
 width:60;
 overflow:visible;
 margin-left:2;
 cursor:hand;
 font-size:12;
 filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#eaeaff', endColorStr='#618fff', gradientType='0');
}
body
{
 font-size:12;
 
}
</style>
</HEAD>

<BODY>
来源于:http://superdullwolf.cnzone.net/Function.html
<p id="ppp"></p>
 <input id="member" value="3"/>
 <button onclick="vbs:Make_n_Members_Function">元一次方程组 </button>
 <button onclick="vbs:GoGoGo">开始算</button>
 <SCRIPT LANGUAGE="vbScript">
 '*****超级大笨狼******
 '定义矩阵
dim matrix()
dim havaResult'是否有解
havaResult = true
Make_n_Members_Function '产生一个n元一次方程,默认是3元

  sub Make_n_Members_Function() 
  '******本过程产生一个n元一次方程,系数随机*****
  dim n
  
  if not isnumeric(member.value) then exit sub 'n必须是数字
  
  if  not cint(member.value)>1 then exit sub  '必须大于1
  
  if  not cint(member.value)<50 then exit sub  '必须小于50
  
  n = cint(cint(member.value))     '定义n元一次方程元数

  dim i,j,str

  redim matrix(n,n)
  
  dim quotiety         '系数
  
  str = ""
  for i=0 to n-1
   for j=0 to n-1
    quotiety = RRR()
    matrix(i,j) = quotiety
    str = str & "<input value='" & quotiety & "' name='L_" & i+1 & "_" & j+1 & "'/> "
    str = str &  "x" & j+1
    if j<>n-1 then
     str = str & " + "
    else
     quotiety = RRR()
     matrix(i,j+1) = quotiety
     str = str & " = " 
     str = str & "<input value='" & quotiety & "' name='R_"  & i+1 & "'/><br/>"  & vbcrlf
    end if
   next
  next
  ppp.innerHTML = str
  
  end sub
 
function RRR()
  '用来产生-9到9随即整数的函数
  randomize
  RRR =cint(rnd * 18)-9
end function
 
sub GoGoGo()
 '开始计算
 dim A(),B(),N
 N = ubound(matrix) 
 'N 是维数
 redim A(N,N)
 redim B(N)

 dim tempStr
  for each Arritem in  ppp.getElementsByTagName("INPUT")
   tempStr = Arritem.name
   if (left(tempStr,1)) ="L" then
   A(cint(split(tempStr,"_")(1)),cint(split(tempStr,"_")(2))) = cint(Arritem.value)
   else
   B(split(tempStr,"_")(1))=cint(Arritem.value)
   end if
  next 
 gaussj A,N,B
 if havaResult then
 show(B)
 else
 alert("无穷解")
 end if
end sub

sub gaussj(a(), n, b())
'高斯消元法。
    dim ipiv(50), indxr(50), indxc(50)
    for j = 1 to n
        ipiv(j) = 0
    next
    for i = 1 to n
        big = 0
        for j = 1 to n
            if ipiv(j) <> 1 then
                for k = 1 to n
                if ipiv(k) = 0 then
                    if abs(a(j, k)) >= big then
                        big = abs(a(j, k))
                        irow = j
                        icol = k
                    end if
                elseif ipiv(k) > 1 then
                    havaResult = false
     exit sub
                end if
                next
            end if
        next
        ipiv(icol) = ipiv(icol) + 1
        if irow <> icol then
            for l = 1 to n
                dum = a(irow, l)
                a(irow, l) = a(icol, l)
                a(icol, l) = dum
            next
            dum = b(irow)
            b(irow) = b(icol)
            b(icol) = dum
        end if
        indxr(i) = irow
        indxc(i) = icol
        if a(icol, icol) = 0 then
    havaResult = false
   exit sub
  end if
        pivinv = 1 / a(icol, icol)
        a(icol, icol) = 1
        for l = 1 to n
            a(icol, l) = a(icol, l) * pivinv
        next
        b(icol) = b(icol) * pivinv
        for ll = 1 to n
            if ll <> icol then
                dum = a(ll, icol)
                a(ll, icol) = 0
                for l = 1 to n
                    a(ll, l) = a(ll, l) - a(icol, l) * dum
                next
                b(ll) = b(ll) - b(icol) * dum
            end if
        next
    next
    for l = n to 1 step -1
        if indxr(l) <> indxc(l) then
            for k = 1 to n
                dum = a(k, indxr(l))
                a(k, indxr(l)) = a(k, indxc(l))
                a(k, indxc(l)) = dum
            next
        end if
    next
end sub
sub show(arr)
 '显示数组的全部内容
 dim str,a
 for each a in arr
  str = str &  a & vbcrlf
 next
 alert(str)
end sub
 </SCRIPT>
</BODY>
</HTML>
 

【上篇】
【下篇】

抱歉!评论已关闭.