The Justin Guide to Matlab for MATH 241 Part 3.

Contents

Multiple Integrals

Since Matlab does integrals so well this is easy, we just nest the integrals. For example consider the following. Read it carefully from the innermost int outwards. Remember that when we do int(f,x,a,b) we integrate f with respect to x from a to b. Here a and b may also contain other variables.

clear all;
syms x y z;
int(int(int(x^2*y+z,z,x+y,x),y,0,2*x),x,-1,2)
 
ans =
 
-81/2
 

Plotting Surfaces - Continued

In section 14.9 we learned how to parametrize surfaces as vector-valued functions of two variables. This is perfect for the ezsurf command we have seen.

For example consider the portion of the cylinder x^2+y^2=4 between z=0 and z=5. In class we parametrized this (or something like this) using r(theta,z) and so we can do the same thing easily here:

clear all;
syms theta z;
rbar = [2*cos(theta),2*sin(theta),z];
ezsurf(rbar(1),rbar(2),rbar(3),[0,2*pi,0,5])
view([10 10 10])

A few things to note:

Here are some other examples. First the portion of the cone z=sqrt(x^2+y^2) having -3<=x<=1 and -2<=x<=1.

clear all;
syms x y;
rbar = [x,y,sqrt(x^2+y^2)];
ezsurf(rbar(1),rbar(2),rbar(3),[-3,1,-2,1])
view([10 10 10])

Here is the portion of the sphere of radius 2 inside the cone phi=pi/6:

clear all;
syms phi theta;
rbar = [2*sin(phi)*cos(theta),2*sin(phi)*sin(theta),2*cos(phi)]
ezsurf(rbar(1),rbar(2),rbar(3),[0,pi/6,0,2*pi])
view([10 10 10])
 
rbar =
 
[ 2*cos(theta)*sin(phi), 2*sin(phi)*sin(theta), 2*cos(phi)]
 

Here is something really cool, a spiral!

clear all;
syms theta z;
rbar = [theta*cos(theta),theta*sin(theta),z];
ezsurf(rbar(1),rbar(2),rbar(3),[0,6*pi,0,2])
view([10 10 10])

Vector Fields

Matlab can plot vector fields using the quiver command, which basically draws a bunch of arrows. This is not completely obvious though. First we have to set up a grid of points for which to plot arrows. In other words we have to tell it for which x and y to actually draw the vector field.

Suppose we want our grid to have x going from -5 to 5 in steps of 1 and the same for y. We first do the following:

clear all;
[x,y]=meshgrid(-5:1:5,-5:1:5);

Now suppose we wish to plot the vector field F(x,y)=(y/5)i-(x/5)j. To do this we type

quiver(x,y,y/5,-x/5,0);

A window will pop up with the vector field on it. The 0 at the end just ensures that Matlab does not do any tricky rescaling of the vectors, something it usually does to make things fit nicely.

Just so you know the first two entries x,y indicate that vectors should be placed at x,y. The second two entries form the vector itself.

A note of caution. Suppose we want the vector field F(x,y)=(y/5)i-(x/y)j. Since x and y are collections of numbers, to divide them we cannot use /, we must use ./ instead. This is a Matlab quirk since we are not working with individual numbers. Thus we would need

quiver(x,y,y./5,-x./y,0)

Note that this is a pretty ugly vector field. Try it!

Vector Fields in 3D

For 3D vector fields we use quiver3. Here is an example. It's the only one we'll do because they can be pretty overwhelming.

[x,y,z]=meshgrid(-5:2:5,-5:2:5,-5:2:5);
quiver3(x,y,z,-y./sqrt(x.^2+y.^2),x./sqrt(x.^2+y.^2),z./5,0)
view([10 10 10])

Line Integrals of Functions.

Line integrals of functions are really easy - we just tell Matlab exactly what to do, step by step. For example here is example 1 from page 1006 of the text.

clear
syms t x y z;
rbar=[t,-3*t,2*t];
f=x+y^2-2*z;
mylength=@(u) sqrt(u*transpose(u));
mag=simplify(mylength(diff(rbar,t)));
sub=subs(f,[x,y,z],rbar);
int(sub*mag,t,0,1)
 
ans =
 
(3*14^(1/2))/2
 

Read this carefully to see how it works! Especially note the line mylength=... which creates a new length function for vectors. The Matlab norm command only works on numerical vectors and not on vectors with variables in them. Our new mylength function will do the job. Even if you don't understand it for now just use it - all it does is finds the norm of a vector even if that vector contains variables.

Line Integrals of Vector Fields.

Just like line integrals of functions these are easy, we just tell Matlab exactly what we want it to do. Here's example 6 from page 1011 of the text. Again read carefully and understand!

clear all
syms t x y z;
rbar=[t,t^2,t^3];
F=[x*y,3*z*x,-5*x^2*y*z];
sub=subs(F,[x,y,z],rbar);
int(dot(sub,diff(rbar,t)),0,1)
 
ans =
 
-1/4
 

Surface Integrals of Functions

The nice thing about having a surface in Matlab like this is that now we can do things with it. For example we know that if we want to integrate a real-valued function over a surface we need the magnitude of the cross products of the derivatives of r. Here is how we do just that bit:

clear all;
syms phi theta;
rbar=[2*sin(phi)*cos(theta),2*sin(phi)*sin(theta),2*cos(phi)];
mylength=@(u) sqrt(u*transpose(u));
simplify(mylength(cross(diff(rbar,phi),diff(rbar,theta))))
 
ans =
 
4*(sin(phi)^2)^(1/2)
 

So now here's the really nice thing. Suppose we want to integrate the function f(x,y,z)=x^2+y^2 over that surface. This involves plugging the i,|j|,|k| components in for x, y, z, multiplying by that magnitude and integrate over the appropriate limits. Here it is all together:

clear all
syms phi theta x y z;
rbar=[2*sin(phi)*cos(theta),2*sin(phi)*sin(theta),2*cos(phi)];
f=x^2+y^2;
mylength=@(u) sqrt(u*transpose(u));
mag=simplify(mylength(cross(diff(rbar,phi),diff(rbar,theta))));
sub=subs(f,[x,y,z],rbar);
int(int(sub*mag,phi,0,pi/2),theta,0,3*pi/2)
 
ans =
 
16*pi
 

Make sure you read this carefully (after the clear all) to see what each line does. The first line sets the symbolic variables. The second line is the parametrization of the surface. The third line is the function to integrate. The fourth line creates a length function we'll need later. The fifth line find the magnitude of the cross product of the derivatives. The sixth line substitutes the components from the parametrization into the real-valued function we want to integrate. The seventh and final line does the double integral required.

Surface Integrals of Vector Fields.

Similarly we can take the surface integral of a vector field. We only need to be careful in that Matlab can't take care of orientation so we'll need to do that and instead of needing the magnitude of the cross product we just need the cross product. Here is problem 6 from the 15.6 exercises.

clear
syms theta x y z;
rbar=[cos(theta),y,sin(theta)];
F=[x,y,z];
kross=simplify(cross(diff(rbar,theta),diff(rbar,y)));
sub=subs(F,[x,y,z],rbar);
int(int(dot(sub,kross),y,-2,1),theta,0,2*pi)
 
ans =
 
(-6)*pi
 

Again read this carefully. After the clear the first line sets the symbolic variables. The second sets the parametrization and the third sets the vector field. The fourth finds the cross product of the derivatives. The fifth substitutes the parametrization into the vector field. The sixth does the double integral of the dot product as required for the surface integral of a vector field.

The end.