Matlab Instructions for Math 241

You must first download the following files

plane.m, plotpts.m, fillpts.m, nice3d.m, nice3dn.m, arrow.m, arrow3.m, parallelepip.m, circle3.m, circle3tube.m, texts.m,tubeplot3.m, isosurf.m, ezcontourc.m , ezsurfvs.m, ezsurfhs.m, ezsurfpol.m, ezsurfspher.m, ezsurfpar.m

and put them in the same directory as your m-files (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.

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.

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 y real; u = x^2; v = x - y/x
subs(u,x,2)       %
evaluate u for x=2
subs(v,{x,y},{2,3}) % evaluate v for x=2, y=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 (sometimes sqrt(dot(a,a)) works better for symbolic vectors)
norm(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])      
% method 1 using determinant
dot(cross(a,b),c)   % method 2 using dot and cross product
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)

Solving linear equations

Find [x1; x2; x3] such that
    2·x1 -2·x2 + 3·x3 = 10
    1·x1 -1·x2 + 2·x3 = 20
    1·x1 -2·x2 + 2·x3 = 30
x = [2 -2 3; 1 -1 2; 1 -2 2]\[10;20;30]
Find one of the many solutions [x1; x2; x3] such that
    2·x1 -2·x2 + 3·x3 = 10
    1·x1 -1·x2 + 2·x3 = 20
x = [2 -2 3; 1 -1 2]\[10;20]
This also works also with symbolic vectors, but it may pick a different solution.

Graphing points, lines, polygons, parallelepipeds, planes, arrows, circles

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
parellepiped in 3D: plot parallelepiped with vertex in (0,0,0) and vectors a=(1,1,0), b=(0,1,1), c=(1,0,1) in green
parallelepip([1,1,0 ; 0,1,1 ; 1,0,1],'g'); 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');
circles in 3D with center P and normal vector N:
circle3(P,N,radius)
circle3([1,2,3],[2,-1,3],5); nice3d
circle3tube(P,N,radius)
circle3tube([1,2,3],[2,-1,3],5); nice3d

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])
ezplot3('cos(2*t)','cos(3*t)','cos(5*t)',[0,3],'animate') % animated version
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 surfaces over regions with Cartesian, polar, or spherical coordinates

Plot graph of f(x,y) over vertically simple region a≤x≤b, g1(x)≤y≤g2 (x):
ezsurfvs(f,a,b,g1,g2,x,y)
Example: top half of sphere with radius 1
syms x y

ezsurfvs(sqrt(1-x^2-y^2),-1,1,-sqrt(1-x^2),sqrt(1-x^2),x,y); nice3d

Plot graph of f(x,y) over horizontally simple region c≤y≤d, h1(y)≤x≤h2 (y):
ezsurfhs(f,c,d,h1,h2,x,y)
Example: top half of sphere with radius 1
syms x y

ezsurfhs(sqrt(1-x^2-y^2),-1,1,-sqrt(1-y^2),sqrt(1-y^2),x,y); nice3d

Plot graph of f in polar coordinates r,theta over region a≤theta≤b, h1(theta)≤r≤h2 (theta):
ezsurfpol(f,a,b,h1,h2,r,theta)
Example: plane z=x over circle of radius 1 with center (1,0):
syms r theta

ezsurfpol(r*cos(theta),-pi/2,pi/2,0,2*cos(theta),r,theta); nice3d

For spherical coordinates rho,phi,theta: Plot surface given by rho = f(phi,theta) over region a≤theta≤b, h1(theta)≤phi≤h2 (theta):
ezsurfspher(f,a,b,h1,h2,phi,theta)
Example: plot part of sphere with center (0,0,0), radius 1
syms phi theta

ezsurfspher(1,0,pi/2,0,pi/2,phi,theta); nice3d

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 =... 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

Plotting parametric surfaces

Case 1: rectangular region for (u,v): a≤u≤b, c≤v≤d

Use ezsurf(f1,f2,f3,[a,b,c,d])

Example: Plot part of sphere ( sin(u)cos(v) , sin(u)sin(v) , cos(u) ) where π/4 ≤ u ≤ π/2 and 0 ≤ v ≤ 3π/2

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.

Case 2: region for (u,v): a≤u≤b, g1(u)≤v≤g2(u)

Use ezsurfpar(f1,f2,f3,a,b,g1,g2,u,v)

Example: Plot part of cylinder y2 + z2 = 1 with -2≤x≤z
( v , cos(u), sin(u) ) where 0 ≤u≤2π and -2≤v≤sin(u)

syms u v
ezsurfpar(v,cos(u),sin(u),0,2*pi,-2,sin(u),u,v); nice3d

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)
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;