Matlab Instructions for Math 241

You must first save the files plane.m, plotpts.m, fillpts.m, nice3d.m, arrow.m, arrow3.m, texts.m,tubeplot3.m, isosurf.m, ezcontourc.m in your home directory (or where Matlab can find them) before you can use the commands below.

Use help commandname for additional information about a command .

Please try these commands on a computer. You can copy and paste from the browser window to the Matlab window.

Contents

Numeric vs. symbolic operations, entering vectors

Matlab can either operate numerically or symbolically.

Numeric operations use floating point numbers with about 15 digits and an exponent between -308 and 308. They are very fast, but each operation has a certain roundoff error, and these roundoff errors can accumulate. Matlab displays numeric vectors without brackets.

Entering a numeric vector: a = [2,3,4]

Symbolic operations use integers, fractions, roots and symbolic parameters. Symbolic operations will give you an exact answer, but for more complicated problems symbolic operations are very slow and lead to huge expressions, or might not work at all. Matlab displays symbolic vectors with brackets.

Entering a symbolic vector with numbers: a = sym([2, 3, 4])
Entering a symbolic vector with symbolic parameters:
syms a1 a2 a3 real    % declare a1 a2 a3 as real symbolic parameters
a = [a1, a2, a3]
Evaluate a symbolic expression numerically:
a=sym(2); expr=sin(sqrt(a))
double(expr)
Substitute a value for a symbolic parameter:
syms x; a = 2+x; expr=sin(sqrt(a))
subs(expr,x,3)        %
evaluate expr for x=3

Operations with vectors and matrices

These operations work for both numeric and symbolic vectors unless noted otherwise:

sum of two vectors a, b; product of scalar c and vector a
a+b; c*a
norm of vector (for numeric vectors you can also use norm(a) instead)
sqrt(dot(a,a))
dot product of two vectors
dot(a,b)
cross product of two vectors of length 3
cross(a,b)
determinant of a square matrix
det(A)
Example: volume of parallelepiped
a = [1,2,3]; b = [1,-2,1]; c = [2,4,1];
det([a;b;c])

dot(cross(a,b),c)
make new matrix by putting vectors or matrices side by side
X = [x1, x2, x3]
(x1, x2, x3 must have the same number of rows)
make new matrix by putting vectors or matrices on top of each other
Y = [y1; y2; y3]
(y1, y2, y3 must have the same number of columns)

Graphing points, lines, polygons, planes, arrows

points and lines in 2D: plot points (3,4), (2,1), (1,2) as circles, connect with solid lines
P1=[3,4]; P2=[2,1]; P3=[1,2];
plotpts([P1;P2;P3],'o-')
filled polygons in 2D: plot triangle with vertices (3,4), (2,1), (1,2) filled with green color
P1=[3,4]; P2=[2,1]; P3=[1,2];
fillpts([P1;P2;P3],'g')
plot arrow in 2D: plot vector (3,4) which starts at point (2,1)
arrow([2,1],[3,4])
points and lines in 3D: plot points (1,1,1), (2,2,1), (3,0,3) as circles, connect with dashed lines
Q1=[1,1,1]; Q2=[2,2,1]; Q3=[3,0,3];
plotpts([Q1;Q2;Q3],'o--'); nice3d
filled polygons in 3D: plot triangle with vertices (1,1,1), (2,2,1), (3,0,3) filled with red color
Q1=[1,1,1]; Q2=[2,2,1]; Q3=[3,0,3];
fillpts([Q1;Q2;Q3],'r'); nice3d
plot plane through point Q with normal vector N: e.g., Q=(1,1,1), N=(2,2,1)
plane([1,1,1],[2,2,1]); nice3d
plot arrow in 3D with tail at p and head at p+v
p = [1 2 3]; v = [1 1 1]
arrow3(p,v); nice3d
label point in graph
P=[1,2,3]; plotpts(P,'o'); nice3d; texts(P,'P');

Plotting functions and curves using plot, plot3, surf, contour

First construct vectors x, y, z containing x, y, z values. Then plot the points specified by these vectors using plot(x,y) or plot3(x,y,z) (for surf, contour matrices X, Y, Z are used).

Note: You must use .*, ./, .^ instead of *, /, ^ when you want to evaluate expressions in x, y, z element by element.
Plot the function sin(x)2cos(x)3 for x = 0 to 20 using a stepsize of .1
x=0:.1:20; y=sin(x).^2.*cos(x).^3;
plot(x,y)

Plot the 2D curve given by (sin(3t), sin(t)cos(5t)) for t=0 to 10 using a stepsize of .01
t=0:.01:10; x=sin(3*t); y=sin(t).*cos(5*t);
plot(x,y)
Plot the 3D curve given by (cos(2t), cos(3t), cos(5t)) for t=0 to 3 using a stepsize of .01
t=0:.01:3; x=cos(2*t); y=cos(3*t); z=cos(5*t);
plot3(x,y,z); nice3d       %
rotate by dragging with mouse
and animate this curve
hold on; comet3(x,y,z); hold off
Plot the 3D curve as a tube
t=0:.01:3; x=cos(2*t); y=cos(3*t); z=cos(5*t);
tubeplot3(x,y,z); nice3d
Plot the graph of a function f(x,y) of two variables: Plot sin(x)sin(y) for x=0 to 6, y=3 to 9, stepsize of .1
[X,Y] = meshgrid(0:.1:6,3:.1:9);
Z = sin(X).*sin(Y);
surf(X,Y,Z)                %
plot as surface
contour(X,Y,Z)             %
plot as contours
contourf(X,Y,Z); colorbar  %
filled contourplot with legend

Plotting functions and curves using ezplot, ezplot3, ezsurf, ezcontour

Here the functions are specified as strings or a symbolic expressions.

Plot the function sin(x)2cos(x)3 for x = 0 to 20
ezplot('sin(x)^2*cos(x)^3',[0,20])
Plot the 2D curve given by (sin(3t), sin(t)cos(5t)) for t=0 to 10
ezplot('sin(3*t)','sin(t)*cos(5*t)',[0,10])
Plot the 3D curve given by (cos(2t), cos(3t), cos(5t)) for t=0 to 3
ezplot3('cos(2*t)','cos(3*t)','cos(5*t)',[0,3])
Plot a function f(x,y) of two variables: Plot sin(x)sin(y) for x=0 to 6, y=3 to 9
ezsurf('sin(x)*sin(y)',[0,6,3,9])       % plot as surface
ezcontour('sin(x)*sin(y)',[0,6,3,9]); colorbar    %
plot level curves
ezcontourc('sin(x)*sin(y)',[0,6,3,9],20); colorbar  % plot 20 level curves

Plotting level sets of functions of two and three variables

Note: You must use .*, ./, .^ instead of *, /, ^ when you want to evaluate expressions in X, Y, Z element by element.

Do not forget the semicolon after the meshgrid and F=... commands !!

Plot level curves where f(x,y) = x2 - y2 is equal to 1 and -1, in the rectangle -2<x<2, -3<y<3
using ezcontourc:
ezcontourc('x^2 - y^2',[-2,2,-3,3],[1,-1])
axis equal; axis tight
using contour, meshgrid:
[X,Y] = meshgrid(-2:.2:2,-3:.2:3);          % specify grid for x,y
F = X.^2 - Y.^2;                            %
compute function values at grid points
contour(X,Y,F,[1,-1])                       %
plot level curves where f=1, f=-1
axis equal; axis tight;
Note: To only plot level curve where f=1 you must use ezcontourc('x^2-y^2',[-2,2,-3,3],[1,1]) or contour(x,y,F,[1,1])
Plot level surfaces where f(x,y,z) = x2 - y2 - z2 is equal to 1 and -1, in the box -2<x<2, -3<y<3, -3<z<3
[X,Y,Z] = meshgrid(-2:.2:2,-3:.2:3,-3:.2:3);% specify grid for x,y,z
F = X.^2 - Y.^2 - Z.^2;                     %
compute function values at grid points
isosurf(X,Y,Z,F,1);  hold on                %
plot level surface where f=1
isosurf(X,Y,Z,F,-1); nice3d; hold off       %
plot level surface where f=-1
For Matlab version 5: You must specify a color in isosurf, e.g., isosurf(X,Y,Z,F,1,'r')

Plotting parametric surfaces

Example: Plot part of sphere ( sin(u)cos(v) , sin(u)sin(v) , cos(u) ) where pi/4 <= u <= pi/2 and 0 <= v <= 3pi/2.
Using ezsurf:
ezsurf('sin(u)*cos(v)','sin(u)*sin(v)','cos(u)',[pi/4,pi/2,0,3*pi/2]); nice3d
You can use strings or symbolic expressions.

Symbolic differentiation and integration

First you must declare your symbolic variables
syms x t real
Define a symbolic expression, e.g.,
expr = sin(x)^2*cos(x)^3
Note: Use *, /, ^, not .*, ./, .^
Differentiate an expression with respect to variable x
diff(expr,x)
Indefinite integral of an expression with respect to variable x
int(expr,x)
Definite integral with respect to variable x going from 0 to 3
q = int(expr,x,0,3)
Find the numerical value of a symbolic expression
double(q)
Simplify an expression
simplify(expr)
It is often necessary to apply simplify to get the desired answer.
Example: We integrate and differentiate expr from above
expr1 = int(expr,x)
expr2 = diff(expr1,x)
Now expr2 should be the same as expr, but it looks very different. We can show that they are the same by
simplify(expr2-expr)
which gives 0.

Numerical integration

Define the integrand as a string where you must use .* , ./ , .^ instead of * , / , ^ :
string = 'sin(x).^2.*cos(x).^3'
Integrate from 0 to 3:
quadl(string,0,3)
(In Matlab 5 use quad8 instead of quadl.)
How to convert a symbolic expression expr to a string:
syms x; expression = sin(x)^2*cos(x)^3
string = char(vectorize(expression))

Plotting vector fields in 2D

Note: You must use .*, ./, .^ instead of *, /, ^ when you want to evaluate expressions in X, Y element by element.

Do not forget the semicolon after the meshgrid and F=... commands !!

Example: Plot the vector field F(x,y)=(x2-y2, 2xy) for -2<x<2, -3<y<3
[X,Y] = meshgrid(-2:.2:2,-3:.2:3);          % specify grid for x,y
F1 = X.^2 - Y.^2; F2 = 2*X.*Y;              %
compute function values at grid points
quiver(X,Y,F1,F2)                           %
plot vector field F1,F2
axis equal; axis tight;