日期:2014-05-16 浏览次数:20368 次
<!DOCTYPE html>
<html>
<head>
<title>习题2-矩阵LU分解</title>
</head>
<body>
<p id="myA">原矩阵为</p>
<p id="resultL">L为</p>
<P id="resultU">U为</P>
<script>
//输入数组
var myMatrix = [
[2,2,3],
[4,7,7],
[-2,4,5]
];
var myLength = myMatrix.length;
var l= new Array(myLength);
var u = new Array(myLength);
//计算
for(j=0;j<myLength;j++){
u[0][j] = myMatrix[0][j];//报错行
}
for(k=1;k<myLength;k++){
l[k][0] = myMatrix[k][0]/u[0][0];
}
for(i=1;i<myLength;i++){
//求Uij
for(j=i;j<myLength;j++){
//求和Lit*Utj
for(t=0;t<i-1;t++){
var tmp =0;
tmp = tmp + l[i][t]*u[t][j];
}
u[i][j] = a[i][j]-tmp;
}
//求Lki
for(k=i+1;k<myLength;k++){
//
for(t2=0;t2<i-1;t++){
var tmp2 = 0;
tmp2 = tmp2 + l[k][t2]*u[t2][j];
}
l[k][i] = 1/u[i][i]*(a[k][i] - tmp2);
}
}
document.getElementById("myA").innerHTML = myMatrix;
document.getElementById("resultL").innerHTML = l;
document.getElementById("resultU").innerHTML = u;
</script>
</body>
</html>