用MATLAB对高斯赛德尔迭代法仿真,怎样把解x1,x2,x3输出在同一坐标系中,怎样输出每一次迭代值.

1个回答

  • 你没把G-S方法迭代的流程图法出来,其实表4.2中的迭代数据是有问题的(少了一步).

    可以生成你所给图中数据的程序:

    A = [4 -1 1; 4 -8 1; -2 1 5];

    b = [7; -21; 15];

    x0 = zeros(3,1);

    eps=1.0e-7;

    D=diag(diag(A));%求A的对角矩阵

    L=-tril(A,-1);%求A的下三角阵

    U=-triu(A,1);%求A的上三角阵

    G=(D-L)U;

    f=(D-L)b;

    Y = [1 zeros(1,3)];

    y=G*x0+f;

    n=1;%迭代次数

    while norm(y-x0)>=eps

    x0=y;

    y=G*x0+f;

    n=n+1;

    Y(n,:) = [n y'];

    end

    % 生成图4.2

    plot(Y(:,1),Y(:,2:4))

    axis([1 12 0 5])

    grid on

    set(gca,'xtick',2:2:12,'ytick',0:5)

    % 输出表4.2

    fprintf('%it%.14ft%.14ft%.14fn',Y.')

    1 0.00000000000000 0.00000000000000 0.00000000000000

    2 1.87500000000000 3.93750000000000 2.96250000000000

    3 1.99375000000000 3.99218750000000 2.99906250000000

    4 1.99828125000000 3.99902343750000 2.99950781250000

    5 1.99987890625000 3.99987792968750 2.99997597656250

    6 1.99997548828125 3.99998474121094 2.99999324707031

    7 1.99999787353516 3.99999809265137 2.99999953088379

    8 1.99999964044189 3.99999976158142 2.99999990386047

    9 1.99999996443024 3.99999997019768 2.99999999173256

    10 1.99999999461628 3.99999999627471 2.99999999859157

    而正确的迭代过程应该是这样的(图形及数据输出部分相同,略):

    A = [4 -1 1; 4 -8 1; -2 1 5];

    b = [7; -21; 15];

    x0 = zeros(3,1);

    eps=1.0e-7;

    D=diag(diag(A));%求A的对角矩阵

    L=-tril(A,-1);%求A的下三角阵

    U=-triu(A,1);%求A的上三角阵

    G=(D-L)U;

    f=(D-L)b;

    % Y = [1 zeros(1,3)];

    n=1;%迭代次数

    while true

    Y(n,:) = [n x0'];

    y=G*x0+f;

    if norm(y-x0)

    n=n+1;

    x0=y;

    end

    输出数据如下(比你给的表格中多了第二行):

    1 0.00000000000000 0.00000000000000 0.00000000000000

    2 1.75000000000000 3.50000000000000 3.00000000000000

    3 1.87500000000000 3.93750000000000 2.96250000000000

    4 1.99375000000000 3.99218750000000 2.99906250000000

    5 1.99828125000000 3.99902343750000 2.99950781250000

    6 1.99987890625000 3.99987792968750 2.99997597656250

    7 1.99997548828125 3.99998474121094 2.99999324707031

    8 1.99999787353516 3.99999809265137 2.99999953088379

    9 1.99999964044189 3.99999976158142 2.99999990386047

    10 1.99999996443024 3.99999997019768 2.99999999173256