



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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A concise record of programming topics and their uses in Matlab, covering variables, matrices, and simple programming commands. It includes examples and explanations for creating and accessing matrices, performing matrix operations, and using for loops and if statements.
What you will learn
Typology: Lecture notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!
If you encounter any spelling or grammatical errors, please let me know, I am writing this up on the fly!
The purpose of this document is to keep a concise record of the programming topics and their uses in Matlab that we will cover over the semester. Each time we introduce a new topic in class, I will endeavor to transcribe it here as quickly as possible. It is my hope that this well help you in your programming projects. It should once again be stressed that Matlab has a very comprehensive and readable Help section which can be accessed by the F1 key. It can also be found under the Help tab by selecting the MATLAB Help
All information in Matlab must be saved to variables. Variables come in many different sizes and flavors. If you ever want to know what flavor your variable is, you can view them with the workspace tab. Variables can be constants, or they can be vectors, arrays or matrices. As a word of caution, there are some names for variables which you should stay away from, such as i, j and pi. Try not to use anything that might refer to a commonly known mathematical constant. To create a vector v of zeros which is 1 row by m columns, you simply type in the following command:
v = zeros(1, m)
If you would like to create a vector with defined elements, you can simply enter them:
v = [2, 4 , 3 , 5 , 8]
In this case, we can access the second entry by the command v(2). Arrays can be created in a similar fashion. If you wish to create a matrix with n rows and m columns:
w = zeros(n, m)
To access the entry in the ith^ row and jth^ column, you would use the command w(i, j). The linspace command also comes in handy. For example,
t = linspace(0, 1 , 101)
creates a vector with 101 entries, starting at a value of 0 at t(1) and a value of 1 at t(101). In general, the command
t = linspace(a, b, n)
creates a vector with n entires whose starting value is a, ending value is b, that is t(1) = a and t(n) = b.
We will devote a whole section to matrices. How do you define a matrix in Matlab? How do you enter the entries? How do you perform matrix operations? These are a few of the questions we will answer.
As previously stated, if you wish to create a matrix with n rows and m columns you use the command:
w = zeros(n, m)
To access the entry in the ith^ row and jth^ column, type w(i, j). You will notice that the w = zeros(n, m) command creates an empty (i.e. each entry in the matrix is equal to zero) matrix. If you already know what goes into your matrix, you can simply enter them in when defining the matrix. As an example, the matrix
can stored in the variable W by the command
W = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ]
A space is used between entries in each row, and a semi-colon separates rows. If for some reason you do not want to define the matrix by rows, but columns instead, you can use the following command:
W =
Notice that an apostrophe was used after each column. This denotes the transpose of a matrix. Remember that the transpose of a matrix A, denoted A>,is where you switch the rows and columns, hence a> ij = aji. Hence, if W is defined as above, then the command
X = W ′
will give
One must be very careful when using the transpose command. If the matrix you wish to compute the transpose of is real, then the apostrophe will be enough. However, consider the following example:
R = [1 + i 1 − i; 2 + 3i 4 − 6 i]
which has complex entries. This matrix looks as follows:
1 + i 1 − i 2 + 3i 4 − 6 i
However, performing the command R′^ yields
1 − i 2 − 3 i 1 + i 4 + 6i
which is not just the transpose of R, but the conjugate transpose of R. To compute the transpose of a complex valued matrix, you use the command R. ′^ (notice the dot that preceeds the apostrophe. Hence,
1 + i 2 + 3i 1 − i 4 − 6 i
Now, you can access more than just an entry of a matrix at a time. You can access whole sections. For instance,
X(2 : 3, 1 : 2)
a 3 × 3 matrix will be returned:
ans = 30 70 110 70 174 278 110 278 446
The operations of addition and subtraction are the same. To subtract a matrix B from a matrix A, simply use the command A − B. Remember, A and B have to have the same dimension. What will happen if the command X − W or W −X is used? What is the result of the subtraction X −W. ′^ or W −X. ′? (Here X and W are defined as previous.)
4 Simple Programming Commands
Next we will take a look at some of the simpler programming commands that you can use with Matlab.
The for loop is one of the most fundamental building blocks of programming. In Matlab, the structure is as follows:
for j = start : step : stop stuff to do each loop end
If the stepsize is not included,, it will assume that it is 1. As an example consider the following very simple loop:
for k = 1 : 5 : 29 k end
The above code simply outputs the value of j each time it goes through the loop. Notice that it should output 1, 6, 11, 16, 21 and 26. Once it gets to 31, it has passed the stop value of 29. An important fact is that the step does not have to be positive, and even more so, it does not have to be an integer. Consider
for k = 1 : -0.1 : 0 k end
This loop starts at the value of 1, and ends at 0 by increments of 0.1. Many times, programs will require nested loops.
for k = 1 : 1 : 10 for l = 5 : -1 : 0 [k, l] end end
Try to determine what the above program will output. Notice that the output is in the innermost for loop. You can download this program from the website, it is called firstfor.m.
The if .. then statement is a standard conditional statement. The simplest standard form is
if (object relation object) stuff to do end
The condition is usually a relation. The standard relations are <, >, <=, >=, == and ∼=, which are less than, greater than, less than or equal to, greater than or equal to, equal to, and not equal to, respectively. As an example:
if (x >= 12) x = x + 1; end
The above segment of code compares the variable x to the value 12, if x is at least 12 or greater, then the one more will be added to x. If x is not at least 12, then nothing will happen. As stated at the beginning of the section, the above is the simplest form of the if .. then statement. There are more complicated forms, such as the if .. elseif .. else ... As an example:
if (x >= 12) x = x + 1; elseif ((x < 12)&(x > 0)) x = x − 1; end
Notice that in the elseif line, there are two relations. You must join these by the & symbol. You can join as many relations as you want this way. The above code does the same as before, however if x is not 12 or larger, then it does not exit, it goes onto the elseif. If x happens to be between 0 and 12, then x will be replaced by x − 1. The above code used only one elseif , but many more can be used.
if (x >= 12) x = x + 1; elseif ((x < 12)&(x > 0)) x = x − 1; else x = 0; end
This last code differs in one small way. There is an else, which stipulates that if none of the other conditions are satisfied, then set x = 0. The M-file f irstif.m is an example of the above code. The correct call sequence is f irstif (value), where value is any number you choose. Try many different values, and see what output you get. Pay particular attention to the values of 0 and 12.
5 Plotting
Plotting can be simple, plotting can be complicated. We will of course start with the simple. Let us first plot sin(x) on the interval (−π, π). To do this, we need to define a linspace over the interval (−π, π) (see the previous section entitled Variables in Matlab). This is done by the command
x = linspace(-pi,pi,1000)
Then to plot, simply try the command