Beginner's Matlab Tutorial: [email protected] - Edu
Beginner's Matlab Tutorial: [email protected] - Edu
Christopher Lum
[email protected]
2. If your window does not appear like this, it is possible that different windows are
currently activated. Let us change the appearance and activate some useful
windows. First, we’ll start a new .m file. To do this use
Or
3. This starts a new M-file which can be edited (more on this later). This probably
opens the editor in a new window as shown below in Figure 2.
4. We would like to be able to see both the editor and the Command Window at the
same time. Go back to the m-file editor and select
6. You can now drag around the 3 activated windows (Command Window, m-file
editor, and Workspace) to arrange the views as you like. To drag a window,
simply click on the window and then drag the blue bar (see Figure 3). The Matlab
interface should now similar to Figure 3.
All declared variables appear in the workspace. Recall that these values are
stored as matrices. The “size” column tells us the dimension of the matrix. As
expected, all these variables are 1x1 scalar values. To double check on value
stored in this matrix, simply double click any of the variables in the Workspace.
2. Now, let’s assume that x and y are actually components of a 2D vector. Let’s
x
construct the vector v = . Note that we are making a column vector of size
y
Also notice that in the workspace, the variable “v” is of size 2x1 as expected.
Once again notice that the variable “p” is created in the workspace and it is of size
1x2 as expected
4. We can create a 2D matrix in a similar fashion. You can use the “[“ to start the
matrix, type in the first row with spaces in between elements, use a “;” to start the
next row, and then repeat. Finally close the matrix with a “]“. For example, to
create a 3x2 matrix, we can use syntax like
Notice that we used the variable ‘p’ as the last row. This is possible since the
dimensions match.
5. Matlab treats most variables as matrices, and therefore operations like addition,
multiplication, etc. must be done with matrices whose dimensions are consistent.
For example, we could enter
6. Now, let’s assume that we wanted to change the value of ‘x’. This would tedious
to retype in all the commands again, so let’s use the m-file to avoid retyping in all
the commands each time we make a change.
The m-file is like a source file which will run all your commands in a top to
bottom fashion. You can use the “%” sign to comment out lines. A sample m-file
is shown below in Figure 5.
7. You can now run this file by hitting the “Run” button or hitting F5. At this point,
Matlab will force you to save you project. Navigate to your desired directory and
This warning appears because the m-file that you are trying to run is not located
in the current working directory. Therefore, you should select the first option
(Change Matlab current directory) and hit OK.
f1 ( x) = 3x + 4
1. If you were to plot this by hand on graph paper, you would probably follow a
procedure such as
a. Choose a series of x values where you would like to evaluate the function.
b. Compute the corresponding y value at each of these x values.
c. Plot each x,y pair.
d. Connect the points with a straight line.
x y = f(x)
-1 3*(-1) + 4 = 1
0
1
3*(0) + 4 = 4
3*(1) + 4 = 7
→
2 3*(2) + 4 = 10
Etc. Etc.
We can first create a vector which represents the x values where we would like to
evaluate the function. Syntax in you m-file might look like
2. We can now evaluate the function at these x values. Syntax in your m-file might
look like
3. Now that we have two vectors (one representing the x values and the other
representing the y values), we can plot the function to see what it looks like. To
do this we will use the “plot” function. To obtain help about any of Matlab’s
functions, simply type in “help name_of_desired_function” into the Command
window. For example for help on the “plot” command, you would type “help
plot” in the command window. Syntax in your m-file might look like
4. We can label the axis, add a title, turn on a grid, put a title and legend on the plot,
and force the axis to a set range. Are more complete plotting syntax in your m-
file might look like.
5. You can export this figure so that it may be easily included into other documents.
To do this, go to the figure and select
File > Save As (choose export file type the hit ‘Save’)
f 2 (t ) = g (t )h(t )
Where g (t ) = 3t 2
h(t ) = sin (4t + 4)
1. Once again, the first thing we need to do is create a list of t values where we
would like to evaluate the function. Instead of manually typing in values, we can
use the syntax as shown below
t
0 1 2 3 4 5
This will cause an error. To understand why, recall that Matlab treats most
variables as matrices. Also, the command t^2 is interpreted as t*t. If you recall, t
is a 1 x 6 matrix. You cannot multiply a 1 x 6 matrix with a 1 x 6 matrix. This is
what causes the error.
t2
02 12 22 32 42 52
In other words, you would like to apply the square operation to each element of
the matrix individually. This is known as an element-wise operation. The syntax
to perform this is
Note here that the term ‘4*t’ is a 1 x 6 matrix and the ‘4’ is simply a 1 x 1 scalar.
In this case, when adding a 1x1 to any other sized matrix, it will automatically
apply an element-wise operation. This is why you do not need to use a ‘.+’ to
perform the addition. The same goes for scalar multiplication (multiplying a 1 x 1
matrix into an m x n matrix).
5. Once again, we can plot this function. To illustrate how the plotting operates,
let’s plot the x,y points as large red ‘x’ marks. Syntax to do this is shown below
6. As can be seen in Figure 11, this is not a very fine resolution representation of the
function. To fix this, we can go back to where we defined the variable t and
choose a smaller step size. For example
↓ change to