→ selwyndd21:你那是pascal嗎,語法...... 05/07 19:24
Program simul3
c Prepare matrix for solving ndim < 100 dimensional simul. eq.
c aug=[a|b], when ax=b
implicit none
integer maxdim, ndim, pivot_c
parameter(maxdim=100)
real*8 aug(maxdim, maxdim+1)
ndim=3
aug(1,1)=4; aug(1,2)=1; aug(1,3)=-1; aug(1,4)=5
aug(2,1)=2; aug(2,2)=4; aug(2,3)=6; aug(2,4)=1
aug(3,1)=1; aug(3,2)=2; aug(3,3)=1; aug(3,4)=3
call displayMatrix(aug,ndim,ndim+1,maxdim,maxdim+1)
do pivot_c=1, ndim
call eliminatePivotColumn(aug,maxdim,maxdim+1,ndim,pivot_c)
call displayMatrix(aug,ndim,ndim+1,maxdim,maxdim+1)
end do
stop
end
c------------------------------------------------
Subroutine displayMatrix(matrix,m_show,n_show,m_max,n_max)
implicit none
integer m_show, n_show, m_max, n_max, i, j
real*8 matrix(m_max,n_max)
c Display the matrix
write(*,*) 'our matrix is now...'
do i=1,m_show
write(*,*) (matrix(i,j),j=1,n_show)
write(*,*)
end do
return
end
c------------------------------------------------
Subroutine eliminatePivotColumn(matrix,m_max, n_max, dim, col)
implicit none
integer col, pivot_r, i, j, m_max, n_max, dim
real*8 matrix(m_max, n_max), tmp(n_max)
c (1) Find the row with biggest coefficient (pivot row)
pivot_r=col
do i=pivot_r+1, dim
if(abs(matrix(i,col)).gt.abs(matrix(pivot_r,col))) then
pivot_r=i
endif
end do
c (2) Swap the rows
if(pivot_r.ne.col) then
do j=1, dim+1
tmp(j)=matrix(col,j)
matrix(col,j)=matrix(pivot_r,j)
matrix(pivot_r,j)=tmp(j)
end do
endif
c (3) Eliminate the column
tmp(1)=matrix(col,col)
do j=col, dim+1
matrix(col,j)=matrix(col,j)/tmp(1)
end do
do i=col+1,dim
tmp(1)=matrix(i,col)
do j=col,dim+1
matrix(i,j)=matrix(i,j)-tmp(1)*matrix(col,j)
end do
end do
return
end
我盡最大努力了
我投降
一開始就不該白目
初級程式設計只修過VB
居然直接跳級數值分析><
--
愛情就像棒球
一支三殺打
會讓前面兩支安打的成果
煙消雲散
要得分只有靠連續安打
或是全壘打
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.137.9.123