早教吧 育儿知识 作业答案 考试题库 百科 知识分享

Gauss消元法解2X1+x2+x3=4,3x1+X2+2X3=6,x1+2x2+2x3=5,用fortran95

题目详情
Gauss消元法解2X1+x2+x3=4,3x1+X2+2X3=6,x1+2x2+2x3=5,用fortran95
▼优质解答
答案和解析
结果均为1
---------------------------------------------------------
program main
implicit none integer :: i, j, k, n real :: temp real :: B(3,4) real :: x(3),coe data ((B(i,j),j=1,4),i=1,3) /2, 1, 1, 4, 3, 1, 2, 6, 1, 2, 2, 5/ n=size(B,1) write(*,*)"before gauss elimination" do i=1,n write(*,*)B(i,:) end do write(*,*)n ! 消元过程 do k=1,n-1 !共需要处理n-1步,每步主元在k行k列 do i=k+1,n !第k+1行至n行的消去 coe=B(i,k)/B(k,k) B(i,k:n+1)=B(i,k:n+1)-coe*B(k,k:n+1) end do end do write(*,*)"after gauss elimination" do i=1,n write(*,*)B(i,:) end do x(n)=B(n,n+1)/B(n,n) ! 回代过程 do k=n-1,1,-1 x(k)=( B(k,n+1) - dot_product(B(k,k+1:n),x(k+1:n)) ) / B(k,k) end do write(*,*)"result:" write(*,*)xend program main-------------------------------------------------------
运行结果如下:
before gauss elimination
2.000000 1.000000 1.000000 4.000000 3.000000 1.000000 2.000000 6.000000 1.000000 2.000000 2.000000 5.000000 3 after gauss elimination 2.000000 1.000000 1.000000 4.000000 0.0000000E+00 -0.5000000 0.5000000 0.0000000E+00 0.0000000E+00 0.0000000E+00 3.000000 3.000000 result: 1.000000 1.000000 1.000000