Docsity

Math 241H - Matlab Assignment #1: Computers & Matlab for Integration & Curve Drawing, Assignments of Advanced Calculus

Toggle sidebar
bg1
Math 241H - Matlab Assignment #1 - Due Friday, March 2, 2007
Finding a computer with Matlab
You may buy a student version of Matlab to use on your own computer; check out the bookstore. If
you are familiar with ssh or telnet you can log in to a university computer from your home computer and
run Matlab that way. Most of you will probably use a public computer however. Go to an owl lab and find
a computer. For example there is an owl lab in room 0203 of the Math building and in the PG2 parking
garage. Others may be found in the list in http://www.oit.umd.edu/wheretogo. Not all the others have
Matlab installed, the list at http://www.oit.umd.edu/wheretogo should tell you which ones do. There
is matlab tutoring for 241 students in the computer lab in Parking Garage 2. A list of the hours tutoring
is available is at http://www.math.umd.edu/undergraduate/resources/tutoringmatlab.shtml. (There
are links to these sites from the class web page.)
Anyway, once you have found a computer with Matlab on it, log in. To start Matlab, for example on
a Unix machine, type in matlab at the prompt in a terminal window, (or maybe you have to type tap
matlab before this depending on the machine). On PCs or Macs there will be some icon to click. (In the
owl lab in room 0203 of the math building, click on start in the lower left corner and choose Matlab from
the applications.)
Matlab will now give you a prompt:
>>
You may now type in commands to Matlab at the prompt.
For some links with more help with Matlab see http://www.math.umd.edu/ hck/340f06matlab.html
Using Matlab for Numerical Evaluation of Integrals
While you can find formulas for the derivatives of just about any function you can write down, the same
cannot be said for the integral. Very few functions have integrals which can be written down as a formula
involving well-known functions. If you can’t find an antiderivative, you must turn to numerical techniques.
A numerical technique will provide you with a number which usually is a good approximation of the integral.
(Always keep in mind that any numerical technique can be fooled and sometimes will give an inaccurate
answer. Generally, numerical integration techniques work well unless you integrate a function which is really
‘spiky’ or oscillates rapidly, and even then they often do quite well.)
The matlab function quad allows you to perform an integration. For example, to evaluate
Z4
2
sin(x3−7x)dx
you could type:
>> syms x
>> f = inline(vectorize(sin (xˆ3 −7∗x)),0x0)
>> quad(f,2,4)
Using Matlab to draw curves
Matlab does a nice job of drawing space curves. Here is an example:
>> ezplot3 ( 0(tˆ3) ∗sin(t)0,03∗cos(2 ∗t)0,0(tˆ2)/20,[0 2 ∗pi ]);
This would plot the curve parameterized by the function
F(t) = t3sin(t)i+3 cos(2t)j+t2/2kfor t in the interval [0,2π]. The semicolons after the Matlab commands
are not needed. However, if you omit them, Matlab will print out long lists of values of tand function values.
Using Matlab to draw surfaces
The following is an example of how you could get Matlab to draw the graph
z=ex−ysin 2xfor −1≤x≤3 and 0 ≤y≤4.
>> ezmesh (0exp(x)−y∗sin(2 ∗x)0,[−1 3 0 4]);
For a different look, you could type
1
pf2

Partial preview of the text

Download Math 241H - Matlab Assignment #1: Computers & Matlab for Integration & Curve Drawing and more Assignments Advanced Calculus in PDF only on Docsity!

Math 241H - Matlab Assignment #1 - Due Friday, March 2, 2007

Finding a computer with Matlab You may buy a student version of Matlab to use on your own computer; check out the bookstore. If you are familiar with ssh or telnet you can log in to a university computer from your home computer and run Matlab that way. Most of you will probably use a public computer however. Go to an owl lab and find a computer. For example there is an owl lab in room 0203 of the Math building and in the PG2 parking garage. Others may be found in the list in http://www.oit.umd.edu/wheretogo. Not all the others have Matlab installed, the list at http://www.oit.umd.edu/wheretogo should tell you which ones do. There is matlab tutoring for 241 students in the computer lab in Parking Garage 2. A list of the hours tutoring is available is at http://www.math.umd.edu/undergraduate/resources/tutoringmatlab.shtml. (There are links to these sites from the class web page.) Anyway, once you have found a computer with Matlab on it, log in. To start Matlab, for example on a Unix machine, type in matlab at the prompt in a terminal window, (or maybe you have to type tap matlab before this depending on the machine). On PCs or Macs there will be some icon to click. (In the owl lab in room 0203 of the math building, click on start in the lower left corner and choose Matlab from the applications.) Matlab will now give you a prompt:

You may now type in commands to Matlab at the prompt. For some links with more help with Matlab see http://www.math.umd.edu/ hck/340f06matlab.html

Using Matlab for Numerical Evaluation of Integrals

While you can find formulas for the derivatives of just about any function you can write down, the same cannot be said for the integral. Very few functions have integrals which can be written down as a formula involving well-known functions. If you can’t find an antiderivative, you must turn to numerical techniques. A numerical technique will provide you with a number which usually is a good approximation of the integral. (Always keep in mind that any numerical technique can be fooled and sometimes will give an inaccurate answer. Generally, numerical integration techniques work well unless you integrate a function which is really ‘spiky’ or oscillates rapidly, and even then they often do quite well.) The matlab function quad allows you to perform an integration. For example, to evaluate ∫ (^4)

2

sin(x^3 − 7 x) dx

you could type:

syms x f = inline(vectorize(sin (xˆ3 − 7 ∗ x)),′^ x′) quad(f,2,4)

Using Matlab to draw curves Matlab does a nice job of drawing space curves. Here is an example:

ezplot3 ( ′(tˆ3) ∗ sin(t)′, ′ 3 ∗ cos(2 ∗ t)′, ′(tˆ2)/ 2 ′, [0 2 ∗ pi ]); This would plot the curve parameterized by the function F (t) = t^3 sin(t) i+3 cos(2t) j+t^2 / 2 k for t in the interval [0, 2 π]. The semicolons after the Matlab commands are not needed. However, if you omit them, Matlab will print out long lists of values of t and function values.

Using Matlab to draw surfaces The following is an example of how you could get Matlab to draw the graph z = ex^ − y sin 2x for − 1 ≤ x ≤ 3 and 0 ≤ y ≤ 4.

ezmesh (′exp(x) − y ∗ sin(2 ∗ x)′, [−1 3 0 4]); For a different look, you could type

1

ezsurf (′exp(x) − y ∗ sin(2 ∗ x)′, [−1 3 0 4]); To get a contour plot, you could type ezcontour (′exp(x) − y ∗ sin(2 ∗ x)′, [−1 3 0 4]);

Saving and printing out your results One way to save your output is to cut and paste from the Matlab window to your favorite word processor, but be careful to include all relevant output if you do this. Another is to use Matlab to save your session for you. If you are not on a unix machine you will probably need to give the command

cd H: to make sure Matlab saves to your home directory on WAM. Now you can type in the command diary prob and Matlab will save all the following output to a file named prob1. When you wish to stop saving, type diary off You can then print the file prob1 as is and write your commentary by hand, or edit in your commentary with a word processor. (Note no commentary is required for this assignment, but may be necessary in future assignments.) Whenever you want to include graphic output use the command print -djpeg Fig (or Fig2 for the second figure etc.) which saves the most recent graphics output onto a file Fig1.jpg. Type help print for a list of other graphics formats you may save in if you don’t want jpeg format. You can print things out at the computer lab. There is a small charge for doing this. You will need to set up a print account, instructions are at http://www.helpdesk.umd.edu/documents/0/184/. If you can transport your file home, for example by e-mailing it to yourself, you can print it out on a home printer and avoid print charges.

Avoiding mistakes Complete your computer assignments well in advance of the due date. If you have not used Matlab before, the hardest parts of this assignment will be finding a computer with Matlab, getting it to run Matlab, and figuring out how you are going to print your results. If you need help with these basics, go to the tutoring hours in Parking Garage 2.

Problems to turn in, Due Friday, March 2, 2007 You are encouraged to work on these problems in groups of two or three. Each group should turn in only one copy of the assignment, but put all your names on the assignment you turn in. Your assignment should include a printout of your Matlab session (i.e., the Matlab commands you gave and Matlab’s responses) as well as your graphic output.

Problem 1. Use numerical integration using the Matlab command quad to find the length of the curve r(t) = (et, t, cos t), 0 ≤ t ≤ 1. (The exponential function et^ is exp(t) in Matlab.) You will need to figure out what function to integrate.

Problem 2. Print out a plot of the curve with parameterization

r(t) = t cos t i + 2t sin t j − t k.

for − 3 π ≤ t ≤ 3 π.

Problem 3. Print out a graph and a contour plot for the function

f (x, y) = (x^2 − 3 y^2 ) exp(1 − x^2 − y^2 )

for − 2 ≤ x ≤ 2 and − 3 ≤ y ≤ 3.