-2

When I run this Laplace expansion in Matlab for determinant, I do get "Undefined function or variable 'A'". I will be happy if anyone can help me correct it and make it workable.

 function value = Laplace
   A=input('matrix A =');
   [rows, columns] = size(A);
if rows == 2
   for i = 1:rows
    value = A(1,1)*A(2,2) - A(1,2)*A(2,1);
   end
 else
   if rows==3:size(A);
   for i = 1:rows
   columnIndices = [1:i-1 i+1:rows];
 value = value + (-1)^(i+1)*A(1,i)*...*Laplace(A(2:rows, columnIndices));
  end
 end

Thanks

babs
  • 31
  • 6
  • 1
    Please provide a [mcve] and the *full* error message. – sco1 Jul 18 '16 at 15:50
  • @excaza I have provided the error message. Also all Matlab codes for Laplace expansion I have gotten online do not work. I can create a workable code for order 2,3 and 4 but I couldn't generalize it for higher dimension.Thanks – babs Jul 18 '16 at 16:06
  • @beaker thank you I won't do that again – babs Jul 18 '16 at 16:11
  • Your question is "how to make it workable" not "how to generalize for higher dimension." If you want help making your current code workable then your current code needs to be valid MATLAB code, which this isn't. Your logic statements are not all terminated correctly and your line continuation (`...`) prevents the code from functioning properly. – sco1 Jul 18 '16 at 16:25

1 Answers1

0

Here is one way to make your code workable:

function value = Laplace(A)
if nargin < 1
    n = input('Matrix dimention = ');
    A = rand(n);
end
[rows, ~] = size(A);
value = 0;
if rows == 2
    for i = 1:rows
        value = A(1,1)*A(2,2) - A(1,2)*A(2,1);
    end
else
    if rows >= 3
        for i = 1:rows
            columnIndices = [1:i-1 i+1:rows];
            value = value + (-1)^(i+1)*A(1,i)*Laplace(A(2:rows, columnIndices));
        end
    end
end
end

It doesn't mean that it does what it should, I only fixed all mistakes that prevent it from working, guessing what was the original intention.

EBH
  • 10,350
  • 3
  • 34
  • 59
  • thank you. I am to compare it with Bariess method and Dodgson condensation and to show that it is not suitable for higher dimension – babs Jul 19 '16 at 13:05
  • Please what is the meaning of "if nargin < 1". I first thought you made a mistake for "margin", then I discovered that if I change it to margin I will get a graph for Bode diagram. You are also write that it does not mean it does what it should. I am looking at how I will fix it to work for dimension greater than two. It seems the value after else is not correct. – babs Jul 19 '16 at 14:03
  • @babs 1. I changed the code so it will run for size > 3 => `if rows >= 3`. Still I don't know all this methods you wrote about, you have to check it is doing what it should. 2. `nargin` is a variable that holds the number of inputs to the function when it was called, so if you call it with no input, like `Laplace` it will ask you for the matrix dimension, but when you call it from inside the function (recursively) `Laplace(A(2:rows, columnIndices))` then it skips the first part. – EBH Jul 19 '16 at 15:25