MATLAB Using MATLAB to implement Cramer's Rule

AI Thread Summary
The discussion focuses on creating an m-file in MATLAB to compute a solution matrix using Cramer’s rule for any square coefficient matrix and its corresponding right-hand side vector. The main challenge is implementing a loop that allows the program to substitute the right-hand side vector for different columns in the coefficient matrix. A suggestion is made to use a for loop along with the determinant function to achieve this. A sample function, `cancelrowcol`, is provided, which demonstrates how to select and cancel specific rows and columns in the matrix, facilitating the calculation of determinants needed for Cramer’s rule. The function effectively handles different cases based on the position of the row and column being canceled.
.....
Messages
53
Reaction score
0
Hi all, I'm trying to write an m-file which will compute a solution matrix given a coefficient matrix and right hand side vector using cramer's rule.. which is no problem for a specified size matrix.. but is it possible to write an m-file which computes the solution when ANY square coefficient matrix and it's right hand side vector is entered?

I guess you would need some kind of loop... but I'm not sure how to write the code to have MATLAB substitute the right hand side vector for a different column in the co-efficient matrix for each term...

any suggestions?
 
Physics news on Phys.org
I am curious about how to do this also...

some ideas would be really helpful.
 
you just need for loop and det function to solve :|
 
here is a row/column selector the rest is looping and det function. r and c is the number of row/column that you cancel of the matrix A, Ac is the result that you get.

Code:
function [Ac] = cancelrowcol(A,r,c)
[n,m] = size(A);

% Shortcut to upper left and lower right corner...
if ((r == 1) && (c ==1))
    Ac = sel(A,2:n,2:m);
    return
elseif ((r == n) && (c == m))
    Ac = sel(A,1:n-1,1:m-1);
    return
% Otherwise
else
     Atemp = vertcat(sel(A,1:(r-1),1:m),sel(A,(r+1):n,1:m));
     Ac = horzcat(sel(Atemp,1:(r-1),1:(c-1)),sel(Atemp,1:(r-1),(c+1:m)));
end
end
 
Back
Top