MATLAB Tips

Contents

File names for .m files

Do not have a space or a hyphen in the names of your .m files. If you have a space or hyphen, when you publish to html MATLAB will give an error message when it tries to execute the first command and won't try to execute any later commands.

The exponential function and the number e

MATLAB (and most mathematical software) knows the exponential function $e^x$ as exp(x) so the number e in MATLAB is exp(1).

The natural logarithm ln

MATLAB (and most mathematical software) knows the natural logarithm $\mbox{ln}(x)$ as log(x).

The quadl command

If MATLAB doesn't recognize the quadl command, make sure that you have typed it correctly. It ends with the letter el. You probably typed the number 1.

Error messages from solve

The solve command takes as its input a symbolic expression, not an equation and assumes that the expression is equal to 0. Suppose you want to solve the equation x^2-5*x=6. The command solve(x^2-5*x=-6) gives the error message

??? solve(x^2-5*x=-6)
                              |
Error: The expression to the left of the equals sign is not a valid target for an assignment.

and solve(x^2-5*x+6=0) gives a similar error message. To solve the equation, give the command:

solve(x^2-5*x-6)

Or you can give the solve command an equation enclosed in single quotes:

solve('x^2-5*x=6')

Plotting a constant function with ezplot

ezplot requires a variable in a symbolic expression. So, for example, to use it to plot the constant function 5, you can write 5 as 5+0*x and plot that.

syms x
ezplot(5+0*x)

Alternatively, you can create an anonymous constant function and plot that.

f = @(x) 5
ezplot(f)
f = 

    @(x)5

New figures (plot windows)

You can get a new figure window by giving the command

figure

Here is the new blank (and invisible) figure.

Give the command before giving the command for the plot you want to appear in the new window.

Preventing MATLAB from putting a new plot on top of an old one

The first thing is to try is the command hold off before the new plot command. If MATLAB ignores hold off (which it does occasionally), give the command

clf

to clear the plot window. Give the command before giving the command for the plot you want to appear in the new window.

Plotting surfaces

To plot a surface, you may want to think of the surface as parametrized. Since it is two dimensional, there will be two parameters. To parametrize the surface given by the equation f(x,y,z)=0, you need functions x(s,t), y(s,t) and z(s,t) so that f(x(s,t),y(s,t),z(s,t))=0. For example, for the ellipsoid $4x^2+9y^2+36z^2=36$ I can use $(3\cos(s)\sin(t),2\sin(s)\sin(t),\cos(t))$ with $0 \le s\le 2\pi,\ 0\le t \le \pi.$

(This is just spherical coordinates, calling the angles s and t, which are shorter to type than theta and phi.) One way to plot this is to use ezsurf or ezmesh. You can do this with the commands

syms s t
ezsurf(3*cos(s)*sin(t),2*sin(s)*sin(t),cos(t))

which gets you a solid shaded surface or with

syms s t
ezmesh(3*cos(s)*sin(t),2*sin(s)*sin(t),cos(t))

which gets you a shaded mesh surface.

Another way to plot this surface is to use surf or mesh, which gives you more control over the plot. You start by creating a grid of points (the parameter values which will be used for plotting).

[s,t]=meshgrid(0:.02*pi:2*pi,0:.02*pi:pi);

You can then plot it with surf or mesh

surf(3*cos(s).*sin(t),2*sin(s).*sin(t),cos(t))

Notice that since I am using a grid, I am effectively working with matrices; I need to multiply two of them component by component (not using ordinary matrix multiplication), so I need a period after the first one.

Plotting more than one surface

To plot more than one surface you have to use the mesh or surf commands - you can't use ezsurf or ezmesh for the first surface because it will undo the effect of a hold on command. For example, to plot the ellipsoid above and the cylinder $x^2+y^2 = 1,$ parametrized by $(cos(u),sin(u),v), \ 0\le u \le 2\pi, -2\le v \le 2$ you can use the following commands:

[s,t]=meshgrid(0:.02*pi:2*pi,0:.02*pi:pi);
[u,v]=meshgrid(0:.05*pi:2*pi,-2:.2:2);

and then

surf(3*cos(s).*sin(t),2*sin(s).*sin(t),cos(t)), axis equal, hold on
surf(cos(u),sin(u),v), hold off

or

mesh(3*cos(s).*sin(t),2*sin(s).*sin(t),cos(t)), axis equal, hold on
mesh(cos(u),sin(u),v), hold off

or use mesh for one surface and surf for the other, so here's my favorite version of the plot:

mesh(3*cos(s).*sin(t),2*sin(s).*sin(t),cos(t)), axis equal, hold on
surf(cos(u),sin(u),v), hold off

The axis equal command makes the scaling the same on all of the axes. If you leave it out, the right circular cylinder would appear to be elliptical instead of circular.

Setting the range on the axes

In 2 dimensions, you can give the command axis([xmin xmax ymin ymax]) and in 3 dimensions, you can give the command axis([xmin xmax ymin ymax zmin zmax]) to set the range on the axes.

Solving a system of equations

You can use solve to solve a system of equations. For example, to solve $x^2-y=0,\ y-3x=7$

you can enter the commands:

syms x y
[x,y] = solve('x^2-y=0','y-3*x=7')
 
x =
 
 37^(1/2)/2 + 3/2
 3/2 - 37^(1/2)/2
 
 
y =
 
 (3*37^(1/2))/2 + 23/2
 23/2 - (3*37^(1/2))/2
 

Notice that you get two values for each variable. There are two solutions to this system, (x(1),y(1)) and (x(2),y(2)).

A command for rotating a 3 dimensional plot

This can be done This can be done with the command view. If you rotate the plot using the tool for rotating, you will see the new azimuth and elevation in the lower left corner of the plot window. For example, if you see Az: -30 El: 28, the command view([-30 28]) will give you the same view. For example, here is a rotated view of the last plot above.

mesh(3*cos(s).*sin(t),2*sin(s).*sin(t),cos(t)), axis equal, hold on
surf(cos(u),sin(u),v), hold off
view([-56 68])

Error messages in viewSolid

If you get the following error messages from viewSolid

??? Attempted to access f(-1); index must be a positive integer or logical. Error in ==> viewSolid>oldviewSolid at 44 YY = f(xx)*ones(1, 21)+((g(xx)-f(xx))/20)*(0:20);

Error in ==> viewSolid at 37 oldviewSolid(char(xvar), double(a), double(b), ...

they are probably the result of using a constant as the lower y limit. The code for viewSolid treats the lower y limit as an inline function of x, and the independent variable in an inline function has to appear explicitly. You can fix this by adding 0*x to the lower y limit, so, for example, if the solid is a cube with 1 or -1 as each coordinate of the vertices, the command viewSolid(z,-1,1,y,-1,1,x,-1,1) produces the error messages above, but

syms x y z
viewSolid(z,-1,1,y,-1+0*x,1,x,-1,1)

works fine.

Fixing a bad pagebreak in your html file

If you have a bad pagebreak in your html file and want to fix it by making sure there's a pagebreak at a particular place, insert the following lines in your m-file where you would like a pagebreak:

%%
%
% <html>
% <p style="page-break-before: always">
% </html>
%

Suppressing warnings

You may want to do this before printing a published m-file, but I don't recommend suppressing warnings at other times, because they might warn you about something you should pay attention to.

The command warning off will suppress warnings. The command warning on will turn warnings back on.

Suppressing steps of simple

If you just want to see the final output from applying the command simple to the expression x^3+3*x^2+3*x+1 and not all the ways MATLAB attempted the simplification, give the result a name, say A and then give the commands

A=simple(x^3+3*x^2+3*x+1);

A

If you would also like to know how MATLAB did the simplification, give the command

[A,how]=simple(x^3+3*x^2+3*x+1)