Justin's Matlab Guide for MATH 241: Comprehensive Guide for Calculus 1, 2, and 3, Study Guides, Projects, Research of Mathematics

A comprehensive guide to using matlab for various calculus problems, including calculus 1, 2, and 3. It covers topics such as running matlab, symbolic expressions, precalculus, taking derivatives and integrals, plotting functions, and working with vectors. The guide includes examples and explanations to help users understand the concepts and apply them in matlab.

Typology: Study Guides, Projects, Research

2018/2019

Uploaded on 12/14/2019

vivi-n
vivi-n šŸ‡ŗšŸ‡ø

1 document

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
The Justin Guide to Matlab
for MATH 241 Part 1.
Table of Contents
Running Matlab .................................................................................................................. 1
Okay, I'm running Matlab, now what? .................................................................................... 1
Precalculus ......................................................................................................................... 2
Calculus 1 .......................................................................................................................... 5
Calculus 2 .......................................................................................................................... 6
Calculus 3 .......................................................................................................................... 7
The basic format of this guide is that you will sit down with this page open and a Matlab window open and you will
try the guide as you go. Once you are finished following through the guide the project should be very easy.
Running Matlab
There are three typical ways to obtain/run Matlab:
1. Free, as a student, through the university. To do this follow the link http://www.it.umd.edu/techsav-
ings/software.html I strongly recommend this way because it guarantees you your own functioning
copy of Matlab on your own machine.
2. Through the Engineering Department's Virtual Lab. To run Matlab this way, follow the link http://
eit.umd.edu/vcl/. You'll need to install Citrix first according to the directions given.
3. In one of the labs on campus.
Okay, I'm running Matlab, now what?
When you run Matlab you will see a bunch of windows. The important one will have a prompt in it which
looks like >>. This is where we will tell Matlab what to do.
Go into this window and type 2+3. You will see:
2+3
ans =
5
Oh yeah, you know Matlab! Let's do something more relevant to calculus like take a derivative. Before
we do that it's important to note that Matlab works a lot with what are called symbolic expressions. If we
want to take a derivative we use the diff command. It's tempting to do diff(x^2) but this errors.
Try it and see!
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Justin's Matlab Guide for MATH 241: Comprehensive Guide for Calculus 1, 2, and 3 and more Study Guides, Projects, Research Mathematics in PDF only on Docsity!

The Justin Guide to Matlab

for MATH 241 Part 1.

Table of Contents

Running Matlab .................................................................................................................. 1 Okay, I'm running Matlab, now what? .................................................................................... 1 Precalculus ......................................................................................................................... 2 Calculus 1 .......................................................................................................................... 5 Calculus 2 .......................................................................................................................... 6 Calculus 3 .......................................................................................................................... 7

The basic format of this guide is that you will sit down with this page open and a Matlab window open and you will try the guide as you go. Once you are finished following through the guide the project should be very easy.

Running Matlab

There are three typical ways to obtain/run Matlab:

  1. Free, as a student, through the university. To do this follow the link http://www.it.umd.edu/techsav- ings/software.html I strongly recommend this way because it guarantees you your own functioning copy of Matlab on your own machine.
  2. Through the Engineering Department's Virtual Lab. To run Matlab this way, follow the link http:// eit.umd.edu/vcl/. You'll need to install Citrix first according to the directions given.
  3. In one of the labs on campus.

Okay, I'm running Matlab, now what?

When you run Matlab you will see a bunch of windows. The important one will have a prompt in it which looks like >>. This is where we will tell Matlab what to do.

Go into this window and type 2+3. You will see:

2+

ans =

5

Oh yeah, you know Matlab! Let's do something more relevant to calculus like take a derivative. Before we do that it's important to note that Matlab works a lot with what are called symbolic expressions. If we want to take a derivative we use the diff command. It's tempting to do diff(x^2) but this errors. Try it and see!

lab for MATH 241 Part 1.

Why does it error? The reason is that Matlab doesn't know what x is and we must tell it that x is a symbol to work with. We do so with the syms command. So we can do the following two commands. The first line tells Matlab that x is symoblic and will be symbolic until we tell it otherwise.

syms x diff(x^2)

ans =

2*x

Eat your heart out Newton and Leibniz! Here's an even uglier derivative. Keep in mind that we don't need a syms x now because x is still symbolic and will remain so. Notice something important - that multiplication in Matlab always needs a *. Leaving this out will give an error.

diff(2x^2sin(x)/cos(x))

ans =

2x^2 + (2x^2sin(x)^2)/cos(x)^2 + (4x*sin(x))/cos(x)

Oh boy, that's pretty messy. Can we simplify that at all? Indeed we can. We can wrap it in the Matlab command simplify as follows:

simplify(2diff(x^2sin(x)/cos(x)))

ans =

(2x(x + sin(2*x)))/cos(x)^

Precalculus

Here are some precalculus things done in Matlab.

Factor a polynomial:

factor(x^4-3x^3-8x^2+21*x+9)

ans =

(x^2 + 3x + 1)(x - 3)^

Solve an equation. Notice that we give it a symbolic expression and the solve command assumes that it equals 0 when it solves.

lab for MATH 241 Part 1.

If you want to give it a specific domain of x values you can do that too:

ezplot(x+3*sin(x),-3,3)

lab for MATH 241 Part 1.

Calculus 1

Take a derivative and then substitute:

subs(diff(x^3),x,3)

ans =

27

Notice that subs is wrapped around diff. If we wrap diff around subs look at what happens and think about why:

diff(subs(x^3,x,3))

ans =

0

Find an indefinite integral. Notice that Matlab, like many careless students, never puts a +C in its indefinite integrals. Grrr.

lab for MATH 241 Part 1.

Calculus 3

Define a vector. Vectors in Matlab are treated like in linear algebra so this may be new to you. The vector 2 i + 3 j + 7 k can be entered either as a horizontal vector or a vertical vector. Whatever you choose to use, be consistent. I will use horizontal vectors in this guide because that's how we think of them in Math 241. As a vertical vector we use semicolons to separate rows:

u=[2;3;7]

u =

2 3 7

As a horizontal vector (like I'll do) we use spaces or commas. I'll use spaces out of habit.

u=[2 3 7]

u =

2 3 7

We can put variables inside vectors too for VVFs and then take derivatives and stuff like that:

a=[t^2 1/t 2*t] diff(a) int(a,1,3)

a =

[ t^2, 1/t, 2*t]

ans =

[ 2*t, -1/t^2, 2]

ans =

[ 26/3, log(3), 8]

Here are various combinations of vectors. Notice also here the semicolons at the end of the first two lines. These semicolons suppress the output, meaning we're telling Matlab "assign the vectors and keep quiet about it". Also note the use of norm for the length (magnitude) of a vector. Don't use the Matlab command length because that just tells you how many elements are in the vector.

u=[6 9 12];

lab for MATH 241 Part 1.

v=[-1 0 3]; norm(u) w=u+v dot(u,v) cross(u,v) dot(u,cross(u,v)) dot(u,v)/dot(v,v)*v

ans =

16.

w =

5 9 15

ans =

30

ans =

27 -30 9

ans =

0

ans =

-3 0 9

A few things to observe: The cross command produces a new vector, obviously. The second-to-last value is 0 and you should have expected that. Why? What is the last calculation finding? It's something familiar.

Here's the distance from the point (1,2,3) to the line with parametric equations x=2t, y=-3t+1, z=5. Make sure you see what's going on here. The point Q is off the line, the point P is on the line and the vector L points along the line. The reason P and Q are given as vectors is that we can then easily do Q- P to get the vector from P to Q.

Q=[1 2 -3]; P=[0 1 5]; L=[2 -3 0]; norm(cross(Q-P,L))/norm(L)

ans =

lab for MATH 241 Part 1.

T=simplify(diff(r)/sqrt(dot(diff(r),diff(r)))) N=simplify(diff(T)/sqrt(dot(diff(T),diff(T)))) K=simplify(sqrt(dot(diff(T),diff(T)))/sqrt(dot(diff(r),diff(r))))

T =

[ 1/(4t^2 + 1)^(1/2), (2t)/(4*t^2 + 1)^(1/2)]

N =

[ -(2t)/(4t^2 + 1)^(1/2), 1/(4*t^2 + 1)^(1/2)]

K =

2/(4*t^2 + 1)^(3/2)

Okay, let's plot some stuff in three dimensions! Here is how we can plot just one point, the point (1,2,3):

plot3(1,2,3,'o') view([10 10 10])

And here are two points:

lab for MATH 241 Part 1.

plot3(1,2,3,'o',-1,2,5,'o') view([10 10 10])

Note the 'o' tells Matlab to draw circles for points. Also note the view command there. Matlab has an annoying habit of viewing the axes from a nonstandard angle. The view([10 10 10]) command puts our point-of-view at the point (10,10,10) so we look down at the origin as we're used to in class.

Important observation! On the figure window there is a little rotation button which allows you to drag the picture around and look at it from different angles. Very useful and fun too! I can't demonstrate in a fixed html document though.

Okay now, let's plot some curves! We'll use the 3D command ezplot3. In class we sketched a helix with r(t)=cos(t)i+sin(t)j+tk.

ezplot3(cos(t),sin(t),t,[0,6*pi]) view([10 10 10])

lab for MATH 241 Part 1.

Okay that's a bit boring but it's happening because the frequency with which it oscillates up and down is the same as the frequency with which it rotates. Here's a better example in which the curve does one full rotation and while rotating it does five full up-down oscillations. We've also given it a specific range of t values. Try rotating that puppy for a really fun time!

ezplot3(cos(t),sin(t),sin(5t),[0,2pi]) view([10 10 10])

lab for MATH 241 Part 1.

If you have a VVF defined already and wish to ezplot3 it then you need to give it the component functions. You can do this like:

r=[t -t t^2]; ezplot3(r(1),r(2),r(3),[-1,1]) view([10 10 10])

lab for MATH 241 Part 1.

This last command daspect sets the aspect ratio so that all the axes are scaled the same. I have no idea why Matlab doesn't do this by default but grumble grumble grumble.

Just to close out let's draw some planes. These are not easy in Matlab in that we can't just throw the equation at it and expect good results. Instead here's the proces we need to take:

For example suppose we wish to plot 2x+3y+2z=12. We solve for z=12-2x-3*y and then we decide to use the suggested -5<=x<=5 and -5<=y<=5 and so we do:

syms x y z; ezmesh(x,y,12-2x-3y,[-5,5,-5,5]) view([10 10 10])

lab for MATH 241 Part 1.

As another example suppose we wish to plot 2x+4y=10. We solve for y=(10-2*x)/4 and then we do the following. Notice that z is still there even though it's not part of the equation. In this case we have -5<=x<=5 and -5<=z<=5:

syms x y z; ezmesh(x,(10-2*x)/4,z,[-5,5,-5,5]) view([10 10 10])