Loren on the Art of MATLAB

Turn ideas into MATLAB

Introduction 8

Posted by Loren Shure,

One of my favorite things about MATLAB is that I can write a mathematical algorithm using the same notation appearing in a technical book or paper. For algorithms that are naturally matrix-based, the economy and clarity of the MATLAB code contribute to the program's elegance.

What constitutes good code? Here are some possible characteristics:

  • fast
  • clear
  • readable
  • maintainable
  • efficient
  • short
  • memory-efficient
  • "clever" – for example, see the MATLAB golf contest
  • reliable

During my first year at MathWorks, 1987, a customer contacted us that he was having trouble translating and debugging an algorithm. The algorithm the customer was interested in was for solving a least-squares problem with constraints that the coefficients be non-negative. He based his MATLAB implementation on the Fortran code supplied in Lawson and Hansen's wonderful book, Solving Least Squares Problems (Prentice-Hall, 1974.). The Fortran code and the translated M-file were equivalent in length, in the range of 300 lines of code.

Taking a look at the code, I decided that though I had used the Fortran routine before, it was now time to dig in and understand the algorithm more. For that I turned to the relevant chapter where I found some pseudo-code. Using the pseudo-code as the basis for coding the algorithm, the resulting M-file was about 80 lines long. The algorithm originally shipped in MATLAB as the function nnls, and has since be renamed to lsqnonneg.

You won't always see the algorithm you want to code expressed in the language of linear algebra even when it might be appropriate. Part of the art of using MATLAB well is learning when and how to cast algorithms into the linear algebra framework. There are often several good ways to solve such a problem, with competing solutions having different benefits. Recognizing which code characteristics are important in any given situation contributes to the art. There is invariably room for esthetics when programming in MATLAB.

earrings.JPGThese earrings, which I found shortly before the first MATLAB User Conference in 1993, remind me of the beauty and flexibility of mathematics, linear algebra, and MATLAB.

This blog will discuss topics relevant to programming in MATLAB. My advice is to write readable, elegant code. One of our goals at MathWorks is to make sure that such code executes well and we continue to invest in MATLAB to ensure that this is so.


Comments are closed.

8 CommentsOldest to Newest

Brad Phelan replied on : 1 of 8
Hi Loren, Natural notation in Matlab could be enhanced by allowing Ruby [ http://www.ruby-lang.org/en/ ] style blocks and iterators. In Ruby every statment is an expression and functions can have blocks implicitly passed to them by providing a ''do'' keyword after the function call. The do keyword opens a block and end closes it. The last expression evaluated is the return value of the block. The observation to make is that for loops and while loops are just specialized cases of iterators. Ruby allows the user to create any type of iterator they can imagine. For example here is the usage of an iterator that generates first 10 fibonacci numbers and only prints them if they are greater than 10;
fib(10) do |f|
   if f > 10
instead of
for i = 1:10
   f = fib(i) % Extra line not needed!
   if f > 10
But iterators are not limited to generating sequential items. They could be used to allow the user to write natural mathematical notation. In Ruby if the quad function were defined you could do a triple integration something like this. ( Performance considerations excluded! )
r = Math.quad(0, 1) do |x|
  Math.quad(0, 2) do |y|
    Math.quad(0,3) do |z|
       if x > 0
          x^2 + y^2 + y^2
          x + y + z
To do the same in Matlab would require a great deal of boiler plate code including named nested functions. You can observe very often in the use of nested functions that the function is only ever used once! I have implemented such an extension to Matlab for my templating engine. It is a change to the Matlab language that compiles into Matlab and makes generating text/code/html/xml etc really easy from Matlab. Regards Brad
O.E. KEVEEN replied on : 2 of 8
I am a 19yrs old student from Nigeria. I am studying computer science in a polytechnic here in Nigeria. I really do like MATLAB. I think learning how to program in MATLAB is really going to be great. Here in my school, we don't have MATLAB in the schools Computer Laboratory. There is FORTRAN , COBOL, BASIC. I have known about MATLAB about 2-months but I have not have the opportunity to work with it. I told some of my course mates in school about MATLAB, because non of them know about this New MATLAB programming. I'll also want to know more about MATLAB. Thanks Yours Faithfully, KEVEEN ORIKPETE EFE #68 JATTU ROAD, OPP. TANIAS AUCHI, EDO STATE, NIGERIA.
kamal replied on : 4 of 8
Hello Madam, I am kamal,doing my final year course of engg.By seeing at your introduction I thought you could give a solution to my problem. I am doing a project based on matlab programmming & simulation.In our project we have to write program as to when to open and close a circuit breaker.Also I have to monitor the voltages at some points in the system.the simulaed power circuit should according to the written program. The problem is I could not write a program.though books are available which describes how to programs for calculating different series of numbers ,plots(only mathematical). so, please give me information about concerned books. thanking you..
Loren replied on : 6 of 8
Kamal- In addition to the MATLAB documentation, which is quite good, and the demos that ship with MATLAB, you might look for books here: https://www.mathworks.com/support/books/ --Loren
tando replied on : 7 of 8
I'm a Matlab beginner and quite recently we were asked to make a program. I'm struggling to find respective forces that will make Moment=0 i.e i know that Moment=cross(r1,F1)+ cross(r2,F2)+.....+cross(rn,Fn) and I know that Moment=0, I need to find the Forces that will make the Moment=0. Can you please suggest ways that I could try to solve this! I've tried if and while Loops.Thanx
Tom Medl replied on : 8 of 8
Do you still have the 1993 conference papers on your website somewhere? Maybe there is a big directory with all the papers from all the user conferences? Thanks, Tom Medl