Matlab:高斯消除函数 [英] Matlab: Gauss Elimination Function

查看:91
本文介绍了Matlab:高斯消除函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function x = Gauss_Elimination(A,b)

n = length(b);
x = zeros(n,1);

% Forward Elimination
for i = 1:n-1 

    for j = i+1:n

        mul = A(j,i)/A(i,i); % Multiplier

        for k = i+1:n

            A(j,k) = A(j,k) - mul*A(i,k);

        end

        b(j) = b(j) - mul*b(i);

    end

end

x(n) = b(n)/A(n,n); % Obtain solution for the last variable

% Back Substitution
for i = n-1:-1:1

    sum = b(i);

    for j = i+1:n

        sum = sum - A(i,j)*x(j);

    end

    x(i) = sum/A(i,i);

end

当A = [0 1 1 1; 3 0 3 -4; 1 1 1 2; 2 3 1 3]; b = [0; 7; 6; 6]; x = [4; -3; 1; 2]

When A=[0 1 1 1;3 0 3 -4;1 1 1 2;2 3 1 3]; b=[0;7;6;6]; x=[4;-3;1;2]

但是,当我使用此功能时,x = [NAN; NAN; NAN; NAN].

However, x=[NAN;NAN;NAN;NAN] when I utilize this function.

有人告诉我原因吗?

推荐答案

尝试使用断点在每次迭代时查看变量的值. 问题是变量"mul",您正在划分某物/0->无穷大.这就是为什么您获得这些结果的原因.如果您设置

Try using breakpoints to see the value of the variables at each iteration. The problem is the variable "mul", you are dividing something/0->infinite. That is why you are getting those results. If you set

A = [2 1 1 1; 3 4 3 -4; 1 1 1 2; 2 3 1 3]; b = [0; 7; 6; 6]

A=[2 1 1 1;3 4 3 -4;1 1 1 2;2 3 1 3]; b=[0;7;6;6]

您得到的答案是: ans =

you get as answer: ans =

-4.7273 1.7273 6.4545 1.2727

-4.7273 1.7273 6.4545 1.2727

希望它会有所帮助.

这篇关于Matlab:高斯消除函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆