Now lets's add one (1) to each element in a. I then could have assigned the value of ans to a by typing a = ans. If I hadn't assigned the integers to the variable a, Matlab would have used the built-in variable ans. But if I want to see the value of a, I can just type its name.Īnd the value is displayed. You will notice that I followed the statement with a semicolon, so the result didn't display. Create a vector of the integers from 1 to 5 and name it a. Now let's try a short exercise to use matrices and matrix operations. So, if we want the entire second column, we would type: To specify an entire row or column, use a colon ( :) in place of the row or column number. The syntax is x(rows,cols) so, for example, if we want the to assign the element in the third row, first column to the variable y, we type: Remember how we used the colon ( :) to fill in numbers when we plotted numbers from 1 to 5 in the first section? We will use that again. To access this matrix, I specify the rows and columns I am interested in. You will notice that I used square brackets ( ) to enclose the matrix, spaces or commas ( ,) between elements within a row, and either a semicolon ( ) or a hard return between rows. In any case, the result is the same, the following 3 x 3 matrix stored as the variable x: For example, to create a 3 x 3 matrix with integer elements, I can type it in on the command line:
In Matlab, there are a number of ways to create matrices. Hopefully, your plot looks something like this. > y = beta*erf(x/5) % Create our function > beta = 0.5 % Define a constant, beta, to use > x = linspace(0,3*pi,100) % Do the same using linspace to make > x = 0:0.1:3*pi % Make x from 0 to pi in units of 0.1
#Matlab tutorial how to
First, let's look at how to generate a vector defining our domain, x.
Now we will look more at how to create and evaluate functions in Matlab (as you will need to do for this week's lab). In the last section, you saw some examples of Matlab functions (e.g. > theta = acos(-1) % Take the arccosine of -1. > y % To get the value of a variable, just > y = 2^2 + log(pi)*sin(x) % Using a semicolon supresses screen output > x = 2+2 % Assign the value of the expression Variables in Matlab can have any name as long as you don't use the same name as a Matlab function. It is highly recommended that you obtain this book as it will make your life (and your Matlab) easier. Here's a short tutorial on working with variables, taken from the book, Getting Started in Matlab. There is also a newsgroup,, where you can get more information. The Mathworks (developers of Matlab) also have a Information for gaining access to this help is at.
#Matlab tutorial manual
There are also a number of books, including the Matlab manual and Getting Started in Matlab by Rudra Pratap. You will get a list of all commands related to keyword, even if you aren't sure of the exact function name. Another helpful command is the lookfor command. This gives you a list of all of the 2-dimensional plotting functions available. By typing help or help command you can get a description of a command or group of commands. The easiest one to use is built into Matlab itself. If you get stuck or just need more information about Matlab, there are a number of different resources. In this tutorial, we'll go over some of the basic commands, how to read in data, how to save data, how to plot data, and how to create simple programs, called m-files. Matlab is also quite good at plotting all types of data, which makes it useful for plotting data generated elsewhere. What makes Matlab very powerful is that you can perform operations on whole vectors or matrices at one time, without having to loop through each element. You can also use scalars (1 x 1 matrices) or vectors (n x 1 or 1 x n matrices). The matrix is the only type of data that can be used, though it may be real or complex. Matlab is a program for doing scientific calculations using matrices.
As you'll see later, the colon (:) is a shorthand for creating a vector of numbers. Which will plot the numbers 1 to 5 (counting by 0.5) by their index (1 to 9). To see the graphics window, you just need to plot something. If so, you are all set - otherwise you need to recheck that you have set your display properly. You should see a splash screen appear on the screen and then disappear. Once running, Matlab is itself a command line program, wherein you will be given a prompt to type in specific commands.
#Matlab tutorial windows
In general, Matlab is run by selecting the application from a windows environment, or typing matlab at the command line prompt in a UNIX environment.