Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Using MATLAB to implement Cramer's Rule

  1. May 29, 2005 #1
    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?
  2. jcsd
  3. Oct 7, 2009 #2
    I am curious about how to do this also...

    some ideas would be really helpful.
  4. Oct 7, 2009 #3
    you just need for loop and det function to solve :|
  5. Oct 8, 2009 #4
    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 (Text):

    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);
    elseif ((r == n) && (c == m))
        Ac = sel(A,1:n-1,1:m-1);
    % Otherwise
         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)));

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook