Anda di halaman 1dari 14

USING MATLAB FOR CALCULUS

POLYTECHNIC UNIVERSITY DEPARTMENT OF MATHEMATICS

1. Introduction Matlab is software designed for doing numerical computations and graphics. It was designed primarily for numerical computations involving vectors, matrices, and linear algebra. This means that it is a little awkward to use for calculus. Please take the time to learn carefully how Matlab works and how you can get it to do what you need it to do. Although you might nd Matlab frustrating at rst, since it is not designed specically to do calculus, you will better appreciate the power of Matlab later when you learn and use linear algebra, dierential equations, and multivariable calculus. This document is not meant to be a tutorial on Matlab. You can nd on the web page http://www..math.poly.edu/courses/matlab.phtml links to some tutorials on Matlab that contain much more detail than here. Please look through all of them, nd the one that you nd easiest to read, and read through it. This document will explain how to use Matlab eectively for precalculus and calculus.

2. Matlab as a programming language Matlab is really just a programming language like C or C++. That means you use Matlab by typing in a sequence of commands that tell the computer to do something. Unlike C and C++, Matlab is an interactive language. After you write a C++ program, to get the computer to carry out the commands you have put in the program, you have to write a whole sequence of commands, save them in a le, run the le through something called a compiler, run another newly created le called an object le through something called a linker, and nally run a newly created le called the binary executable. This process makes sense when you are developing sophisticated software; it does not, if you just want the computer to do a few simple things. With Matlab you type in a single command, and Matlab does the command immediately. If there is output, it will display the output immediately. Sometimes, you dont want to see the output; usually this is because the output is really long. To suppress the output of a Matlab command, just end the line with a semicolon
1

POLYTECHNIC UNIVERSITY DEPARTMENT OF MATHEMATICS

Unfortunately, the commands you use in Matlab look similar to but are not exactly the same as the ones you use in C++. It is like the dierence between American English and British English. Since you will be using C++ in your CS course and Matlab in your math course, this will be a constant headache.

3. Using Matlab as a calculator Start Matlab. Find the window named Command Window. If you dont see it, select it in the menu titled View. You normally use Matlab by typing commands into this window. For example, you can use Matlab as a scientic calculator. Try the following: >> 3 + 4 ans = 7 >> 5*(3^7)/75 ans = 145.8000 Notice that you use the symbol * to represent multiplication and the symbol ^ to represent raising to a power. Matlab also has almost every common mathematical function built into it, including the natural exponential (exp(x)), trignometric functions (sin(x), cos(x), tan(x), sec(x),...), the natural logarithm (log(x)) (notice that this is dierent from the book), and inverse trignometric functions (asin(x), acos(x), atan(x)) (notice that this is also dierent from the book). Sometimes, it is convenient to assign partial results to variable names. You can do this easily as follows: >> x = 10 x = 10 >> y = 5 y =

USING MATLAB FOR CALCULUS

5 >> z = sqrt(x^2 + y^2) z = 11.1803 >> 4. What is a vector? What is a matrix? To use Matlab eectively, you must understand what a vector and a matrix are. You will learn in more detail what a vector and a matrix are when you take linear algebra. For now we will only use them in simple ways. A matrix is simply a rectangular table of numbers. For example, A= 1 2 3 1 0 2

is what we call a 2by3 matrix, meaning it has 2 rows and 3 columns. A vector is a list of numbers written as a matrix that has only one column or only one row. For example, 1 v = 1 15 is an example of a column vector, which is a matrix with only one column, and w = 1 1 15 is an example of a row vector, which is a matrix with only one row. Notice that a single number is also a 1by1 matrix. 5. Why are vectors and matrices useful in calculus? Vectors and matrices are not normally used in calculus. They are, however, convenient to use in MATLAB for creating lists and tables of numbers. Any time you want to create a vector or a matrix in MATLAB, you must assign it a name, just like a variable name. The variable name can be one or more characters long, where the rst character must be an upper or lower case letter and the other characters must be either a letter, a number, or an underscore. Valid variable names include x, a, number, xy0, grades 1999.

POLYTECHNIC UNIVERSITY DEPARTMENT OF MATHEMATICS

The simplest way to create a matrix is to just list the numbers in the matrix as follows: >> a = [1 2 3] a = 1 2 3

>> b = [1; 2; 3] b = 1 2 3 >> c = [1 2 3 4; 2 4 6 8 ; 3 6 9 12] c = 1 2 3 2 4 6 3 4 6 8 9 12

Notice that numbers within the same row are separated by spaces. Rows are separated by semicolons. Entering every single number of a matrix, however, can get tedious, if you want to create a large matrix with lots of numbers. So there are convenient shortcuts that we will learn. 6. Plotting You can draw simple plots using the Matlab function plot. You just give plot a matrix that has two columns. Each row of the matrix is viewed as the x and y coordinates of a point. The function plot will plot these points and join them with line segments. Here is an example: >> x = [-3 -2 -1 0 1 2] x = -3 -2 -1 0 1 2

>> y = [1 -2 0 0 3 5] y =

USING MATLAB FOR CALCULUS

Figure 1

-2

>> plot(x,y) >> will create a new window with the plot shown in Figure 1. 7. Plotting the graph of a function To use plot to draw the graph of a function f , we need to create a vector, call it x, that contains lots of values for x and a second vector, call it y, that contains the corresponding values of f (x). Matlab provides a simple way of creating the vector x. For example, we can create a row vector that contains evenly spaced values from 3 to 3, where the space between any two consecutive numbers is 0.5 by entering >> x = -3:0.5:3 x = Columns 1 through 8

POLYTECHNIC UNIVERSITY DEPARTMENT OF MATHEMATICS

Figure 2

-3.0000

-2.5000

-2.0000

-1.5000

-1.0000

-0.5000

0.5000

Columns 9 through 13 1.0000 1.5000 2.0000 2.5000 3.0000

Normally, we dont want to see the output of this command, especially if we decide to generate a vector with 100 numbers in it. So from now on we will end such commands with a semicolon. To draw a graph of y = ex 1, for 3 x 3, do the following: >> x = -3:0.1:3; >> y = exp(x) -1; >> plot(x,y) The plot shown in Figure 2 should now appear in the window named Figure No. 1. Notice that if the window Figure No. 1 already exists with another plot, Matlab will just replace that plot with the new one. It will not create a new window. Also, notice that by ending the rst two lines with semicolons, there is no output. Try it once without the semicolons to see what happens.

USING MATLAB FOR CALCULUS

Figure 3

8. Formulas involving vectors To create the vector y when plotting a function, you need to nd the value of the function for each xvalue in the vector x. Matlab allows you to do this easily by just using the vector x in formulas as if it were just a single number. There are, however, a few complications. Here are some examples of what you can do:

x + 2 3.*x x./5 x.^2 3./x exp(x) x1 + x2 x1 .* x2 x1 ./ x2

Add 2 to each number in x Multiply each number in x by 3 Divide each number in x by 5 Raise each number in x to the power 2 Divide 3 by each number in x Calculate the exponential of each number in x Add each number in x1 to the corresponding number in x2 Multiply each number in x1 by the corresponding number in x2 Divide each number in x1 by the corresponding number in x2

To plot y = 1/(x2 + 1), >> plot(x,1./(x.^2 + 1)) The window Figure No. 1 should have the plot shown in Figure 3.

POLYTECHNIC UNIVERSITY DEPARTMENT OF MATHEMATICS

9. Working with matrices You can put two matrices together to make one big one as follows: >> x = [1 2 3; 4 5 6] x = 1 4 2 5 3 6

>> y = [0 0 0; 0 0 0] y = 0 0 0 0 0 0

>> z = [x y] z = 1 4 2 5 3 6 0 0 0 0 0 0

>> w = [x;y] w = 1 4 0 0 2 5 0 0 3 6 0 0

The transpose of a matrix is a new matrix whose columns consist of the original matrixs rows. For example, >> A = [1 2 3; 4 5 6] A = 1 4 2 5 3 6

USING MATLAB FOR CALCULUS

>> A ans = 1 2 3 4 5 6

To create a table showing values of the function f (x) = e2x , >> x= [0:0.5:3] x = 0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 >> [x exp(-2*x)] ans = 0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 1.0000 0.3679 0.1353 0.0498 0.0183 0.0067 0.0025 10. Defining functions You can dene your own functions in Matlab. If the function has a simple oneline formula, you can do it as follows: >> f = inline(x.^2 - 3*x + 2) f = Inline function: f(x) = x.^2 - 3*x + 2

10

POLYTECHNIC UNIVERSITY DEPARTMENT OF MATHEMATICS

Figure 4

>> x = [-3:3]; >> [x f(x)] ans = -3 -2 -1 0 1 2 3 20 12 6 2 0 0 2

>> x=[-3:0.1:5]; >> plot(x,f(x)) The last command produces the plot shown in Figure 4.

11. Using an Mfile to define a function If the function is more complicated, you can put its denition in a separate le called an Mle. To create an Mle, select Mle in the submenu attached to New under the menu titled File. A new window opens up. You type the commands dening the function in this window. For example, to dene the function g(x) = you would type the following in the Mle: x2 1 x2 + 1

USING MATLAB FOR CALCULUS

11

function y = g(x) y = (x.^2 - 1)./(x.^2 + 1); Notice the semicolon at the end of the line. Try it without a semicolon to see why you want it there. Select Save from the menu titled File, and save the le using the name g.m in the default directory, which should be called work. Now return to the Command Window. You can now use the function g. >> g(-10) ans = 0.9802 >> g(0) ans = -1 >> g(5) ans = 0.9231 >> x = -10:0.1:10; >> y = g(x); >> plot(x,y); The last command produces Figure 5. Lets try a more complicated example. Let 1 h(x) = x 1

if x 1; if 1 x 1; if x 1.

The following appears to work: Create a new Mle and enter the following: function y = h(x) if x <= -1 y = 1; elseif x <= 1 y = -x;

12

POLYTECHNIC UNIVERSITY DEPARTMENT OF MATHEMATICS

Figure 5

else y = -1; end Save this as h.m, and return to the Command Window. You can now use the function h. >> h(-2) ans = 1 >> h(-0.5) ans = 0.5000 >> h(0) ans = 0 >> h([0 1 2 3]) ans = -1 The last command did not do what we wanted. Why not?

USING MATLAB FOR CALCULUS

13

We have to x this bug, because we want to be able to plot h. The MATLAB commands we used in the Mle for h do not work properly, because they assume that the variable x is a single number. However, we want to be able to let the input x be a vector or a matrix, and we want the output y to be a vector or matrix of the same dimensions containing the respective values of h. How do we x the M le so that this happens automatically? Here is one way: Change the Mle h.m to the following: function y = h(x) y = x for i = 1:numel(x) if x(i) <= -1 y(i) = 1; elseif x(i) <= 1 y(i) = -x(i); else y(i) = -1; end end The rst command y = x is used simply to make y a matrix of the same dimensions as x. The remaining commands set each component of the matrix y equal to the value of h evaluated at the corresponding component of the matrix x. We now try the same commands using h as before: >> h(-2) y = -2

ans = 1 >> h(-0.5) y = -0.5000

ans =

14

POLYTECHNIC UNIVERSITY DEPARTMENT OF MATHEMATICS

Figure 6

0.5000 >> h(0) y = 0

ans = 0 >> h([0 1 2 3]) ans = 0 -1 -1 -1

>> x = -10:0.1:10; >> y = h(x); >> plot(x,y) The last command now produces the correct graph of h, which is shown in Figure 6. Unfortunately, you cant see the graph in this picture, so you have to try it yourself with MATLAB.

Anda mungkin juga menyukai