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.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

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