File Exchange Pick of the Week

June 26th, 2009

“Don’t let that INV go past your eyes; to solve that system, FACTORIZE!”

Jiro's pick this week is FACTORIZE by Tim Davis.

If you browse through the MATLAB Newsgroup, you will occasionally find discussions on "matrix inverse". In many of those discussions, the ultimate goal of wanting matrix inversion was to solve a linear system

   Ax = b

Take this simple example:

A = randn(3, 3)
b = randn(3, 1)
A =
      0.52006     -0.79816     -0.71453
    -0.020028       1.0187       1.3514
    -0.034771     -0.13322     -0.22477
b =
     -0.58903
     -0.29375
     -0.84793

Technically, you can solve it using the INV function.

x1 = inv(A)*b
x1 =
      -30.333
      -56.674
       42.054

But as the documentation for inv suggests, there is a better way to solve this problem, both in terms of efficiency as well as accuracy. That is to use the backslash operator:

x2 = A\b
x2 =
      -30.333
      -56.674
       42.054

Tim takes this one step further and extends the capability with his Factorize object. One of the benefits is that you can "reuse" this efficiency in different conditions:

c = randn(3, 1);

F = factorize(A);
x3 = F\b
x4 = F\c;
x3 =
      -30.333
      -56.674
       42.054

This is a simple 3-by-3 example for illustration, but you'll appreciate its power when you're working with larger systems. I like this entry for several reasons.

  • He uses the new MATLAB Class system (introduced in R2008a). Specifically, he utilizes object-oriented techniques, such as property attributes and abstract classes.
  • He includes a very thorough, pedagogical document that explains the uses of the object and the theory behind different techniques for solving linear systems.
  • He includes a test suite for testing the accuracy, performance, error-handling, and display methods of the Factorize object.

I recommend looking at this highly-rated entry by Tim.

Comments?


Get the MATLAB code

Published with MATLAB® 7.8

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • oleg: The author has implemented skewness, kurtosis and checks answering appropriately to the critic.
  • Ashok: how to store 10 or more random number in a loop a loop for i = 1:n mean(i) = input(’enter the mean value...
  • Ben: Doug, Thanks for the very helpful videos! Uitables seem like a convenient way to make a customized property...
  • oleg: Allstats has no checks, no comments and could also be improved (talking about prctile implementatio). Not to...
  • Todd: Additional information and a link to download free MATLAB and Simulink LEGO MINDSTORMS NXT code can be found at...
  • Doug: @Leo, Here is the “English version” of that code. “vec = []” makes an empty variable...
  • leo: Dear Doug I have a question in your code ‘October 9th, 2009 at 13:53′ vec = []; vec = [vec val]; I...
  • Shanker Keshavdas: You sir, are a gentleman and a scholar… No really, helped me out a lot. As to what is...
  • Quan Zheng: how can I get a copy of stepspecs.m?
  • Doug: @Lucy I think this is what you seek to move a line with the mouse in MATLAB. http://blogs.math...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.