An example of using crammers rule or geuss elimination?

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by