








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A matrix is an array of numbers arranged in rows and columns. You will study these in detail in a Linear Algebra course (Math 1114). Since we will be using ...
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!









MatLab was designed as a Matrix Laboratory, so all operations are assumed to be done on matrices unless you specifically state otherwise. (In this context, numbers (scalars) are simply regarded as matrices with one row and one column.) A matrix is an array of numbers arranged in rows and columns. You will study these in detail in a Linear Algebra course (Math 1114). Since we will be using MatLab here for Calculus of real numbers, not matrices, some of the arithmetic operators discussed below will be different than the standard ones that you would expect. Press āreturnā after each MatLab statement as you work through the examples below. Most of the time, the corresponding output from MatLab is also given so that you may check your typing.
An āequalsā sign is used to make an assignment. For example, type the following after the ā>>ā prompt in your command window:
x=
Now, press āreturnā to see what Matlab has stored as your variable x. You should see:
»x = 2
x =
2
If you just type 2, then MatLab will store it in the default variable, āansā.
Ā»
ans =
2
Right now, you have assigned x = (a list consisting of just one number, 2) You can also give a variable such as x a range of values (i.e., define it to be an array) by using a colon, ā : ā. For example, to let āxā take on all of the positive integers from 2 to 4, type:
»x=2:
x = 2 3 4
The semicolon, ; , suppresses output. Type āx = 2:4;ā and see what happens. You can also enter the array as x = [ 2, 3, 4] or x = [ 2 3 4]. Spaces separate the numbers in the last entry.
If you would like the step size to be .5 instead of one, insert the new step size between the first and last number: »x=2:.5:
x = 2.0000 2.5000 3.0000 3.5000 4.
You may find a negative step size useful:
»x=50:-10:
x = 50 40 30 20 10 0
Notice that we can redefine the variable x as often as we like.
Arithmetic operations can be performed on each entry of an array x, but you must insert a āperiodā before certain operators, specifically ā ***** ā, ā / ā, and ā ^ ā for multiplication, division, and exponentiation respectively. These operations have a different definition as matrix operations and without the āperiodā, they would be performed as such. To indicate that you want to perform an element-by-element multiplication, division, or exponentiation, you must precede the operation symbol by a period. For example, type the following. Press return after each line to see the output.
x=1: y=2: xy x.y x^ x.^ x/y (Notice that you get a number, but not an array. Matlab computes (xy')/(yy').) x./y
Note that addition and subtraction are always defined as an element-by-element operation so that you do not need the period with them. You will learn in linear algebra that if x = 1:5 = [1 2 3 4 5], then x+1 is not defined. Nevertheless Matlab interprets the command x+1 to mean that the number 1 is to be added to each element of the array x.
»x=1:
x = 1 2 3 4 5
»x+
ans = 2 3 4 5 6
Since all inputs to Matlab are treated as matrices, you will automatically create lists of data when you use the colon operator described above. For example, suppose you want a table of values for time, using every half-hour over the interval from 1 to 4.
»format short »time=1:.5:
time = 1.0000 1.5000 2.0000 2.5000 3.0000 3.
Then, suppose you want corresponding values for temperature but the values are not equally spaced. Simply list the values within square brackets separated by spaces or commas.
»temp=[51 53 52 49 49 46 41]
temp = 51 53 52 49 49 46 41
You can access any of these values using parentheses. For example, to find the third value in the temperature list:
»temp(3)
ans = 52
To combine both time and temperature into a table, use a single quotation mark (the ātranspose operatorā) after each variable. This will convert a row matrix into a column matrix and vice versa. You will study more about the transpose of a matrix in Linear Algebra.
Ā»[time',temp']
ans = 1.0000 51. 1.5000 53. 2.0000 52. 2.5000 49. 3.0000 49. 3.5000 46. 4.0000 41.
Matlab can be used to create different types of graphs. Pay careful attention to the coordinate axes and ranges that you see for each command used.
Use plot(x,y) to graph a curve in Matlab. The sizes of the x and y arrays must be the same. In the following example, define the domain from ā3 to 3 and create the x array. Unless you tell Matlab how many points to use in the graph, it will use the integers only. The .01 between the colons lets Matlab know that you want to use the numbers .01 units apart from ā3 to 3 ( ā3, -2.99, -2.98, -2.97,ā¦, 2.97, 2.98, 2.99, 3). Then graph y = x^2. Remember, the semicolon ā ; ā suppresses output.
»x=-3:.01:3; »y=x.^2; (y = x.^2 creates a y-array of the same size. Each entry in the y-array will be the square of the corresponding entry in the x-array.) »plot(x,y)
Note that you can also plot the graph without defining y by typing the following.
»x=-3:.01:3; »plot(x,x.^2)
To see what the graph looks like using the default, type x = -3:3 and then plot the graph.
If you want to plot more than one graph on the same set of axes, you must list the domain defined by the variable x for each graph. Below is the graph of y = 2x+1 and y = x^2 on the same set of axes.
»x=-3:.01:3; plot(x,x.^2,x,2.*x+1)
-5 -3 -2 -1 0 1 2 3
0
5
10
plot([2,2],[-3,3])
-3 1 1.5 2 2.5 3
0
1
2
3
It is possible to specify color, linestyle, and markers, such as plus signs or circles. Use plot(x,y,ācolor_style_markerā). Colors are denoted by ācā, āmā, āyā, ārā, āgā, ābā, āwā, and ākā. These correspond to cyan, magenta, yellow, red, green, blue, white and black. Linestyles are denoted by ā-ā for solid, ā--ā for dashed, ā:ā for dotted, ā-.ā for dash-dot, and ānoneā for no line. The most common marker types are ā+ā , āoā, ā*ā, and āxā.
x=-3:.3:3;plot(x,x.^2,'r--o')
Both axes and graphs may be labeled.
»plot(x,x.^2) »xlabel('x');ylabel('y');title('Graph of x^2')
(^0) -3 -2 -1 0 1 2 3
1
2
3
4
5
6
7
8
9
(^0) -3 -2 -1 0 1 2 3
1
2
3
4
5
6
7
8
9
x
y
Graph of x^2
To add text to a graph use text(x,y, ātextā) where x and y are your x and y coordinates to place the beginning of the text and the words in quotes are the text that you want on your graph.
»x=-1;y=2;plot(x,y,'o');text(-.9,2,'(-1, 2)')
(^1) -2 -1.8 -1.6 -1.4 -1.2 -1 -0.8 -0.6 -0.4 -0.2 0
2
3
(-1, 2)
Sometimes it is necessary to adjust the range of the graph. If you want to plot y = 1/x on the domain ā1 Ā£ x Ā£ 3 and do not specify the range, some machines will show this incorrect graph.
x=-1:.01:3;plot(x,1./x)
To adjust the range use the command axis([x1 x2 y1 y2]) where x1 £ x £x2 and y1 £ y £ y2.
x=-1:.01:3;plot(x,1./x);axis([-1 3 -10 10])
Note that on some machines the vertical line may not be graphed.
-1 -1 -0.5 0 0.5 1 1.5 2 2.5 3
0
1
2
3
4
5 x 10^16
-10 -1 -0.5 0 0.5 1 1.5 2 2.5 3
0
2
4
6
8
10
The abs command, abs(x) , computes the absolute value of x. (x may be a number or an array.)
»x=[-4,1/2, 0, -15.178, 32] ,abs(x)
x = -4.0000 0.5000 0 -15.1780 32. ans = 4.0000 0.5000 0 15.1780 32.
The max command, max(x) returns the largest value in an array x. The min command, min(x) returns the smallest value in an array x. For example, suppose you want the range for y = sin x over the interval (^) -p £ x £ p.
»x=-pi:.01:pi; y=sin(x); »max(y), min(y), abs(min(y))
ans = 1. ans = -1. ans = 1.
The subplot command allows you to display multiple plots on the same window. The command subplot(m,n,p) breaks the figure into an m-by-n array of small subplots and selects the pth subplot for the current plot. The plots are numbered first along the top row of the figure window, then the second row and so on.
»x=-3:.01:3;y1=x.^2;y2=2*x+5; »subplot(2,1,1) %first plot in a 2 x1 array of plots »plot(x,y1) »title('x^2') »subplot(2,1,2) %second plot in a 2x1 array of plots »plot(x,y2) »title('2x+5')
(^0) -3 -2 -1 0 1 2 3
2
4
6
8
10 x^2
-5 -3 -2 -1 0 1 2 3
0
5
10
15 2x+
The ginput function allows you to get coordinate points from a graph. The form [x,y]=ginput(n) finds n points from the current plot or subplot based on mouse click positions within the plot or subplot. If you press the Return or Enter key before all n points are selected, ginput stops with fewer points. We will find 5 points on y = x^2. Note that the mouse clicks record cursor coordinates and therefore may not be exact points on the curve. For instance in the example below, the point (-0.0227, 0) is chosen, but this point does not lie directly on the curve (x, x.^2).
»x=-3:.01:3; »plot(x,x.^2) »[x,y]=ginput(5) x = -2. -0. -0.
y =
0
You can define variables to be symbolic so that you can perform some basic algebraic and
calculus operations. For example, to define the function, f ( x ) = 4 x^2 - 3 x - 10 , first tell MatLab to treat the letter x as a symbolic variable. Then define the function. When working with symbolic variables, you do not need to use the period before the operation symbol.
»syms x »f=4x^2-3x-
f =
4x^2-3x-
To evaluate f(2), you must request a āsubstitutionā. The command subs(f,x,x0) replaces the variable x with the value x0 in the function f.
»subs(f,x,2) ans = 0
If you want a decimal representation for your answer, use the command double. Remember that MatLab stores results in a default variable called ans unless you state otherwise. Type:
»double(ans) ans = 2. -1.
To plot symbolic functions, use ā ezplot ā. The default domain is approximately ā2piĀ£xĀ£2pi unless you specify your own. For example type:
»ezplot(f)
-20 -6 -4 -2 0 2 4 6
0
20
40
60
80
100
120
140
160
180
x
4x^2-3x-
»ezplot(f,2,5)
2 2.5 3 3.5 4 4.5 5
0
10
20
30
40
50
60
70
80
x
4x^2-3x-
You must use the symbolic toolbox when you want to perform some calculus techniques such as differentiation. To differentiate a function we use the command diff(f).
»syms x »f=4x^2-3x-
f = 4x^2-3x-
»diff(f) ans = 8*x-