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.
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.
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 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
=3These operations work for both numeric and symbolic vectors unless noted otherwise:
a
, b
;
product of scalar c
and vector a
a+b; c*a
sqrt(dot(a,a))
works better for symbolic vectors)norm(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])
% method 1 using determinant dot(cross(a,b),c)
% method 2 using dot and cross product 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)x = [2 -2 3; 1 -1 2; 1 -2 2]\[10;20;30]
x = [2 -2 3; 1 -1 2]\[10;20]
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
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--'); nice3d
Q1=[1,1,1]; Q2=[2,2,1]; Q3=[3,0,3];
fillpts([Q1;Q2;Q3],'r'); nice3d
parallelepip([1,1,0 ; 0,1,1 ; 1,0,1],'g'); nice3d
plane([1,1,1],[2,2,1]); nice3d
p
and head
at p+v
p = [1 2 3]; v = [1 1 1]
arrow3(p,v); nice3d
P=[1,2,3]; plotpts(P,'o'); nice3d;
texts(P,'P');
circle3(P,N,radius)
circle3tube(P,N,radius)
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 off
t=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])
ezplot3('cos(2*t)','cos(3*t)','cos(5*t)',[0,3],'animate') % animated version
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
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
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 !!
ezcontourc
:ezcontourc('x^2 - y^2',[-2,2,-3,3],[1,-1])
axis equal; axis tight
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;
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=-1Use 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
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 x t real
expr = sin(x)^2*cos(x)^3
*
, /
,
^
, not .*
, ./
,
.^
x
diff(expr,x)
x
int(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)
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;