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.
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.
a =
sym([2, 3, 4])syms a1 a2 a3 real % declare a1 a2 a3 as real symbolic
parameters
a = [a1, a2, a3]a=sym(2); expr=sin(sqrt(a))
double(expr)syms x; a = 2+x; expr=sin(sqrt(a))
subs(expr,x,3) % evaluate expr for x=3These operations work for both numeric and symbolic vectors unless noted otherwise:
a, b;
product of scalar c and vector aa+b; c*anorm(a) instead)sqrt(dot(a,a))dot(a,b)cross(a,b)det(A)
Example: volume of parallelepipeda = [1,2,3]; b = [1,-2,1]; c = [2,4,1];
det([a;b;c])dot(cross(a,b),c)X = [x1, x2, x3] x1, x2, x3 must have the same
number of rows)Y = [y1; y2; y3] y1, y2, y3 must have the same
number of columns)nice3d after your
plotting commands. This makes x,y,z-units the same size, uses perspective
and shows a box and grid around objects. After you add things to the graph
with hold on you should type again nice3d
.nice3d you can rotate 3D
graphs by dragging with the mouse. If you let go of the mouse button while
you are still dragging the graph will keep rotating.hold
on and hold off, e.g.,
arrow3([0 0 0],[1 2 3]); hold on; plane([1 2 3],[1 1 1]);
hold off
double before
plotting:
p = sym([1,2,3]); v = sym([1,1,1])
arrow3(double(p),double(v)); nice3d
P1=[3,4]; P2=[2,1]; P3=[1,2];
plotpts([P1;P2;P3],'o-')P1=[3,4]; P2=[2,1]; P3=[1,2];
fillpts([P1;P2;P3],'g')arrow([2,1],[3,4])Q1=[1,1,1]; Q2=[2,2,1]; Q3=[3,0,3];
plotpts([Q1;Q2;Q3],'o--'); nice3dQ1=[1,1,1]; Q2=[2,2,1]; Q3=[3,0,3];
fillpts([Q1;Q2;Q3],'r'); nice3dplane([1,1,1],[2,2,1]); nice3dp and head
at p+vp = [1 2 3]; v = [1 1 1]
arrow3(p,v); nice3dP=[1,2,3]; plotpts(P,'o'); nice3d;
texts(P,'P');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).
.*, ./,
.^ instead of *, /,
^ when you want to evaluate expressions in x,
y, z element by element.
x=0:.1:20; y=sin(x).^2.*cos(x).^3;
plot(x,y) t=0:.01:10; x=sin(3*t); y=sin(t).*cos(5*t);
plot(x,y)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 mousehold on; comet3(x,y,z); hold offt=0:.01:3; x=cos(2*t); y=cos(3*t); z=cos(5*t);
tubeplot3(x,y,z); nice3d[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
legendHere the functions are specified as strings or a symbolic expressions.
ezplot('sin(x)^2*cos(x)^3',[0,20])ezplot('sin(3*t)','sin(t)*cos(5*t)',[0,10])ezplot3('cos(2*t)','cos(3*t)','cos(5*t)',[0,3])ezsurf('sin(x)*sin(y)',[0,6,3,9]) %
plot as surface
ezcontour('sin(x)*sin(y)',[0,6,3,9]);
colorbar % plot level curvesezcontourc('sin(x)*sin(y)',[0,6,3,9],20);
colorbar % plot 20
level curves
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
!!
ezcontourc:ezcontourc('x^2 - y^2',[-2,2,-3,3],[1,-1])
axis equal; axis tightcontour, 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;ezcontourc('x^2-y^2',[-2,2,-3,3],[1,1]) or
contour(x,y,F,[1,1])[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
/4
u
/2 and 0
v
3
/2.
syms x t realexpr = sin(x)^2*cos(x)^3 *, /,
^, not .*, ./,
.^xdiff(expr,x)xint(expr,x)x going from 0 to 3q = int(expr,x,0,3)double(q)simplify(expr)simplify to get the desired
answer.expr from aboveexpr1 = int(expr,x)
expr2 = diff(expr1,x)expr2 should be the same as expr, but it
looks very different. We can show that they are the same bysimplify(expr2-expr)0..* , ./ ,
.^ instead of * , / ,
^ :string = 'sin(x).^2.*cos(x).^3' quadl(string,0,3) quad8 instead of quadl.)expr to a
string:syms x; expression = sin(x)^2*cos(x)^3
string = char(vectorize(expression))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
!!
[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;