CS151 - Quick Reference #1

CS151 - Quick Reference #1
1. Environment
1.1. ESC Pod
1.1.1. Open Matlab: Start->All Programs->MATLAB then click on the Matlab icon
1.1.1.1.
Matlab is a server-based program instead of being installed on each computer.
1.1.1.2.
Because Matlab runs on a server, if there are any issues with the server or
network, Matlab will not run
1.1.2. T drive: temporary shared drive to use if you don’t have a flash drive with you. However,
the T drive is extremely problematic, because the drive is shared among all students using
that computer. It is erased on a daily basis, so do not count on files being there for long!
1.1.3. Flash drive:
1.1.3.1.
A flash drive is the preferred method of saving your work.
1.1.3.2.
Be sure to insert your flash drive and have MS Windows recognize the drive fully
before starting Matlab; otherwise, Matlab will not be able to see the flash drive. This is
because of the networked software environment.
1.2. ESS Lab
1.2.1. Open Matlab: Start->All Programs->Matlab->Matlab R2008a
1.2.2. Saving on hard drive: files in the ESS computer lab can be deleted at anytime without
warning
1.2.3. Flash drive:
1.2.3.1.
A flash drive is the preferred method of saving your work
1.2.3.2.
Your flash drive can be inserted after Matlab is started in the ESS computer lab
2. Matlab
2.1. Matlab Layout
2.1.1. Command Window: the window where single commands, functions, etc. can be executed
2.1.2. Command History:
2.1.2.1.
a log of every command executed within the Command Window
2.1.2.2.
commands can be double-clicked to execute again
2.1.3. Workspace:
2.1.3.1.
list of all variables currently in memory
2.1.3.2.
Double-click on any of the variables to see their values in a worksheet format
2.1.4. Current Directory:
2.1.4.1.
location of working directory to which all .m files
2.1.4.2.
Matlab can only run .m files
2.1.4.3.
To change the Current Directory, click on the
button
2.1.5. Using Help
2.1.5.1.
use the F1 button OR from the menu Help->Product Help
2.1.5.2.
Do not use the Search within Help, it is a very non-intelligent search
2.1.5.3.
Instead of Search, click on the Index tab within Help and “Enter index term:”
2.1.5.4.
Only click on terms that has “Matlab” listed in the “Product” column
CS151 - Quick Reference #1
1
Command History Workspace
Command Window
Current Directory
Change Current Directory
2.2. Create/Open/Save Matlab files
2.2.1. Matlab files are .m files that script files, allowing the execution and saving of multiple
commands, functions, etc., unlike what the Command Window allows of single commands
2.2.2. Create an .m file:
2.2.2.1.
click on the new file
icon OR from the menu File->New->M-File
2.2.2.2.
the Matlab Editor window will appear
2.2.3. Run .m file:
2.2.3.1.
Change the Current Directory to the location where .m file is to be saved
2.2.3.2.
use the F5 button OR the play
icon in the Editor window
2.2.3.3.
Matlab will always save the file before the file is run as part of the run process
2.2.4. Naming files (same rules go for naming variables):
2.2.4.1.
Can use letters and numbers
2.2.4.2.
must start with a letter
2.2.4.3.
cannot have any spaces or punctuation, except for underscore “_”
2.2.5. Ignore the .asv file, only use/email the .m file
CS151 - Quick Reference #1
2
2.3. Terms
2.3.1. “Mat” in Matlab stands for matrix
2.3.2. All data in Matlab is stored as a matrix, whether it’s a scalar, vector, or matrix
2.3.3. A scalar is simply a 1x1 matrix
2.3.4. A vector is a matrix that consists of a single row matrix or a single column matrix
2.3.4.1.
A single row matrix is called a row vector
2.3.4.2.
A single column matrix is called a column vector
2.4. Intro commands, functions, operators, etc.
2.4.1. Matlab .m file example:
2.4.1.1.
% is used to make a comments in the code
2.4.1.2.
The top 3 comment lines in the example are the proper way to start quizzes and
homework
2.4.1.3.
clc clears the Command Window
2.4.1.4.
clear deletes all variables in memory (Workspace)
2.4.1.5.
; is used to suppress the output to the Command Window, which is useful in not
showing every little intermediate step
2.4.1.6.
Variables names are on the left of the equals sign, if a value/equation is not
explicitly assigned to a variable, then it will be assigned to the default variable “ans”
2.4.1.7.
Naming variables (same rules go for naming files):
2.4.1.7.1.
Can use letters and numbers
2.4.1.7.2.
must start with a letter
2.4.1.7.3.
cannot have any spaces or punctuation, except for underscore “_”
2.4.1.8.
Equations follow all orders of operation used in algebra
% John Doe
% CS 151-001
% Assignment 1
clc
clear
% Step 1 - assign variables
% initial values
highest_point=100
gravity=-9.81
start_alt=25000
% conversions
ft_to_in=12;
in_to_cm=2.54;
cm_to_m=1/100;
m_to_km=1/1000;
%km
%m/s^2
%feet
%conversion
%conversion
%conversion
%conversion
from
from
from
from
feet to inches
inches to centimeters
centimeters to meters
meters to kilometers
% Step 2 - starting altitude from feet to meters
start_alt = start_alt * ft_to_in * in_to_cm * cm_to_m
% Step 3 - distance traveled
distance_traveled = highest_point/m_to_km - start_alt
% Step 4 - starting altitude from feet to meters
v=0;
u = (v^2 - 2*gravity*distance_traveled)^0.5
CS151 - Quick Reference #1
3