## Review of the new Symbolic Toolbox in MATLAB 2008b

October 27th, 2008 | Categories: math software, matlab | Tags:

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.

Bonus! Full version of Mupad!

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.

1. I think that using MATLAB for symbolic math is not exactly what Matlab was built for. I would never cough up more money for the package. Maxima is a pretty good open source symbolic math.
Moreover, I hate having to declare the symbolic variables in matlab; sometimes if you forget, those annoying errors would pop up.

2. Thanks for this interesting and very informative article. My college is going to be purchasing 50 licenses for MATLAB and the Symbolic Toolbox in early 2009 for use in our mathematics curriculum. We currently use Maple for symbolic stuff. It looks like with this switch to MuPad, we’ll have a choice between straight Maple functionality or the MuPad version from within MATLAB, and to me that’s a win-win situation.

3. Hi Robert

I agree – if you already have Maple then this move by Mathworks is a good one. There is a lot of nice functionality in Mupad and I will be covering some of it on here in the future. If you have any particular topics in mind that you would like more info on then let me know and I will add them to the list of possible future articles.

Cheers,
Mike

4. i am a student,I can not afford to buy any one of Mathematica/matlab/maple/MuPAD,so i am using scilab,xmaxima,R,though i knew quite well that they are so excellent.

5. You can also open MuPad and use its interface instead of Matlab’s by typing ‘mupad’ or going through ‘Start’ menu.

6. I found a bug in Mupad/R2008b right off the bat:
Previous versions using the Maple engine give:
>> int((sin(x))^2/x,x)
ans =
1/2*log(x)-1/2*cosint(2*x)

Fails under R008b!

7. I use both Maple and Matlab – apparently you can get the toolbox to join Maple and MATLAB free from the Maple site:
http://www.maplesoft.com/products/maplematlab/index.aspx
Hopefully this will help solve some of the issues you might encounter if you are used to using the symbolic toolbox with Maple.

8. Thank you for taking the time to provide this review! It was very informative.
There were some questions that you had regarding compatibility with Maple, and whether or not the Maple Toolbox for Matlab was backwards compatible. I hope you don’t mind, but I wanted to take the opportunity to help answer those questions for you.

With respect to compatible versions, Maple had to be updated to accommodate the changes made in Matlab in 2008b so the most recent version (Maple 12.01) is the only version of Maple that is compatible with this most recent version of Matlab and the new Symbolic Math Toolbox. There are more details available here: http://www.maplesoft.com/mtm/index.aspx
As for backwards compatibility, the Maple Toolbox for Matlab is the only way to ensure full compatibility with all of your solutions created with older versions of the Symbolic Math Toolbox. If you have access to Maple and Matlab, you can request your own copy of the Maple Toolbox for Matlab from Maplesoft – it is offered free of charge for the rest of 2008.
If you do find an instance where an incompatibility exists, I would appreciate it if you could notify our Technical Support Team: support@maplesoft.com

Thanks again for the review. I always enjoy reading your blogs!

Cheers,
Christina Spirou
Product Director, Maplesoft R&D

9. Hi Christina

Thanks for your kind words and for the extra information. Unfortunately, I do not have my own copy of Maple so I can’t evaluate the Maple toolbox but I know that some people at my University do and so this info might be useful to them.

Best Wishes,
Mike