An example of using crammers rule or geuss elimination?

1 回表示 (過去 30 日間)
Matlab Rahool
Matlab Rahool 2019 年 5 月 9 日
回答済み: John D'Errico 2019 年 5 月 9 日
Just looking for a good example

採用された回答

Matlab Rahool
Matlab Rahool 2019 年 5 月 9 日
Crammers
A = [ ...];
b = [...]';
[nr,nc] = size(A);
x = cram_rule(A,b);
fprintf('****************\n')
for i = 1:nr
fprintf(' x(%d) = %5.3f \n',i,x(i))
end
fprintf('****************\n')
function x = cram_rule(A,b)
[nr,nc] = size(A);
DA = det(A);
for i = 1:nr
C = A;
C(:,i) = b;
x(i) = det(C)/DA;
end
Geuss
C = ...;
[nr nc] = size(C);
xn = [1:nr]';
[xn X] = gauss_elim(nr,nc,xn,C);
fprintf('****************\n')
for i = 1:nr
fprintf(' x(%d) = %5.3f\n',xn(i),X(i))
end
fprintf('****************\n')
function [xn X] = gauss_elim(nr,nc,xn,C)
% Forward Elimination
for j = 1:nr-1
for i = j+1:nr %row 1 pivot row
C(i,:) = C(i,:) - (C(i,j)/C(j,j))*C(j,:);
end
end
% Back Substitution
for i = nr:-1:1
sm = 0;
for j = nr:-1:i+1
sm = sm +X(j)*C(i,j);
end
X(i) = [C(i,nc) - sm]/C(i,i);
end
end

その他の回答 (1 件)

John D'Errico
John D'Errico 2019 年 5 月 9 日
Don't use either of them! There are many examples of things that you might learn to use when doing homework, and in class as a student. However, the fact is you don't ever want to write that code. In virtually all cases you will be better off using the provided software to solve systems of equations.
Simple rule: NEVER write your own code to do numerical methods when code is already provided. Unless of course, you are capable of writing that same code at a fully professional level. If you need to ask this question, then you are not there.

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by