码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【国科大——矩阵分析与应用】使用高斯消元法,测试二元一次方程系数产生的误差


    文章目录

    • 使用高斯消元法,测试二元一次方程系数产生的误差
      • 原始方程
      • 误差来源
      • 保留有效数字
      • 代码

    使用高斯消元法,测试二元一次方程系数产生的误差

    原始方程

    { 47 x + 28 y = 19 89 x + 53 y = 36 \begin{equation} % equation带*之后的意思不整体标号 \begin{cases} 47x +28y=19\\ 89x+53y=36 \end{cases} \end{equation} {47x+28y=1989x+53y=36​​​

    误差来源

    对于上述方程组来说,肉眼可解:x=1,y=-1

    然而,高斯消元会出现分数 89 47 \frac{89}{47} 4789​ 。 由于计算机的精度不同,会出现误差,下面测试不同的精度导致使用 高斯消去法 得到的结果不同。

    保留有效数字

    print(float(format(52.91, '.3g')))  # 保留3位有效数字
    
    • 1

    代码

    import numpy as np
    from numpy import *
    
    
    def Gauss(Mat, Bais, num):
        if (Mat.shape[0] != Mat.shape[1]) or (Mat.shape[0] != Bais.shape[0]):
            raise ValueError("size of matrix doesn't match!")
        A = Mat.copy()
        B = Bais.copy()
        n = A.shape[0]  # n=2
        arg = '.' + str(num) + 'g'
        l_ik = float(format(A[1, 0] / A[0, 0], arg))
        # print(l_ik)
    
        a1 = float(format(A[0, 0], arg))
        b1 = float(format(A[0, 1], arg))
        a2 = float(format(A[1, 0], arg))
        b2 = float(format(A[1, 1], arg))
        c1 = float(format(B[0, 0], arg))
        c2 = float(format(B[1, 0], arg))
        b2 = float(format(b2 - float(format(l_ik * b1, arg)), arg))
        # print('b2', b2)
        c2 = float(format(c2 - float(format(l_ik * c1, arg)), arg))
        y = float(format(c2 / b2, arg))
        # print(y)
        c1 = float(format(c1 * l_ik, arg))
        b1 = float(format(b1 * l_ik * y, arg))
        # print('b1',b1)
        # print('c1',c1)
        temp = float(format(c1 - b1, arg))
        # print('temp:',temp)
        x = float(format(temp / a2, arg))
        # print('x:',x)
        return [x, y]
    
    
    if __name__ == '__main__':
        x = np.mat([[47, 28],
                    [89, 53]])
        y = np.mat([[19],
                    [36]])
        print(Gauss(x, y, 4))
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    环形队列结构
    spring boot中的标注@Component、@Service等
    Vue3组件库打包指南,一次生成esm、esm-bundle、commonjs、umd四种格式
    GraalVM + Springboot3 + IDEA 在widow10 上完成构建本地化服务
    气传导和骨传导耳机哪个好?骨传导耳机优点更多
    Cryptographic primitives(密码原语)
    牛客网——verilog练习题思路汇总
    关于飞桨UIE等模型预测推理时间很久的问题分析以及解决,蒸馏剪枝部署问题解决
    db_ha执行ha_isready报错authentication method 13 not supported
    Windows保姆级安装Docker教程
  • 原文地址:https://blog.csdn.net/qq_44824148/article/details/126634200
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号