An example of using crammers rule or geuss elimination?

1 vue (au cours des 30 derniers jours)
Matlab Rahool
Matlab Rahool le 9 Mai 2019
Just looking for a good example

Réponse acceptée

Matlab Rahool
Matlab Rahool le 9 Mai 2019
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

Plus de réponses (1)

John D'Errico
John D'Errico le 9 Mai 2019
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.

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by