zoukankan      html  css  js  c++  java
  • (原创)列主元Gauss消去法的通用程序

     1 import numpy as np
     2 np.set_printoptions(precision=5)
     3 
     4 A = np.array([[31., -13., 0., 0., 0., -10., 0., 0., 0., -15.],  # 定义待求解方程组的增广矩阵
     5              [-13., 35., -9., 0., -11., 0., 0., 0., 0., 27.],
     6              [0., -9., 31., -10., 0., 0., 0., 0., 0., -23.],
     7              [0., 0., -10., 79., -30., 0., 0., 0., -9., 0.],
     8              [0., 0., 0., -30., 57., -7., 0., -5., 0., -20.],
     9              [0., 0., 0., 0., -7., 47., -30., 0., 0., 12.],
    10              [0., 0., 0., 0., 0., -30., 41., 0., 0., -7.],
    11              [0., 0., 0., 0., -5., 0., 0., 27., -2., 7.],
    12              [0., 0., 0., -9., 0., 0., 0., -2., 29., 10.]])
    13 
    14 [M, N] = np.shape(A)  # 得到系数矩阵的大小
    15 x = np.array([0.] * M)  # 初始化解向量,全0
    16 
    17 for j in range(0, M):  # 列主元Gauss消去
    18     max = A[j][j]
    19     max_i = j
    20     for i in range(j, M):  # 寻找主元
    21         if abs(A[i][j]) > max:
    22             max = abs(A[i][j])
    23             max_i = i
    24     temp_row = np.array(A[j])  # 交换
    25     A[j] = A[max_i]
    26     A[max_i] = temp_row
    27 
    28     for i in range(j + 1, M):  # 消去
    29         A[i] = A[i] - A[i][j] / A[j][j] * A[j]
    30 print("经列主元Gauss消去法得到的三角方程组的增广矩阵为:")
    31 print(A)
    32 for j in range(M - 1, -1, -1):  # 解三角方程组
    33     x[j] = (A[j][N - 1] - np.sum(A[j][0:M] * x)) / A[j][j]
    34 print("求解结果为:")
    35 print("x=", end="")
    36 print(x)
  • 相关阅读:
    Hibernate框架—简介
    ooad单例模式-Singleton
    About-JavaOOAD
    win10内置ubuntu,mysql完美清理,并安装
    VUE关于对象动态添加属性无法双向绑定问题
    正则留档
    centos下tomcat中文路径不识别
    web服务器解释html-include
    rose
    聊天框突出箭头
  • 原文地址:https://www.cnblogs.com/wt-seu/p/9799811.html
Copyright © 2011-2022 走看看