Getting Started in MATLAB

Most of the .m files that you will create and are found on the H-drive are files that are called SCRIPT files. They are lines of commands just as if you or I had typed them in line-by-line in the MATLAB command window. Script files are very convenient to use, as to get something just a little different, you just change the file and don't have to re-type everything in (and helpful when things aren't working to fix them). This allows for more experimentation with commands and the coding. It is best to create the script file through the editor window in MATLAB and save it in the directory of your choice. See the Workspace page for more info on the directories.

Running the script file: First, your ''Current Directory'' should be the directory that you saved your files. This is found at the top, towards the right on the MATLAB screen. You can run it two ways:

  1. From the editor window: have the script file open, and either hit F5 or select ''Run'' (or ''Save and Run'') in the Debug menu at the top. This can only be done with script files that require no additional input from the user.
  2. From the command window: If your file is filename.m, at the command prompt type filename and press enter.

One thing to pay close attention to is how to do component-wise computations on vectors (and matrices). This is especially important for plotting, as to plot something as simple as \( y = \dfrac{2x^2 + 5}{x} \), we create a vector of x-values, and then we want to compute a vector of the corresponding y-values. To multiply, divide, and take powers component-wise (addition, subtraction and scalar multiplication is automatically so), we put a ''.'' before the operation symbol. For the graph of \( y = \dfrac{2x^2 + 5}{x} \) for \( 0.5 \le x \le 2\), we'd do this by doing something like the following (the comments are only for your benefit). Comments in the script files start with with %

Example of MATLAB commands to create a basic plot. NOTICE THE '';'' AT THE END OF THE LINES WHEN CREATING x AND y!

x=linspace(0.5,2);          % creates a vector of 100 numbers equally spaced between 0.5 and 2
y= (2*x.^2 + 5)./x;         % computes (2x^2+5)/x component-wise
plot(x,y)                   % plots the points formed by x, y vectors
title('Basic Plot Example') % puts a title in the plot