QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2513|回复: 0
打印 上一主题 下一主题

用稀疏矩阵解决联立方程组

[复制链接]
字体大小: 正常 放大
回帖奖励 1 点体力 回复本帖可获得 1 点体力奖励! 每人限 1 次

413

主题

36

听众

1854

积分

升级  85.4%

  • TA的每日心情
    开心
    2019-9-18 21:55
  • 签到天数: 258 天

    [LV.8]以坛为家I

    社区QQ达人

    群组2015国赛冲刺

    群组2016美赛公益课程

    群组国赛讨论

    群组第三届数模基础实训

    群组Matlab讨论组

    跳转到指定楼层
    1#
    发表于 2015-9-27 22:24 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    用稀疏矩阵解决联立方程组
    为了解说明稀疏矩阵在MATLAB中应用,我们将用全矩阵和稀疏矩阵来解决下面的联立方程组。
    1.0x1 + 0.0x2 + 1.0x3+ 0.0x4 + 0.0x5 + 2.0x6 + 0.0x7- 1.0x8 = 3.0
    0.0x1 + 1.0x2 + 0.0x3+ 0.4x4 + 0.0x5 + 0.0x6 + 0.0x7+ 0.0x8 = 2.0
    0.5x1 + 0.0x2 + 2.0x3+ 0.0x4 + 0.0x5 + 0.0x6 - 1.0x7+ 0.0x8 = -1.5
    0.0x1 + 0.0x2 + 0.0x3+ 2.0x4 + 0.0x5 + 1.0x6 + 0.0x7+ 0.0x8 = 1.0
    0.0x1 + 0.0x2 + 1.0x3+ 1.0x4 + 1.0x5 + 0.0x6 + 0.0x7+ 0.0x8 = -2.0
    0.0x1 + 0.0x2 + 0.0x3+ 1.0x4 + 0.0x5 + 1.0x6 + 0.0x7+ 0.0x8 = 1.0
    0.5x1 + 0.0x2 + 0.0x3+ 0.0x4 + 0.0x5 + 0.0x6 + 1.0x7+ 0.0x8 = 1.0
    0.0x1 + 1.0x2 + 0.0x3+ 0.0x4 + 0.0x5 + 0.0x6 + 0.0x7+ 1.0x8 = 1.0


    答案
    为了解决这一问题,我们将创建一个方程系数的全矩阵,并用sparse函数把他转化为稀疏矩阵。我们用两种方法解这个方程组,比较它们的结果和所需的内存。
    代码如下:
    % Script file: simul.m
    %
    % Purpose:
    % This program solves a system of 8 linear equations in 8
    % unknowns (a*x = b), using both full and sparse matrices.
    %
    % Record of revisions:
    % Date      Programmer      Description of change
    % ====      ========      ===============
    % 10/14/98  S. J. Chapman     Originalcode
    %
    % Define variables:
    % a                --Coefficients of x (full matrix)
    % as               --Coefficients of x (sparse matrix)
    % b                 --Constantcoefficients (full matrix)
    % bs                --Constantcoefficients (sparse matrix)
    % x                 --Solution(full matrix)
    % xs                --Solution(sparse matrix)
    % Define coefficients of the equation a*x = b for
    % the full matrix solution.
    a = [
    1.0 0.0 1.0 0.0 0.0 2.0 0.0 -1.0; ...
    0.0 1.0 0.0 0.4 0.0 0.0 0.0 0.0; ...
    0.5 0.0 2.0 0.0 0.0 0.0 -1.0 0.0; ...
    0.0 0.0 0.0 2.0 0.0 1.0 0.0 0.0; ...
    0.0 0.0 1.0 1.0 1.0 0.0 0.0 0.0; ...
    0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0; ...
    0.5 0.0 0.0 0.0 0.0 0.0 1.0 0.0; ...
    0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0
    ];
    b = [ 3.0 2.0 -1.5 1.0 -2.0 1.0 1.0 1.0]';
    % Define coefficients of the equation a*x = b for
    % the sparse matrix solution.
    as = sparse(a);
    bs = sparse(b);
    % Solve the system both ways
    disp ('Full matrix solution:');
    x = a\b
    disp ('Sparse matrix solution:');
    xs = as\bs
    % Show workspace
    disp('Workspace contents after the solutions:')
    whos

    运行这个程序,结果如下
    >> simul
    Full matrix solution:
    x =
        0.5000
        2.0000
       -0.5000
       -0.0000
       -1.5000
        1.0000
        0.7500
       -1.0000
    Sparse matrix solution:
    xs =
       (1,1)       0.5000
       (2,1)       2.0000
       (3,1)      -0.5000
       (5,1)      -1.5000
       (6,1)       1.0000
       (7,1)       0.7500
       (8,1)      -1.0000
    Workspace contents after the solutions:
      Name      Size                    Bytes  Class
      a         8x8                       512  double array
      as        8x8                       276  double array (sparse)
      b         8x1                        64  double array
      bs        8x1                       104  double array (sparse)
      x         8x1                        64  double array
      xs        8x1                        92  double array (sparse)
    Grand total is 115 elements using 1112 bytes
    两种算法得到了相同的答案。注意用稀疏矩阵产生的结果不包含x4,因为它的值为0。注意b的稀疏形式占的内存空间比全矩阵形式还要大。这种情况是因为稀疏矩阵除了元素值之外必须存储它的行号和列号,所以当一个矩阵的大部分元素都是非零元素,用稀疏矩阵将降低运算效率。


    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    数学中国版主团队!
    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2025-5-21 05:16 , Processed in 0.319686 second(s), 53 queries .

    回顶部