• Embed Doc
  • Readcast
  • Collections
  • 1
    CommentGo Back
Download
 
數值分析第五次作業
LU Decomposition
組別: 第六組學生: 陳器奇、謝鎮宇學號:9301062a、9301035a程式碼(使用C語言):#include <stdio.h>#include <stdlib.h>#include <math.h>#define dim 10//(1)double a[dim][dim], L[dim][dim], U[dim][dim], x[dim], b[dim], bp[dim];//說明(2)mprint( int , int , double [][dim] , double [] );//(3)main(){int n,i,j,k;//(4)doubletemp;printf( "\nInput the dimension of Matrix: " );//(5)scanf( "%d",&n );if(n>=dim)//(6){printf( "Dimension over preset value %d\n",dim );exit(0);}printf( "\n Input the a matrix\n\n" );for(i=1; i<=n; i++){for(j=1;j<=n;j++){printf( "Input the element of a[%d,%d]: ",i,j );//(7)scanf( "%lf",&a[i][j] );}}printf( "\n Input the b vector\n\n" );for(i=1; i<=n ; i++){printf( "input the value of b[%d]: ",i );//(8)scanf( "%lf",&b[i] );
 
}mprint(n,n,a,b);//(9)/*LU*/for(i=0; i<dim; i++){for(j=0; j<dim;j++){L[i][j]=0.0; U[i][j]=0.0;//(10)}}for(i=1; i<=n; i++) { L[i][1]=a[i][1]; }//(11)for(j=1; j<=n; j++) { U[1][j]=a[1][j]/L[1][1]; }for(j=2; j<=n; j++){for(i=j; i<=n;i++){temp=0.0;for(k=1;k<=j-1;k++){temp=temp+L[i][k]*U[k][j];}L[i][j]=a[i][j]-temp;}U[j][j]=1.0;//(12)for(i=j+1;i<=n;i++)//(13){temp=0.0;for(k=1; k<=j-1;k++){temp=temp+L[j][k]*U[k][i];}U[j][i]=(a[j][i]-temp)/L[j][j];}}/* Backward substitution
(反代法)
*/bp[1]=b[1]/L[1][1];//(14)for(i=2; i<=n; i++){temp=0.0;for(k=1; k<=i-1;k++){temp=temp+L[i][k]*bp[k];}bp[i]=(b[i]-temp)/L[i][i];
 
}x[n]=bp[n];for(j=n-1; j>=1; j--)//(15){temp=0.0;for(k=j+1; k<=n; k++){temp=temp+U[j][k]*x[k];}x[j]=bp[j]-temp;}mprint(n,n,L,b);//(16)mprint(n,n,U,bp);for(j=1; j<=n; j++)//(17){printf(" x[%d]=%10.5lf\n",j,x[j]);}}/**//* */mprint(int m,int n,double aa[][dim],double bb[])//(18){int i,j;for(i=1; i<=m; i++){for(j=1; j<=n;j++){printf(" %10.5e ",aa[i][j]);}printf(" %10.5e \n",bb[i]);}}程式說明:LU Decomposition定義:
將一個矩陣
A
分解成上三角矩陣
U
與下三角矩陣
L。
解法方程式為
Ax = b=>LUx = b設定Ux=b',求x,由下列兩個方程式求解:Lb' = bUx = b'先算b',再代回算出x
(反代法)
矩陣的
LU
分解
(
以四乘四為例
)
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
05 / 21 / 2011This doucment made it onto the Rising List!
You must be to leave a comment.
Submit
Characters: ...