Archive for the ‘matlab’ Category

May 12th, 2009

I have got a long list entitled blog articles I want to write this year and about halfway down it are the words Write a tutorial for working with Excel Spreadsheets in MATLAB. Thanks to the guys at Blinkdagger I have crossed off that particular item without writing a thing.  They have produced two fantastic tutorials at the following links

If you have any topics that you would like me to add to the list of blog articles to write then let me know in the comments section.

April 27th, 2009

I’ve just discovered a blog bost where the author was installing Octave on a Mac.  Looks hard!

I compare it with Ubuntu’s installation method for Octave along with the symbolic package:

sudo apt-get install octave octave-symbolic

and wonder what is going wrong for it on Macs.  Insights anyone?

April 9th, 2009

A new version of the popular free, open source MATLAB clone, Octave, was released yesterday containing a fix to a bug introduced in the previous version, 3.0.4 which itself was only released last week containing yet more bug fixes.

Octave is a fantastic project and it’s great to see that the development team responds to bugs in the code so quickly.

April 8th, 2009

I was discussing the (relatively) new Symbolic Toolbox for MATLAB with someone yesterday and found the need to trawl through the documentation.  There is a command called mfun in the symbolic toolbox which allows you to numerically evaluate various special functions such as Dawson’s integral or the digamma function.  Let’s look at the output of  help mfun in MATLAB 2009a.  The bold highlights are mine.

help mfun
MFUN   Numeric evaluation of a special function.
MFUN(‘fun’,p1,p2,…,pk), where ‘fun’ is the name of a Maple
function and the p’s are numeric quantities corresponding to fun’s
parameters.  The last parameter specified may be a matrix. All other
parameters must be the type specified by the Maple function.
MFUN numerically evaluates ‘fun’ with the specified parameters
and returns MATLAB doubles. Any singularity in ‘fun’ is returned
as NaN.

Of course, it should be MuPAD rather than Maple these days :-)  The text in the help browser is correct though and refers to MuPAD throughout.

What’s more if you do help mfunlist in MATLAB 2009a (to list all of the functions supported by mfun) then near the end of the list you’ll get the following extract

Orthogonal Polynomials (Extended Symbolic Math Toolbox only)
T      n,x             Chebyshev of the First Kind
U      n,x             Chebyshev of the Second Kind

However, the Extended Symbolic Toolbox was discontinued from MATLAB 2008b onwards and all of the Orthogonal Polynomials mentioned in the help file are now available in new symbolic toolbox as standard.

This is no big deal (Walking Randomly probably has MANY more typos than this for example) and after a change as major as swapping the Maple toolbox for the Mupad one I guess things such as this should be expected from time to time.

April 2nd, 2009

I recently installed MATLAB 2009a on a Mac running Mac OS 10.5.6 and, although the installation seemed to go fine, MATLAB wouldn’t start.  The error message I received (copied from the console output) was


02/04/2009 16:19:42 [0x0-0x2d32d3].com.mathworks.StartMATLAB[36443] dyld: Library not loaded: /usr/X11R6/lib/libXmu.6.dylib
02/04/2009 16:19:42 [0x0-0x2d32d3].com.mathworks.StartMATLAB[36443] Referenced from: /Applications/MATLAB_R2009a.app/sys/os/maci/libXm.3.dylib
02/04/2009 16:19:42 [0x0-0x2d32d3].com.mathworks.StartMATLAB[36443] Reason: image not found

Now I don’t know very much about Macs and I tend to think of Mac OS X as a closed-source version of Linux with pretty bits so there may be a much better way of fixing this than what you are about to read but it did the job for me. Your mileage may vary.

Firing up a terminal I had a look to see if the offending file was anywhere on the machine by typing

locate libXmu.6.dylib

Sure enough I had it in /usr/X11/lib but MATLAB was looking for it in /usr/X11R6/lib so I created a symbolic link as follows

sudo ln -s /usr/X11 /usr/X11R6

Tried MATLAB again and it worked perfectly. Let me know if this works for you or if you are a Mac expert and you know of a better way.

March 23rd, 2009

Near the end of last week I was acting as teaching assistant for a MATLAB course and some of the students wanted to know if there were any viable free, open source alternatives to MATLAB.  I duly listed what I considered to be the standard set – Scilab, Octave, SAGE and Python – which are more or less MATLABy depending on your point of view.

One student asked ‘What about R?’ and I had to confess that I didn’t know much about it except that it is used a lot by statisticians and is essentially a free, open source version of S+.  I ventured the opinion that maybe R was too specialised to be considered a general MATLAB alternative with the caveat that I didn’t actually know what I was talking about.

It seems that there is nothing a student likes more than to teach the teacher and so much googling ensued.  I was pointed to a small set of speed comparisons between MATLAB and R for example which includes some matrix operations like finding eigenvalues and generating Toeplitz matrices.  You don’t get much more MATLABy than matrices!  Other articles such as this comparison between various data analysis packages also proved interesting and useful.

So, thanks to a line of questioning from some MATLAB students, I have yet another thing on my to-do list – find out more about R.  What do you think?  Can R replace MATLAB sometimes?

March 13th, 2009

Say you want a vector that starts at 0 and goes to 1 in steps of 0.1
There are two ways you might do this in MATLAB

x=0:0.1:1
y=linspace(0,1,11)

If you display either x or y then you will get

[0    0.1000    0.2000    0.3000    0.4000    0.5000    0.6000    0.7000    0.8000    0.9000    1.0000]

So it looks like we are in clover.  However if we do

x==y

we get

[1     1     1     0     1     1     1     1     1     1     1]

Which shows that the vectors x and y are not EXACTLY equal for all values (1 stands for true and 0 stands for false so one value is different).  We can see that the difference is extremely small by doing

x-y

1.0e-16 *
0         0         0    0.5551         0         0         0         0         0         0         0

This tiny difference is almost certainly caused by the fact that the colon operator and linspace use slightly different methods to calculate the vectors as pointed out in the newsgroup post where I discovered this little piece of trivia.  .  If floating point arithmetic was exact then it wouldn’t matter what algorithm you used to calculate a vector like this – the result would always be the same as you would expect.

However, floating point arithmetic isn’t exact and so things like the order of operations matters.  This sort of thing has been discussed many times by people considerably more erudite than me so I refer you to them for details.

What I was curious about was ‘How,exactly, does the colon operator calculate it’s values and how does this differ from linspace?’ Oh yes, I am a sucker for mathematical trivia.

If I didn’t have access to either of these methods in MATLAB then I would calculate our vector like this

n=0:10
a=n*0.1

But it turns out that this isn’t equal to either x or y so this isn’t how linspace or the colon operator works:

a==x (compare to colon operator)

1     1     1     1     1     1     0     0     1     1     1

a==y (compare to linspace)

1     1     1     0     1     1     0     0     1     1     1

Another way for calculating this vector that springs to mind is to start with 0 and repeatedly add 0.1 until you reach 1.  I normally would never do it like this due to the possibility of accumulating rounding errors on each addition but let’s take a look.

b=[zeros(1,11)];
for i=2:11
b(i)=b(i-1)+0.1;
end

As I’d expect, this isn’t how MATLAB does it either.

b==x (compare to colon operator)

1     1     1     1     1     1     1     1     0     0     0

b==y (compare to linspace)

1     1     1     0     1     1     1     1     0     0     0

My methods don’t agree with each other either (do a==b to see) but I was expecting that.

So, I am non the wiser.  How does linspace and the colon operator work? Thoughts and comments would be welcomed.

March 6th, 2009

The math software releases are coming thick and fast at the moment. This latest release of Mathworks’ flagship program comes with a huge number of improvements but possibly the most important in my mind right now is the following.

When you use the Parallel Computing Toolbox with MATLAB 2008b or below, you can only make use of up to 4 cores simultaneously.  This was starting to become a problem for users at my workplace because dual quad core machines are becoming increasingly common around there.

I installed the PCT (at a cost of 192 pounds +VAT for the network licensed version) for the owner of such a machine a few months back and we immediately hit upon the 4 core limit of MATLAB 2008a.  He had 8 cores and wanted to use them so I fired off an email to Mathworks support for advice.  I figured that we would have to pay another 192 pounds for another 4 core PCT but I was wrong.  So, very very wrong.

Cutting a long story short we were told that if we wanted to use 8 cores then we would have to buy 8 licenses for their dsitributed computing server product at a total cost of 1400 pounds + VAT. Ouch!

So…to summarize – the pricing structure we were being offered for MATLAB 2008a was

  • Parallelisation over 4 cores cost 192 +VAT or 48 pounds per core
  • Parellisation over 8 cores cost 1400 + VAT or 175 pounds per core

I was rather stunned by this outcome and said as much to my long-suffering Mathworks account manager (who has the patience of a saint I think – I’d like to take this opportunity to publicly thank her for putting up with me).

Anyway, this has been fixed in 2009a and the PCT can now use up to 8 cores and so  this particular toolbox is now twice as powerful as it was before.

Great news all round I think.

If you enjoyed this article, feel free to click here to subscribe to my RSS Feed.

March 6th, 2009

I needed to profile some MATLAB code recently to try and optimise it.  It’s been a while since I have done this sort of thing so I first reminded myself of the details by reading the documentation.  Eventually I came to a sage piece of advice concerning multi-core processors…

Note If your system uses Intel multi-core chips, you may want to restrict the active number of CPUs to 1 for the most accurate and efficient profiling. See Intel Multi-Core Processors — Setting for Most Accurate Profiling for details on how to do this.

‘Sounds fair enough‘ I thought so I clicked on the link and started reading the instructions.  The first couple of steps were

1. Open Windows Task Manager.

2. On the Processes tab, right-click MATLAB.exe and then click Set Affinity.

Outrage!  How dare they assume I am using Windows ;)  A bit of searching through the documentation revealed that, sure enough, The Mathworks had forgotten about us Linux users in this particular instance.  Mac users are left in the cold too for that matter.

So, I fired off a quick email to Mathworks support and asked them how I should proceed with setting the processor affinity as a Linux user.  I got a reply within just a couple of hours.  Here it is in full (edited very slightly by me) for anyone else who needs to do this:

The Linux  taskset command (part of a scheduler utilities package) can be used to set the processor affinity for a certain Process ID (PID). To find MATLAB’s PID, you can execute

ps -C MATLAB

from a Linux terminal. Then, to set the processor affinity for MATLAB to one CPU (e.g. CPU #0), execute (also from a Linux terminal):

taskset -pc 0 PIDhere

The ‘-p’ option specifies that taskset should operate on an existing PID instead of creating a new task. The ‘-c’ option allows you to specify a list of processor numbers instead of a bitmask representing the processors you would like to use. For more information on the syntax of taskset, it is best to look at the manpage for this command (i.e. by executing man taskset from a Linux terminal).

The support analyst (who wishes to remain anonymous) who was helping me out then informed me that he had raised a request for enhancement with the Mathworks documentation team to try and get this information covered in future versions of the MATLAB docs.

I’m impressed with this level of service so thank you to everyone involved at the Mathworks for solving this little issue of mine.

December 27th, 2008

The guys at Blinkdagger put together a great MATLAB Christmas card for you all and sent it in to me as part of the Walking Randomly Christmas challenge.  I would have posted it earlier but I have been away from the Internet over Christmas (sorry Rob). Thanks guys!

MATLAB Christmas card

A brief explanation of the maths involved (from Rob of Blinkdagger)
– the snowdrifts are sine waves of different amplitudes and offsets
– the tree is a 3D spiral of green asterisks
– the ornaments are a random selection of the same points as the tree,
with different colors and marker types
– the presents under the tree are the trademark “membrane” surface that is the MATLAB logo.
– the star atop the tree, as well as the star in the sky are just single points graphed, but using the default marker types creatively
– the text is, well it’s just text


% Xmas card from blinkdagger.com for the challenge

%% setup the fig
h1 = figure(‘Position’,[50 50 800 600],’Color’,’w’);

%% Put background snowdrifts and star
axes(‘Position’,[0 0.5 1 .05]);
ezplot(‘sin(.1*x+1.8)’); axis off; title(”);

axes(‘Position’,[0 0.4 1 .1]);
ezplot(‘sin(.3*x)’); axis off; title(”);

axes(‘Position’,[0 0.25 1 .1]);
ezplot(‘sin(.4*x-1.5)’); axis off; title(”);

axes(‘Position’,[0 0.1 1 .15]);
ezplot(‘cos(.4*x-2)’); axis off; title(”);

axes(‘Position’,[0.75 0.75 0.2 0.2]);
plot(1,1,’x’,’markersize’,48,’markeredgecolor’,[.5 .5 .5]);
axis off; title(”); hold on;
plot(1,1,’h’,’markersize’,36,’markeredgecolor’,[.5 .5 .5]);
plot(1,1,’+’,’markersize’,80,’markeredgecolor’,[.5 .5 .5]);
hold off;

%% Put tree on and decorate it

axes(‘Position’,[0.55 0.2 0.4 0.6]);
z = 0:.05:100;
x = cos(z); x = (101-z).*x;
y = sin(z); y = (101-z).*y;
plot3(x,y,z,’g*’,’markersize’,18); axis off; view(0,10);
hold all;
% star on top
plot3(0,0,107,’p’,’markersize’,24,’markerfacecolor’,[1 .8431 0],…
‘markeredgecolor’,’r’);
% plot ornaments
k = rand(size(x));
k1 = k>.97;
k2 = k>.94 & k<.97;
k3 = k>.91 & k<.94;
k4 = k>.88 & k<.91;
k5 = k>.85 & k<.88;
k6 = k>.82 & k<.85;
plot3(x(k1),y(k1),z(k1),’ro’,’markersize’,8,’linewidth’,2); axis off; view(0,10);
plot3(x(k2),y(k2),z(k2),’ys’,’markersize’,8,’linewidth’,2); axis off; view(0,10);
plot3(x(k3),y(k3),z(k3),’md’,’markersize’,8,’linewidth’,2); axis off; view(0,10);
plot3(x(k4),y(k4),z(k4),’b^’,’markersize’,8,’linewidth’,2); axis off; view(0,10);
plot3(x(k5),y(k5),z(k5),’kh’,’markersize’,8,’linewidth’,2); axis off; view(0,10);
plot3(x(k6),y(k6),z(k6),’c*’,’markersize’,8,’linewidth’,2); axis off; view(0,10);

hold off;

%% Put presents under the tree
axes(‘Position’,[0.5 0.12 0.1 0.15]);
colormap(hot*.7); surf(membrane,membrane); shading flat; axis off; view(300,20);
axes(‘Position’,[0.57 0.1 0.1 0.15]);
surf(membrane); shading flat; axis off; view(300,20);
axes(‘Position’,[0.63 0.08 0.1 0.15]);
surf(membrane); shading flat; axis off; view(300,20);
axes(‘Position’,[0.7 0.08 0.1 0.15]);
surf(membrane); shading flat; axis off; view(300,20);
axes(‘Position’,[0.77 0.1 0.1 0.15]);
surf(membrane); shading flat; axis off; view(300,20);
axes(‘Position’,[0.83 0.13 0.1 0.15]);
surf(membrane); shading flat; axis off; view(300,20);

%% Overlay snowflakes

%% Text greeting

axes(‘Position’,[.05 .6 .5 .5]); axis off;
text(.08,.55,’Seasons Greetings!’,’fontsize’,30,’color’,’r’,…
‘fontname’,’Garamond’,’fontweight’,’bold’);
text(.07,.31,’Best wishes and have a great 2009′,’fontsize’,20,…
‘color’,[0 .4 0],’fontname’,’Garamond’);
text(.07,.18,’solving problems with MATLAB!’,’fontsize’,20,…
‘color’,[0 .4 0],’fontname’,’Garamond’);
text(.07,.05,’From everyone at blinkdagger.com!’,’fontsize’,20,…
‘color’,[0 .4 0],’fontname’,’Garamond’);