Getting Started in MATLAB
Most of the .m
files that you will create and are found on the shared MATLAB 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 (your own MATLAB drive is recommended).
Running the script file: First, your ''Current Folder'' should be the location you saved your files. This is found at the top, towards the right on the MATLAB screen. You can run your m-file two ways:
- From the editor window: have the script file open, and select ''Run'' (or ''Save and Run'') in the Editor menu at the top. This can only be done with script files that require no additional input from the user.
- From the command window: If your file is
filename.m
, at the command prompt typefilename
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 %