Tag Archives: Cylindrical coordinate system

It’s been a little quiet on the screencasting front lately, but in the next couple of weeks my colleague teaching Calculus III will be hitting material for which I volunteered to provide some content: namely, using MATLAB to visualize some of the surfaces and solids used in multiple integration. Yesterday, I finished two of these. The first on is on polar coordinates and polar function plotting in MATLAB:

MATLAB doesn’t provide a built-in function for plotting in cylindrical coordinates. Instead — and this is either ingenious or annoying depending on how you look at it — to plot something in cylindrical coordinates, you generate all the points you need in cylindrical coordinates and then use the pol2cart function to convert them en masse to cartesian coordinates, then plot the whole thing as usual in cartesian coordinates.

I think this is smart, since by avoiding the use of a specialized function for cylindrical plots and sticking instead to a single command for 3D plotting, you learn one command for all 3D plots and you get to use all the extras available, such as adding a contour plot onto the cylindrical plot. Overloading the pol2cart function so that it can accept and produce the third coordinate makes this all work. Overall I like how MATLAB doesn’t try to make a function for everything but rather creates a well-featured set of relatively simple tools that will do lots of things.

But I can see where some people — especially MATLAB novices — would find all this annoying, since the entire process takes several steps. There’s a workflow diagram for doing this in the screencast, but a better way is to make an M-file that holds all the steps. Here’s the one I flashed briefly at the end of the screencast:

It would be simple enough to modify this so that it’s a function rather than a script, accepting the arrays theta and r and a function handle for z, and then producing the 3D plot. Or, one could even make an “ez” version where the user just enters a string containing the function s/he wants. If somebody wants to try that out, and you want to share your results, just put the source code in the comments.

The third one in this series will be up later this weekend. It’s on spherical coordinates and it’s pretty much the same process, only using sph2cart instead of pol2cart. There might be a fourth one as well, dealing with some special cases like constant cylindrical/spherical functions (you can’t just say “rho = 5”, because rho has to be a matrix) and how to plot not just the surfaces but the volumes underneath them.