Calculating Determinants in Visual Basic: Cramer's Rule Method

In summary, the person is asking for help with a Visual Basic program that can work out determinants, and has run into problems. They have a function that calculates a determinant based on the dimensions of the matrix, but has run into issues with larger matrices. They also mention wanting to solve a matrix using Cramer's rule, but are unsure of how to do it efficiently.
  • #1
Shahil
117
19
:cry: :cry: :cry:

Nobody wants to help me in the Software foum so I'm hoping someone loves me enough here to gimme some help! :shy:


Basically, I need a Visual Basic program that can work out 4x4 determinants.

I need it for a project and practical usage as well.

Anyway - the method I deviced uses slowly decomposing the 4x4 into 3x3's then 2x2's and hence calculate an answer.

Problem 1: IT'S LONG (ie. 24 terms made up of 4 different matrix entires for each!)
Problem 2: If I need a 5x5...

So :shy:

help me please! I was thinking about using triangularisation but doing it is one thing, CODING :surprise: it is another!

Anyway, hope SOMEONE can help me!
 
Physics news on Phys.org
  • #2
I remember writing a function just like this in C, I'm not familiar with VB but I can give some general tips. First of all, recognize that determinants can be calculated recursively, in fact they are. That is, for some m x m matrix:

[tex]A\ =\ [a_{ij}][/tex]

[tex]\det A\ =\ \sum a_{1j}A_{1j}\mbox{, for j = 1, 2, 3, ... m, and }A_{ij}[/tex]
is the (m-1) x (m-1) matrix remaining after removing row "i" and column "j" from A.

So, you should have a function that looks something like this:

PHP:
int determinant (int dimension, int [] matrix) {

   // base case
   if (dimension == 2) return (matrix[0]*matrix[3] - matrix[1]*matrix[2]);
   int sum = 0;

   for (int i = 0; i < dimension; i++) {

      // for each "i", you will create a smaller matrix based on the original matrix
      // by removing the first row and the "i"th column.
      int smallMatrix [(dimension - 1)*(dimension - 1)];
      for (int j = 1; j < dimension; j++) {
         for (int k = 0; k < dimension; k++) {
            if (k < i) smallMatrix [(j-1)*(dimension-1) + k] = matrix [j*dimension + k];
            if (k > i) smallMatrix [(j-1)*(dimension-1) + k - 1] = matrix [j*dimension + k];
         }
      }

      // after creating the smaller matrix, multiply the "i"th element in the first
      // row by the determinant of the smaller matrix.
      sum += (matrix [i])*(determinant (dimension - 1, smallMatrix));
   }
   return sum;
}
 
  • #3
Thanks for the help (though I don't understand the code!)

The formula helped though and I figure out something! It was similar in nature to your code (only in VB) and I got 95% for it! Woopee! Only, it gives the wrong determinants (I think it's reducing the rows a bit too much!)
 
  • #4
Thanks for the help (though I don't understand the code!)

The formula helped though and I figure out something! It was similar in nature to your code (only in VB) and I got 95% for it! Woopee! Only, it gives the wrong determinants (I think it's reducing the rows a bit too much!)
 
  • #5
shahil :)
 
  • #6
I'm back here again!

Just a thought on the C code AKG: WOuld you suggest I use this method to find a determinant then solve a matrix using Cramer's Rule or is there a much easier way?

BTW I've moved up from VB to C code (not C++ yet coz I don't have the software and I'm running Linux so C is better!) and I want to solve huge matrices ie. up to 10x10.
 
  • #7
Yeah, it gives the wrong determinants, because something is missing. It was:

[tex]\det A\ =\ \sum _{j = 1} ^m a_{1j}A_{1j}[/tex]

and should have been:

[tex]\det A\ =\ \sum _{j = 1} ^m (-1)^{(j+1)} a_{1j}A_{1j}[/tex]

It's easy to adjust your code to compensate for this.

What do you mean "solve a matrix"? You mean solve for X where AX = Y? I suppose I would do what you said, use Cramer's rule. If you're terribly concerned with efficiency, you may want to find a more efficient way, this algorithm, as far as I can tell, will take take time on the enormous order of O(n(n!)³) for nxn matrices. Once you type in the matrix and the Y vector, this should work nicely and give you the solution, so it's easy, but it will take time. Try it with simple matrices, 2x2 or 3x3 to start. See how high you can go before it starts taking too long, I would suspect it would be too slow pretty fast, although I don't know of a more efficient algorithm.

I would suggest doing some research of your own on algorithms to find determinants.
 
  • #8
Thanks for the help! Though..I'm just a bit confused how to actually implement your correction!

Also, I thought about using my VB Gauss reduction method to solve this problem but...it seems that the GuI/non-GUI interface thingy is screwing me around and I'm seriously lost!

Can you help me a liccle bit more then :blushing:

BTW - sorry for taking so long to reply ---> having MAJORT posting problems (stupid varsity computers...)
 
  • #9
It should be simple. Change
PHP:
sum += (matrix [i])*(determinant (dimension - 1, smallMatrix));
to
PHP:
if (i % 2 == 1) sum += (matrix [i])*(determinant (dimension - 1, smallMatrix));
else sum-= (matrix [i])*(determinant (dimension - 1, smallMatrix));
If you want to include the math library, I believe there's a function Math.pow(x, y) which gives you [itex]x^y[/itex], so you could just replace it with:
PHP:
sum += (Math.pow(-1, i + 1))*(matrix [i])*(determinant (dimension - 1, smallMatrix));
Now, I think Gaussian elimination would give you a more efficient solution, but the algorithm would be harder to implement as far as I can tell. Note that with the determinant way, if you get the determinant of the original matrix to be zero, you know right away that you don't have a unique solution (and may not have a solution at all).
 
Last edited:
  • #10
Kewl...ja, think the math thing should work!

I'll post the Gauss method I intended using maybe tomorrow (it's not on this computer that I'm using now!) And I shall try out your way!

Thanks a lot!
 
  • #11
Hey! This is what I got using row reduction. I THINK it works (well it worked so far at least!)Take a look. method.

PHP:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
 int   i, j,dummy,m, n,k;
  double sum, dum;


 printf("\n enter no. of unknowns \n");
 scanf("\n%d",&n);

 double A[n][n+1];
 double B[n];
 int Row[n];

 for (i = 1; i <= n; i++)
 {
  for (j = 1; j <= n+1; j++)
  {
  printf("\nA[%d][%d] = ",i, j);
  scanf("%lf", &A[i - 1][j - 1]);
  }
}


/*   pivots  */

  for (k = 1; k <= n; k++) Row[k-1] = k-1;


for (i = 1; i <= n - 1; i++)
    {
      for (j = i + 1; j <= n; j++)
      {
        if ( fabs(A[Row[j-1]][i-1]) > fabs(A[Row[i-1]][i-1]) )
         {
           dummy    = Row[i-1];
           Row[i-1] = Row[j-1];
           Row[j-1] = dummy;
         }
      }
      for (j = i + 1; j <= n ; j++)
       {
         dum = A[Row[j-1]][i-1]/A[Row[i-1]][i-1];
         for (m = i + 1; m <= n + 1; m++)
         {
           A[Row[j-1]][m-1] -= dum*A[Row[i-1]][m-1];
         }
       }
    }


  /* back subst*/

  B[n-1] = A[Row[n-1]][n] / A[Row[n-1]][n-1];
  for (j = n - 1; j >= 1; j--)
   {
    sum = 0;
    for (m = j + 1; m <= n; m++)
    {
     sum += A[Row[j-1]][m-1] * B[m-1];
    }
    B[j-1] = (A[Row[j-1]][n] - sum) / A[Row[j-1]][j-1];
   }




 /* output */

 for (i = 1; i <= n; i++)
 {
   for (j = 1; j <= n + 1 ; j++)
    {
      printf("%12.4f", A[i-1][j-1]);
    }
   printf("\n");
 }

 for (j = 1; j <= n; j++)
  {
   printf("Unknown#%d = %6.2f\n", j, B[j-1]);
  }

}

It is a bit long and involved so I would like to know the Cramer's Rule method!
 

1. How can I use VB to find the determinant of a matrix?

To find the determinant of a matrix using VB, you can use the built-in function Det() or write your own function using nested for loops to calculate the determinant based on the matrix's size and values.

2. Can I find the determinant of a non-square matrix using VB?

No, the determinant can only be calculated for square matrices. If you have a non-square matrix, you can use VB to convert it into a square matrix by adding or removing rows and columns of zeros.

3. Is it possible to find the determinant of a matrix with complex numbers using VB?

Yes, VB has built-in functions for complex number operations, including finding the determinant of a matrix with complex numbers. You can use the Complex type to declare and manipulate complex numbers in VB.

4. Are there any limitations to using VB for finding determinants?

VB is a powerful programming language, but it may not be the most efficient option for calculating determinants of large matrices. If you need to find the determinant of a very large matrix, it may be better to use a specialized mathematical software or a language that is specifically designed for mathematical computations.

5. Can I use VB to find the determinant of a matrix with symbolic variables?

Yes, VB has a built-in function called SymbolicDeterminant() that allows you to find the determinant of a matrix with symbolic variables. This can be useful for solving equations or performing other mathematical operations involving unknown variables.

Similar threads

  • Introductory Physics Homework Help
Replies
1
Views
731
Replies
7
Views
1K
  • Introductory Physics Homework Help
Replies
7
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
8K
Replies
2
Views
36
  • Introductory Physics Homework Help
Replies
2
Views
1K
Replies
2
Views
750
  • Linear and Abstract Algebra
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
7
Views
2K
  • Introductory Physics Homework Help
Replies
1
Views
990
Back
Top