Review of the new Symbolic Toolbox in MATLAB 2008b

October 27th, 2008

Introduction

Just over a couple of weeks ago, The Mathworks released the latest version of their main product, MATLAB 2008b, which includes a completely new version of their Symbolic Toolbox. If you are new to MATLAB then maybe a quick explanation is in order here. The base MATLAB package is strictly numerical and has no support for the symbolic manipulation of equations. For example, if you want to solve the quadratic equation x^2 -2*x -5=0 numerically then basic MATLAB can help you. The syntax may look at bit minging at first sight but it does the job and does it efficiently.
 roots([1 -2 -5])

ans =
3.4495
-1.4495

If, on the other hand, you wanted to solve the general quadratic a*x^2+b*x+c=0 in terms of a,b and c then you are out of luck using MATLAB on its own. With the symbolic toolbox, however, calculations such as this are trivial thanks to the solve command
 syms a b c x; y=solve(a*x^2 +b*x+c)

y =
-(b + (b^2 – 4*a*c)^(1/2))/(2*a)
-(b – (b^2 – 4*a*c)^(1/2))/(2*a)

It will also give the answer to our first quadratic exactly – which is nice.
 solve('x^2 -2*x -5=0')

ans =
1 – 6^(1/2)
6^(1/2) + 1

It even has the power to evaluate integrals symbolically – something that I wish I had access to when I was in high school. It misses off the constant of integration but this is the standard behaviour for almost all symbolic integrators and so isn’t anything to worry about. (Note to teachers: If you have a student who always gets the integral correct but shows no working and never includes the constant of integration – now you know why). A simple example:
 int('cos(x)^2')

ans =
x/2 + sin(2*x)/4

So how are these symbolic feats achieved you may ask? Well, it’s all a bit of a trick really because MATLAB isn’t doing any of the work itself. What it does is send the problem to another program called MuPad. Mupad solves the problem and sends the result back to MATLAB for display. Think of it as mathematical out-sourcing if you will – a bit like copying your friend’s calculus homework. This is all done seamlessly behind the scenes and so, as far as the user is concerned, the symbolic toolbox simply adds a range of new commands to MATLAB that can do symbolic calculations. It doesn’t matter that MuPad is really doing the work. Does it?

Yes it does! You see, it wasn’t always MuPad that did MATLAB’s symbolic homework for it. In older versions of the Symbolic Toolbox (2008a and earlier) it was a completely different application that did the symbolic grunt work on MATLAB’s behalf, namely Maple. For some reason, known only to Maplesoft and Mathworks, Maple was dropped from the symbolic toolbox in favour of the lesser-known MuPad. The Mathworks then went and bought the company that produced MuPad and now the only way you can buy a copy of Mupad is to buy MATLAB together with the symbolic toolbox.

So, this new incarnation of the Symbolic Toolbox for MATLAB may look the same as the old version but it has had a brain transplant and thus has a completely different personality with a different set of abilities and behaviours. When I first heard about this change I was very worried – I’ve seen this before you see.

Another mathematical application, MathCAD, also used to use Maple as it’s symbolic engine and it dropped it in favour of MuPad when version 14 was released back in early 2007. It wasn’t pretty! I support MathCAD at the University of Manchester in the UK and all of a sudden people’s symbolic calculations weren’t working as they expected. Lecture notes needed to be rewritten, code needed to be modified and I spent ages scratching my head trying to work out where all the differences were. Some were…interesting…to say the least.

I also support MATLAB at Manchester and we have a LOT more MATLAB users than MathCAD users. Orders of magnitude more in fact and so this transition from Maple to Mupad in the symbolic toolbox was giving me sleepless nights. How much was it going to change things? How different is the input syntax from before? What differences in functionality can I expect to see? Is it any better or worse than before? Most importantly, how much work is it going to generate for me and my colleagues.

Same input, different output

I was very relieved to see that most of the input syntax hasn’t changed at all from older versions of the toolbox. What has changed, however, is the output. Lets look at our simple quadratic example again. In both 2008a and 2008b the input syntax is
 syms a b c x; y=solve(a*x^2 +b*x+c) 

In an old version of the symbolic toolbox (specifically 2008a) the output from these commands is
 y = -1/2*(b-(b^2-4*a*c)^(1/2))/a -1/2*(b+(b^2-4*a*c)^(1/2))/a 
In the new version (2008b) we get
 y = -(b + (b^2 - 4*a*c)^(1/2))/(2*a) -(b - (b^2 - 4*a*c)^(1/2))/(2*a) 

Of course these solutions are mathematically equivalent but they are written in rather different ways. It’s worth pointing out that this isn’t an isolated case – almost every symbolic calculation I tried gave subtly different output like this. Not incorrect I hasten to add – just different. Also, notice that 2008b has given the roots in a different order to 2008a. Most of the time this will not matter but if you have written code that assumes a certain order (and doesn’t check) then you might be in trouble. Of course writing code in such a manner is a bad idea anyway but I like to cover all bases when thinking of possible support issues.

More examples:
 syms a b b=a/3

2008a:1/3*a
2008b:a/3

 syms x y [x,y]=solve('x^2+y^2=1','x^3-y^3=1') 
In 2008a:
 x = 0 1 -1+1/2*i*2^(1/2) -1-1/2*i*2^(1/2)

y =
-1
0
1+1/2*i*2^(1/2)
1-1/2*i*2^(1/2)

in 2008b:
 x = 1 0 (2^(1/2)*i)/2 - 1 - (2^(1/2)*i)/2 - 1

y =
0
-1
(2^(1/2)*i)/2 + 1
1 – (2^(1/2)*i)/2

Practical upshot: If you have written a MATLAB book or a MATLAB lecture course and you have included printouts of the output then you have some minor updating to do. If you have written code that assumes a certain solution order then you have some programming to do.

Variable precision arithmetic

With plain vanilla MATLAB you are limited to, at best, double precision arithmetic. This is screamingly fast since it is directly supported by the hardware of your computer but for some problems you simply need more precision. This is where variable precision arithmetic (vpa) comes in handy, allowing you to calculate to as many decimal places as your computer can handle at the expense of computational speed. The new version of vpa seems to work in exactly the same way as the old one:

digits(50) vpa(pi) vpa('(1+sqrt(5))/2')

3.1415926535897932384626433832795028841971693993751
1.6180339887498948482045868343656381177203091798058

Calculus

The first real problem I ever asked a computer algebra system to do for me was an indefinite integral that took me ages to work out by hand. The fact that I got the correct answer in just a few seconds completely entranced me and I have been throwing integrals at such programs ever since. These days I am much more interested by integrals that computer programs fail to do rather than the ones they do with ease. It’s even more fun when the integral turns out to be trivial to do by hand.

Unfortunately for Mathworks, this new symbolic toolbox has given me a few more ‘interesting’ examples to think about. Here are a couple of integrals that the old toolbox could do but the new one can’t – both of which come from an introductory MATLAB textbook (which now needs a 3rd edition by the looks of things).
 int('arctan(x)/x^(3/2)',0,1) 2008a: -1/2*pi+2^(1/2)*log(2+2^(1/2))-1/2*2^(1/2)*log(2)+1/2*2^(1/2)*pi 2008b: Warning: Explicit integral could not be found. 

 simplify(int('sqrt(1+cos(x)^2)')) 2008a:-csgn(sin(x))*EllipticE(cos(x),i) 2008b: Warning: Explicit integral could not be found. 

There are probably more but I haven’t had much of a poke around so if anyone comes across any more of these, please let me know via the comments and I’ll do some follow up posts. Obviously, I’d also like to know of any integrals that 2008b can do which elude 2008a – I just haven’t found any myself yet.

Of course, just like the old symbolic toolbox, this new one has no problem with differentiation and here is a simple example for the sake of completeness.
 diff('tan(x)') 2008a:1+tan(x)^2 2008b:tan(x)^2 + 1

More examples of symbolic functionality.

If you have never seen the symbolic toolbox in action before then here are a few more examples of what it can do – with output from both this new kernel and the old one.

Factor a large integer into its prime factors.
 factor(sym('567836543898634'))

2008a: (2)*(37)*(347)*(4804201)*(4603)
2008b: 2*37*347*4603*4804201

Solve an Ordinary Differential Equation symbolically
 dsolve('D2y = -a^2*y', 'y(0) = 1', 'Dy(pi/a) = 0')
2008a: cos(a*t)
2008b: cos(a*t)

Find the determinant of a symbolic matrix
 m = sym('[d^2 2 7; d d^3 9; 1 5 1/5]'); det(m)

2008a:1/5*d^5-45*d^2+173/5*d+18-7*d^3
2008b:d^5/5 – 7*d^3 – 45*d^2 + (173*d)/5 + 18

You get the idea – this is a useful toolbox – no matter which version you are using!

Talking to the Mupad kernel directly
The commands provided by the symbolic toolbox represent just a tiny fraction of what MuPad is capable of and so The Mathworks have added some commands to the 2008b symbolic toolbox to allow you to talk directly to the MuPad kernel. As long as the command you are sending produces a non-graphical output then you are good to go:

You can evaluate a Mupad command from the MATLAB command line by using the evalin command which uses the following syntax.

y = evalin(symengine,'MuPAD_Expression');

As a specific example – let’s calculate the number of partitions of 100 in MATLAB using Mupad’s combinat::partitions command:
 y = evalin(symengine,'combinat::partitions(100)')

y =
190569292

I think this is just fabulous as it means that the symbolic toolbox adds an immense amount of functionality to MATLAB. This is a major advance compared to the old version of the symbolic toolbx which only provided direct access to a subset of the underlying Maple computational kernel. If you wanted access to all (non-graphical) Maple functionality from within MATLAB then you needed to buy a rather expensive toolbox called the Extended Symbolic Toolbox. As of version 2008b this extended toolbox no longer exists since the standard symbolic toolbox contains the full version of MuPad. Advanced symbolics from MATLAB just got a whole lot cheaper!

So much syntax, so little time

Having access to the full functionality of Mupad from MATLAB is wonderful but it comes at a price – you have to learn a whole new set of syntax rules. Let’s look at some examples

 MATLAB - acos MUPAD - arccos MATLAB - besselj MUPAD - besselJ MATLAB - conj MUPAD - conjugate 

In MATLAB you suppress output with a ; but in Mupad you use a :
In MATLAB you list all used variables with the whos command. In Mupad it’s anames(All, User)

You get the idea…Of course this is to be expected since Mupad and MATLAB are two completely different systems but these syntax differences really show up the fact that the symbolic toolbox is simply just a set of glue and bolts holding together two different products. Maybe now that Mathworks have bought Mupad outright these differences will start to go away and Mupad will become much better integrated with MATLAB in the future.

For a more detailed list of the syntax differences see this article from Mathworks – Mupad for MATLAB users.

Of course the people I really worry about are those who have written a lot of code using the Maple syntax of the old toolboxes (both standard and extended). They now face a choice – never upgrade, rewrite their code from the ground up or find another way of hooking into the Maple kernel.

If you have a full copy of Maple installed on your machine then one option open to you is to type symengine into MATLAB which will reward you with a dialogue box allowing you to switch from the Mupad engine to the Maple engine. Obviously you cannot do this if you do not have a copy of Maple installed.

At the moment I am unsure which versions of Maple are compatible with this option and I couldn’t find a definitive list on Maple’s website but I guess that will become clear in time. Alternatively you may choose to use Maplesoft’s new product – The Maple toolbox for MATLAB – but from what I can tell that isn’t fully backwards compatible with the old symbolic toolboxes either.

Something I am unsure of at the moment is whether or not you could get the 2008b symbolic toolbox to use the Maple kernel from an older version of the toolbox should you have one lying around. Expect to see an update on this question soon.

While the old version of the symbolic toolbox for MATLAB only had access to a limited number of Maple functions, this new version contains everything in Mupad and when I say everything I really mean it. Type mupad into MATLAB and you are rewarded with nothing less than the full version of Mupad 5.1 – exactly as if you had installed it as normal and started it from the Windows start menu.

It’s all there…programming, graphics – the lot. This is great news for MATLAB users but probably not such good news for old time Mupad users because if they want to upgrade to the latest version of Mupad then they need to buy both MATLAB and the Symbolic Toolbox – a combination that is more expensive than Mupad on it’s own used to be.

There is so much great stuff in Mupad, an application I had never used before until now, that this review almost didn’t get written because I was having too much fun playing.

Summary

So..this update of the symbolic toolbox for MATLAB is a biggie and no mistake! Thanks to the Maple->Mupad brain transplant it is a completely different product and I sometimes find myself wondering if it should have been given a totally different name from ‘symbolic toolbox’ to reflect this.

On the whole, I like it. It’s probably going to cause me a world of pain when I am asked to help people migrate from the old version but I am hopeful that it’ll be worth it in the long run. Here’s the executive summary

• The majority of the input syntax for the symbolic toolbox ‘wrapper’ commands such as syms, int and diff is identical to older versions.
• The output given by these commands is different from the old version in almost every case. The differences range from simple re-arrangements through to completely different evaluations.
• The actual symbolic work is now done by Mupad. In the past it was done by Maple.
• The new toolbox seems to be weaker at integral calculus than the old one but I cheerfully admit that I haven’t exactly gone into detail on this.
• The Extended Symbolic Toolbox is no more. If you need full access to Maple commands then you need to buy the full version of Maple.
• You get the full version of Mupad included with the new symbolic toolbox. Just type mupad at the MATLAB command prompt to launch it.
• The only way of buying Mupad these days is to buy MATLAB and the symbolic toolbox.

Finally – since I mentioned my job in this review I would like to make the usual disclaimer. This article (and indeed this blog) has nothing to do with my employer, The University of Manchester. All opinions are mine and mine alone – not theirs.

Problem of the week #4

October 24th, 2008

I haven’t done a ‘problem of the week’ for a while so I thought I would throw a fun one out there to see what happens. Prove (or otherwise) that 0.9 recurring (that is 0.999999999999…… etc) is equal to one.

Update: Several solutions have been posted in the comments section

October 21st, 2008

I can’t beleive I missed this one! I knew that Mathworks were ditching Maple in favour of MuPad for their symbolic toolbox but I didn’t realise that they liked it so much that they bought the company. I wonder how this is going to affect other products, such as Mathcad and Scientific Workplace, that have adopted MuPad as a symbolic engine? Will Mathworks continue to license the technology to their competitors or will the likes of Mathcad have to find an alternative?

Recent downtime

October 21st, 2008

Sorry about the recent downtime – we encountered some problems while upgrading to the latest version of WordPress. Normal service has been resumed.

41st Carnival of Maths over at 360

October 13th, 2008

The 41st Carnival of Maths is now available over at 360 and includes two articles from Walking Randomly. Don’t let that put you off though as there is loads of other great stuff for your reading pleasure.

GNU Octave version 3.03 released October 10th

October 13th, 2008

There have been loads of mathematical software releases recently and here is another – version 3.03 of Octave – the free, open-source MATLAB like maths package. No major new features this time as it is essentially a bug fix release.

MATLAB 2008b released

October 13th, 2008

The Mathworks released the latest version of their flagship product, MATLAB 2008b, on October 9th. Along with the usual wide range of incremental improvements, there are one or two major changes such as a complete overhaul of the symbolic toolbox and a new Econometrics toolbox.

Other posts you may be interested in.
Review of the new Symbolic Toolbox in MATLAB 2008b

Review of Maple 12 on Macworld

October 13th, 2008

Flip Phillips, Professor of Psychology and Neuroscience at Skidmore College, has reviewed Maple 12 running on a Mac over at MacWorld. The Exploration Assistant sounds rather interesting from my point of view – if only I could find the time to have a play with it. It seems that Flip is rather impressed with this new version so why not head over there to read more?

International Mathematica User Conference 2008

October 11th, 2008

Just a quick note to say good luck (not that’ll she’ll need it I’m sure) to Maria of Teaching College Math Technology fame who will be giving a talk at the upcoming International Mathematica User Conference in Champaign, Illinois. Her talk will be on Online Calculus and Wolfram Demonstrations which is right up my street so I am sorry I can’t be there. I almost managed to get a place as my boss thought I should go but we fell down at the vital ‘getting funding’ hurdle.

To be expected I guess – we are in the middle of a credit crunch (click this video – very funny!) after all.

More excuses for celebration thanks to Wolfram Demonstrations

October 9th, 2008

Right now we are all being subjected to some pretty depressing news. Flicking through today’s newspaper I see article after article on things like the credit crunch, the global energy crisis and impending recession. In the face of all this doom and gloom what we need is as many excuses to celebrate as possible.

Birthdays are a great excuse for celebration – a night out (or in) with friends, good food and maybe a glass (or three) of wine but the problem with birthdays is that they only come around once a year. Once a year, that is, if you only consider Earthly years but if you expand the net to include Martian years, Jovian years and, best of all, Mercurian years then you get a whole new set of celebration dates for your calendar.

The problem you are now faced with is working out when your next Martian birthday is. That’s where this Wolfram Demonstration from Chris Boucher comes in handy. Simply enter your birthdate and you’ll instantly get told your age as it would be on the various planets of the solar system. Despite it’s recent demotion from planetary status, Pluto has been included as well, and rightly so in my opinion. As an added bonus you’ll get told when your next birthday will be on each planet – helping you to fill up that all important celebration calendar. Cheers!