您的位置:程序门 -> vb -> 网络编程



求助:用高斯-塞德尔迭代法求解线形方程组


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求助:用高斯-塞德尔迭代法求解线形方程组
发表于:2007-06-22 22:46:39 楼主
方程组:      
                                        10*x1-x2+2*x3=6      
                                        -x1+11*x2-x3+3*x4=25      
                                {       2*x1-x2+10*x3-x4=-11      
                                        3*x2-x3+8*x4=15
发表于:2007-06-23 11:11:151楼 得分:0
'以前的作业,矩阵乘法,改一下就是解方程了把。
'用定义做的,所以如果矩阵太大会死敲敲。
'保存为*.hta,点击运行,vbs和vb完全一样语法。

<head>
<meta   http-equiv= "content-type "   content= "text/html;   charset=gb2312 ">
<meta   name= "generator "   content= "microsoft   frontpage   4.0 ">
<meta   name= "progid "   content= "frontpage.editor.document ">
<style>
span{font-size:10pt;   color:blue;}
.cssmatrixitem{width:30;   height:20;   border:1   solid   white;   background:menu;}
</style>
</head>

<body>
<span   id= "htmmatrixa "   style= "border:2   solid   menu;   background:menu; ">
<span> 定义矩阵a </span>
<select   id= "row "   onchange= "matrixstructurechange() ">
<option   value= "1 "> 1 </option> <option   value= "2 "> 2 </option> <option   value= "3 "> 3 </option> <option   value= "4 "> 4 </option> <option   value= "5 "> 5 </option> <option   value= "6 "> 6 </option> <option   value= "7 "> 7 </option>
</select>
<select   id= "column "   onchange= "matrixstructurechange() "   >
<option   value= "1 "> 1 </option> <option   value= "2 "> 2 </option> <option   value= "3 "> 3 </option> <option   value= "4 "> 4 </option> <option   value= "5 "> 5 </option> <option   value= "6 "> 6 </option> <option   value= "7 "> 7 </option>
</select>
</span   >
&nbsp;
<span   id= "htmmatrixb "   style= "border:2   solid   menu;   background:menu; ">
<span> 定义矩阵b </span>
<select   id= "row "   disabled>
<option   value= "1 "> 1 </option> <option   value= "2 "> 2 </option> <option   value= "3 "> 3 </option> <option   value= "4 "> 4 </option> <option   value= "5 "> 5 </option> <option   value= "6 "> 6 </option> <option   value= "7 "> 7 </option>
</select>
<select   id= "column "   onchange= "matrixstructurechange() ">
<option   value= "1 "> 1 </option> <option   value= "2 "> 2 </option> <option   value= "3 "> 3 </option> <option   value= "4 "> 4 </option> <option   value= "5 "> 5 </option> <option   value= "6 "> 6 </option> <option   value= "7 "> 7 </option>
</select>
</span   >
&nbsp;=&nbsp;
<span   id= "htmmatrixc "   style= "border:2   solid   menu;   background:menu; ">
<span> 结果矩阵c </span>
<select   id= "row "   disabled>
<option   value= "1 "> 1 </option> <option   value= "2 "> 2 </option> <option   value= "3 "> 3 </option> <option   value= "4 "> 4 </option> <option   value= "5 "> 5 </option> <option   value= "6 "> 6 </option> <option   value= "7 "> 7 </option>
</select>
<select   id= "column "   disabled>
<option   value= "1 "> 1 </option> <option   value= "2 "> 2 </option> <option   value= "3 "> 3 </option> <option   value= "4 "> 4 </option> <option   value= "5 "> 5 </option> <option   value= "6 "> 6 </option> <option   value= "7 "> 7 </option>
</select>
</span   >
&nbsp;
<input   type= "button "   value= "   生成矩阵   "   onclick= "generatematrixstructure() ">
<hr>
<table   >
<tr>
<td   style= "font-size:30pt; "> ( </td>
<td> <span   id= "htmmatrixainput "> a </span> </td>
<td   style= "font-size:30pt; "> )x( </td>
<td> <span   id= "htmmatrixbinput "> b </span> </td>
<td   style= "font-size:30pt; "> )=( </td>
<td> <span   id= "htmmatrixcinput "> c </span> </td>
<td   style= "font-size:30pt; "> ) </td>
</tr>
</table>

<hr>
<input   value= "   计   算   "   type=button   onclick= "caculatematrix() ">
</body>


<script   language=vbs>
option   explicit
sub   document_onreadystatechange()
generatematrixstructure
end   sub

sub   matrixstructurechange()
htmmatrixb.all( "row ").value=htmmatrixa.all( "column ").value
htmmatrixc.all( "row ").value=htmmatrixa.all( "row ").value
htmmatrixc.all( "column ").value=htmmatrixb.all( "column ").value
generatematrixstructure
end   sub

sub   generatematrixstructure()
dim   imatrixarow,imatrixbrow,imatrixcrow,imatrixacolumn,imatrixbcolumn,imatrixccolumn
imatrixarow=htmmatrixa.all( "row ").value
imatrixbrow=htmmatrixb.all( "row ").value
imatrixcrow=htmmatrixc.all( "row ").value
imatrixacolumn=htmmatrixa.all( "column ").value
imatrixbcolumn=htmmatrixb.all( "column ").value
imatrixccolumn=htmmatrixc.all( "column ").value

dim   irow,icolumn,strtmp

strtmp= " "
for   irow=1   to   imatrixarow
for   icolumn=1   to   imatrixacolumn
strtmp=strtmp   &   " <input   maxlength=3   class= 'cssmatrixitem '   value=0   onchange= 'checkitem() '   id= 'item "   &   irow   &   icolumn   &   " '> </input> "
next
strtmp=strtmp   &   " <br> "
next
htmmatrixainput.innerhtml=strtmp

strtmp= " "
for   irow=1   to   imatrixbrow
for   icolumn=1   to   imatrixbcolumn
strtmp=strtmp   &   " <input   maxlength=3   class= 'cssmatrixitem '   value=0   onchange= 'checkitem() '   id= 'item "   &   irow   &   icolumn   &   " '> </input> "
next
strtmp=strtmp   &   " <br> "
next
htmmatrixbinput.innerhtml=strtmp

strtmp= " "
for   irow=1   to   imatrixcrow
for   icolumn=1   to   imatrixccolumn
strtmp=strtmp   &   " <input   readonly   class= 'cssmatrixitem '   style= 'width:40; '   onchange= 'checkitem() '   value=0   id= 'item "   &   irow   &   icolumn   &   " '> </input> "
next
strtmp=strtmp   &   " <br> "
next
htmmatrixcinput.innerhtml=strtmp
end   sub

sub   checkitem()
if   not(isnumeric(window.event.srcelement.value))   then  
window.event.srcelement.value=0
else
window.event.srcelement.value=cint(window.event.srcelement.value)
end   if

end   sub

sub   caculatematrix()
dim   imatrixarow,imatrixbrow,imatrixcrow,imatrixacolumn,imatrixbcolumn,imatrixccolumn
imatrixarow=htmmatrixa.all( "row ").value
imatrixbrow=htmmatrixb.all( "row ").value
imatrixcrow=htmmatrixc.all( "row ").value
imatrixacolumn=htmmatrixa.all( "column ").value
imatrixbcolumn=htmmatrixb.all( "column ").value
imatrixccolumn=htmmatrixc.all( "column ").value

redim   aryimatrixa(imatrixarow-1,imatrixacolumn-1)
redim   aryimatrixb(imatrixbrow-1,imatrixbcolumn-1)
redim   aryimatrixc(imatrixcrow-1,imatrixccolumn-1)

getmatrixinputvalue   "htmmatrixa ",aryimatrixa
getmatrixinputvalue   "htmmatrixb ",aryimatrixb
resetmatrixinputvalue   "htmmatrixc ",aryimatrixc
matrixmultiply   aryimatrixa,aryimatrixb,aryimatrixc

letmatrixinputvalue     "htmmatrixc ",aryimatrixc

end   sub

function   matrixmultiply(byref   iarya,byref   iaryb,byref   iaryc)
'计算矩阵尺寸
dim   iamaxrow,iamaxcolumn,ibmaxrow,ibmaxcolumn,icmaxrow,icmaxcolumn
iamaxrow=ubound(iarya,1)+1
iamaxcolumn=ubound(iarya,2)+1

ibmaxrow=ubound(iaryb,1)+1
ibmaxcolumn=ubound(iaryb,2)+1

icmaxrow=ubound(iaryc,1)+1
icmaxcolumn=ubound(iaryc,2)+1

'判定矩阵是否符合乘法要求
if   iamaxrow <1   or   iamaxcolumn <1   or   ibmaxrow <1   or   ibmaxcolumn <1   or   icmaxrow <1   or   icmaxcolumn <1   then
matrixmutiply=false
exit   function
end   if
if   iamaxcolumn <> ibmaxrow   or   icmaxrow <> iamaxrow   or   icmaxcolumn <> ibmaxcolumn   then  
matrixmutiply=false
exit   function
end   if

'计算
dim   iccolumn,icrow,iacolumn
dim   itmp

for   icrow=0   to   icmaxrow-1
for   iccolumn=0   to   icmaxcolumn-1
itmp=0
for   iacolumn=0   to   iamaxcolumn-1
itmp=itmp   +   iarya(icrow,iacolumn)   *   iaryb(iacolumn,iccolumn)
next
iaryc(icrow,iccolumn)=itmp
next
next

matrixmultiply=true
end   function  

sub   getmatrixinputvalue(strhtmmatrixid,byref   iarymatrix)
dim   imatrixrow,imatrixcolumn
imatrixrow=document.all(strhtmmatrixid).all( "row ").value
imatrixcolumn=document.all(strhtmmatrixid).all( "column ").value

dim   irow,icolumn
for   irow=1   to   imatrixrow
for   icolumn=1   to   imatrixcolumn
iarymatrix(irow-1,icolumn-1)=cint(document.all(strhtmmatrixid& "input ").all( "item "&cstr(irow)&cstr(icolumn)).value)
next
next
end   sub

sub   letmatrixinputvalue(strhtmmatrixid,byref   iarymatrix)
dim   imatrixrow,imatrixcolumn
imatrixrow=document.all(strhtmmatrixid).all( "row ").value
imatrixcolumn=document.all(strhtmmatrixid).all( "column ").value

dim   irow,icolumn
for   irow=1   to   imatrixrow
for   icolumn=1   to   imatrixcolumn
document.all(strhtmmatrixid& "input ").all( "item "&cstr(irow)&cstr(icolumn)).value=iarymatrix(irow-1,icolumn-1)
next
next
end   sub

sub   resetmatrixinputvalue(strhtmmatrixid,byref   iarymatrix)
dim   imatrixrow,imatrixcolumn
imatrixrow=document.all(strhtmmatrixid).all( "row ").value
imatrixcolumn=document.all(strhtmmatrixid).all( "column ").value

dim   irow,icolumn
for   irow=1   to   imatrixrow
for   icolumn=1   to   imatrixcolumn
iarymatrix(irow-1,icolumn-1)=0
next
next
end   sub
</script>



快速检索

最新资讯
热门点击