How to write cramer's rule 3x3 by matlab ?

1 Kommentar

James Tursa
James Tursa am 10 Mär. 2016
What have you done so far? Do you know how to replace elements in a matrix with other elements? Do you know how to use a for loop? Do you know how to calculate the determinant of a matrix?

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Explorer
Explorer am 10 Mär. 2016
Bearbeitet: Explorer am 10 Mär. 2016

5 Stimmen

Question: Find the system of Linear Equations using Cramers Rule:
2x + y + z = 3
x – y – z = 0
x + 2y + z = 0
The above example is taken from http://www.purplemath.com/modules/cramers.htm
% Demo Code:
A = [2 1 1; 1 -1 -1; 1 2 1] % Coefficient Matrix
X = [3; 0; 0]
Ax = [3 1 1 ; 0 -1 -1;0 2 1 ]
Ay = [2 3 1; 1 0 -1; 1 0 1]
Az = [2 1 3; 1 -1 0; 1 2 0]
detA=det(A)
x = det(Ax)/detA
y = det(Ay)/detA
z = det(Az)/detA

6 Kommentare

Batool Alfaraj
Batool Alfaraj am 10 Apr. 2018
How did you do the Ax, Ay, Az?
James Tursa
James Tursa am 10 Apr. 2018
By selectively replacing a column of A with X. Ax has 1st column replaced, Ay has 2nd column replaced, Az has third column replaced.
Robert Strunz
Robert Strunz am 15 Jan. 2021
Is there not a tider way of replacing columns in a matrix than this?
Robert Strunz
Robert Strunz am 15 Jan. 2021
Bearbeitet: Robert Strunz am 15 Jan. 2021
Is this a better solution,my matrix is called NAM, and the column vector is I ?
% Create a modified NAM for node b by inserting I into the first column.
mNAM_b = [I NAM(:,2:3)]
delta_b = det(mNAM_b)
VB = delta_b/delta_0
% Create a modified NAM for node c by inserting I into the second column.
mNAM_c = [NAM(:,1) I NAM(:,3)]
delta_c = det(mNAM_c)
VC = delta_c/delta_0
% Create a modified NAM for node e by inserting I into the third column.
mNAM_e = [NAM(:,1) NAM(:,2) I]
delta_e = det(mNAM_e)
VE = delta_e/delta_0
Rather than creating the modified matrix with concatenation, a direct assignment of the column:
k = the column number to replace
mNAM = NAM;
mNAM(:,k) = I;
This could easily be put into a loop.
Sebastián
Sebastián am 12 Mär. 2025
Bearbeitet: Sebastián am 12 Mär. 2025
This is my solution
%original matrix
x=[2 1 1;1 -1 -1;1 2 1]
res=[3 0 0]
detx=det(x)
sx=length(x)
ans=zeros(1,sx)
sistem=repmat(x,1,1,3);
%replace independent matrix in original matrix
for i=1:1:sx
sistem(:,i,i)=res;
ans(:,i)=det(sistem(:,:,i))/detx;
end
disp(ans)

Melden Sie sich an, um zu kommentieren.

Faith Ira Daro
Faith Ira Daro am 24 Nov. 2021

0 Stimmen

given the linear equations:
3x+2y = -5
-5x+7y=1
solve for the values of x and y using cramers rule
Asif Iqbal
Asif Iqbal am 28 Jan. 2022

0 Stimmen

% Demo Code:
A = [2 1 1; 1 -1 -1; 1 2 1] % Coefficient Matrix
X = [3; 0; 0]
Ax = [3 1 1 ; 0 -1 -1;0 2 1 ]
Ay = [2 3 1; 1 0 -1; 1 0 1]
Az = [2 1 3; 1 -1 0; 1 2 0]
detA=det(A)
x = det(Ax)/detA
y = det(Ay)/detA
z = det(Az)/detA

Kategorien

Mehr zu Linear Algebra finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 10 Mär. 2016

Bearbeitet:

am 12 Mär. 2025

Community Treasure Hunt

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

Start Hunting!

Translated by