+ Reply to Thread
Results 1 to 11 of 11

Find Matrix Inverse for 2*2 and 3*3 matrices

  1. #1
    Registered User
    Join Date
    03-08-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    6

    Exclamation Find Matrix Inverse for 2*2 and 3*3 matrices

    I am writing a VBA function (of type Variant) to calculate the
    inverse of an arbitrary 2*2 or 3*3 matrix in Excel. I am using the formula involving the adjoint
    of the matrix. I need to have my function to flag unsuitable matrices (i.e., not 2 * 2 or 3 * 3) with a message box and then stop. To further clarify the adjoint of the matrix:
    Step 1: calculating the Matrix of Minors,
    Step 2: then turn that into the Matrix of Cofactors,
    Step 3: then the Adjugate, and
    Step 4: multiply that by 1/Determinant.

    This is the code I have written so far but I am having a problem implementing the code to calculate the matrix cofactors and the matrix of minors. I am also not sure whether the code I have written for transpose and the determinant are correct. My main issue is figuring out how to successfully access the cell elements and multiply/add/subtract them. Thanks for any help in advance.


    Please Login or Register  to view this content.
    Last edited by AliGW; 03-09-2019 at 07:11 AM. Reason: Post reinstated.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,939

    Re: Find Matrix Inverse for 2*2 and 3*3 matrices

    How is this thread any different from your other thread here?
    https://www.excelforum.com/excel-pro...functions.html
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Find Matrix Inverse for 2*2 and 3*3 matrices

    It is an interesting programming exercise, but is there some reason you are not just using the built in MINVERSE() function https://support.office.com/en-us/art...9-59da2d72efc6 Recall that you can call Excel's built in functions from VBA: https://docs.microsoft.com/en-us/pre...1(v=office.14) Unless there is some reason (such as an educational exercise), it seems to me that this will be easiest to just use Excel's built in function.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    03-08-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    6

    Re: Find Matrix Inverse for 2*2 and 3*3 matrices

    The titles show the difference. I need a function that calculates the inverse of a 2*2 or 3*3 matrix in this thread. For the other thread, you pointed to, I need a function that solves the inner product of two matrices. Thank you.

  5. #5
    Registered User
    Join Date
    03-08-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    6

    Re: Find Matrix Inverse for 2*2 and 3*3 matrices

    MrShorty, You are correct. This is an educational exercise and for that reason, I am not allowed to use the built-in functions in excel. Thank you for your comment.

  6. #6
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,939

    Re: Find Matrix Inverse for 2*2 and 3*3 matrices

    Please do NOT delete posts, even if the thread is duplicate.

    here is the original post #1...
    -
    I am writing a VBA function (of type Variant) to calculate the
    inverse of an arbitrary 2*2 or 3*3 matrix in Excel. I am using the formula involving the adjoint
    of the matrix. I need to have my function to flag unsuitable matrices (i.e., not 2 * 2 or 3 * 3) with a message box and then stop. To further clarify the adjoint of the matrix:
    Step 1: calculating the Matrix of Minors,
    Step 2: then turn that into the Matrix of Cofactors,
    Step 3: then the Adjugate, and
    Step 4: multiply that by 1/Determinant.

    This is the code I have written so far but I am having a problem implementing the code to calculate the matrix cofactors and the matrix of minors. I am also not sure whether the code I have written for transpose and the determinant are correct. My main issue is figuring out how to successfully access the cell elements and multiply/add/subtract them. Thanks for any help in advance.


    Please Login or Register  to view this content.

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,929

    Re: Find Matrix Inverse for 2*2 and 3*3 matrices

    I've reinstated the opening post. The OP was not particularly happy about my reconciling his three threads on this issue.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Find Matrix Inverse for 2*2 and 3*3 matrices

    I'm not sure how best torespond. As an educational exercise, I would not want to just give answers, as I believe you will learn more discovering answers on your own. A few comments that I hope will help without interfering with what you are supposed to be learning:

    I need to have my function to flag unsuitable matrices (i.e., not 2 * 2 or 3 * 3) with a message box and then stop.
    This comment depends a lot on how you intend to use this function. When I started creating User-Defined functions (UDFs) intended to be called from a spreadsheet, I learned very quickly that I did not want a UDF to use an input box or a message box. When the error condition exists, you will get the message box (and need to dismiss the message box) for each and every calculation event (and for every copy of the function in the spreadsheet, if you have more than one copy). It should not take much imagination to see that this will likely become very irritating very quickly. Just want to suggest that you consider how you intend to use the function so you know how you want the function to interact with the user.

    My main issue is figuring out how to successfully access the cell elements and multiply/add/subtract them.
    Since r is a range object, I would suggest referencing MSFT's help file for the range object: https://msdn.microsoft.com/en-us/vba...e-object-excel -- in particular, note the similarities and differences between the .cells() method and the .range() method. As a quick test procedure, I wrote this simple procedure
    Please Login or Register  to view this content.
    I might suggest running this simple procedure with different ranges past to the function and see what each debug.print statement outputs. Modify the code to test accessing different cells in the range. This and the help file should give you some good idea of how to access the different cells in the range.

    Does that help? Do you have additional questions?

  9. #9
    Registered User
    Join Date
    03-08-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    6

    Re: Find Matrix Inverse for 2*2 and 3*3 matrices

    MrShorty, what you suggested helped me understand the difference but I can not seem to think of a way to use .range instead of .cell. However, Since my matrix will always be a 2*2, can I store the values of each cell then retrieve those in an array? I wrote a few lines in an attempt to store the corresponding values of each cell indiviually so that I can manipualte those values as needed. Is it possible to return those values in an array once again? Thanky you for taking the time to guid me through this. I appreciate your help!

    Please Login or Register  to view this content.

  10. #10
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,939

    Re: Find Matrix Inverse for 2*2 and 3*3 matrices

    Quote Originally Posted by AliGW View Post
    I've reinstated the opening post. The OP was not particularly happy about my reconciling his three threads on this issue.
    OH well. They would either be merged, or closed.

  11. #11
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Find Matrix Inverse for 2*2 and 3*3 matrices

    I can not seem to think of a way to use .range instead of .cell.
    Is it necessary to use the .Range method? The idea behind the different methods is to choose the one that is easiest to use/program. If .Cells() is easier than .Range(), there should be no need to use .Range() -- Unless, again, this is an educational exercise to practice using the different methods.

    can I store the values of each cell then retrieve those in an array?
    If I understand the question -- yes, you can take r and store it in a VBA array. See tutorial here: http://www.cpearson.com/Excel/ArraysAndRanges.aspx One thing I would note is that, if these are UDFs intended to be called from the spreadsheet, you will assign the result array to the function name which is how the array will get output to the spreadsheet.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem with inverse matrixes 6x6 matrix
    By fabio12 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-22-2014, 07:06 PM
  2. Calculate Matrix Inverse without the MINVERSE function in excel
    By shirleyxiao in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-07-2013, 11:17 AM
  3. [SOLVED] MATRICES ISSUE: Trouble translating some geographical matrix theory into a formula
    By spanishguy in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-14-2012, 01:50 PM
  4. Need help with inverse matrix
    By kylwggns in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-05-2012, 11:02 AM
  5. Inverse Matrix Function Problem
    By MichielVrijbergen in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-14-2011, 10:21 AM
  6. Inverse Matrix
    By adashiu in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 10-23-2010, 11:24 AM
  7. Inverse and multiplication of Matrices In VBA
    By adashiu in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-30-2008, 07:10 PM
  8. How to compute the inverse of a matrix with some complex elements
    By Annoushka42 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-05-2006, 08:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1