Word

advertisement
ENGR 1187 | MATLAB 1: Introduction to MATLAB
Preparation Material
Learning Objectives
1. Demonstrate proper usage of basic MATLAB features (e.g., the Command Window, script files,
other default windows, arithmetic operations, assigning variables and proper variable names,
built-in functions, help command)
2. Translate mathematical notation into proper MATLAB syntax
Topics
Read Chapter 1 of the MATLAB book before coming to class. This preparation material is
provided to supplement this reading. You will learn a basics of how to use MATLAB. This material
contains the following:
1.
2.
3.
4.
5.
6.
7.
8.
MATLAB Overview
The Command Window
Script Files
Basic Arithmetic Operations with Scalars
Assigning Variables
Variable Names
Built-In Functions
The Help Command
1. MATLAB Overview
MATLAB is a powerful programming tool that can be used for numerical computations, plotting,
and general programming. In many ways, MATLAB is very similar to other programming languages
such as Java and C++. MATLAB, which stands for Matrix Laboratory, excels at mathematical
computations, plotting graphs, and as one might guess, it is very good at handling matrices.
MATLAB is currently taught to all freshman engineers at OSU, and most, if not all, of the engineering
majors at OSU will revisit MATLAB at some point!
2. The Command Window
The command window (boxed in red, See Figure 1) can be used to issue a variety of commands to
MATLAB. Simple calculations such as 3+3 can be entered into the command window, and it will
return you the answer (assigned to the variable ans). In addition to acting as a basic calculator, the
command window can also be used to assign variables, open and run programs and functions, and
it also offers other tools such as MATLAB’s built in help system. The command window is also
where MATLAB will display the results of a program. The Command prompt, denoted by >>, is
where the user can type commands into the command window.
1
ENGR 1181 MATLAB 1: Introduction to MATLAB
Preparation Material
Command prompt
Figure 1: MATLAB Command Window
3. Script Files
A MATLAB script file (.m extension) is a program file created by you the user that executes a series
of MATLAB commands (i.e., basic arithmetic). They are simplest types of files for coding in MATLAB
and are beneficial for programing computations that need to be performed repeatedly.
Program files, such as script files, are opened, edited, and executed in a separate MATLAB window
called the editor window. See, for example, Figure 2.
The run button, boxed in red, can be pressed to run the code within the editor window. Running a
script file will also save the file. Use save as to change the name of the file from the default Untitled.
Figure 2: MATLAB Editor Window
2
ENGR 1181 MATLAB 1: Introduction to MATLAB
Preparation Material
4. Basic Arithmetic Operations
MATLAB has a handful of basic arithmetic operations that can be done within a script file, or
entered directly into the command window.
Symbol
Example
Addition
+
5+3
Subtraction
-
5-3
Multiplication
*
5*3
Right Division
/
5/3
Left Division
\
5\3=3/5
Exponentiation
^
5^3
Operation
Order of Operations:
1.
2.
3.
4.
Parentheses (innermost pair first)
Exponents
Multiplications and division
Addition and subtraction
5. Assigning Variables
A value can be assigned to a variable by using the assignment operator “=”.
For example, if you type “x=3” into the command window and hit enter you will see the following:
>> x = 3
x=
3
MATLAB assigned the value of 3 to the variable x and has this stored in memory. Now we can use
the variable x in arithmetic operations. For example, we can add 5 to x and re-assign this new value
to x.
>> x = x + 5
x=
8
3
ENGR 1181 MATLAB 1: Introduction to MATLAB
Preparation Material
6. Variable Naming Rules
Remember the following when creating variable names:





Can be up to 63 characters long.
Must begin with a letter.
Can contain letters, digits, and the underscore, but no spaces.
MATLAB is case sensitive. This means that X and x are viewed as different variables.
Try to avoid naming a variable the same name as a built in function such as sin or cos.
7. Built-In Functions
Included with the MATLAB software is a library of built-in functions that can be used. Examples of
these functions are provided in Chapter 1 of your book. Note: The book does not cover every built
in function in MATLAB (there are hundreds if not thousands of built-in functions in MATLAB).
8. The Help and Doc Command
The help command in MATLAB is used for getting information on built-in (or user-defined)
functions in MATLAB. The syntax for using the help command is:
>> help function_name
For example, you want to use the function that allows you to take the sine of x (x being a variable
you assigned a value to). MATLAB has a built-in function for sine, “sin”. In the command window
you can type:
>> help sin
And then information about that function will appear in the command window. See, for example,
Figure 3.
Figure 3: The help command in MATLAB
4
ENGR 1181 MATLAB 1: Introduction to MATLAB
Preparation Material
For additional help you can select the document link “doc sin” that will open a new window in
MATLAB with additional information and examples for using the function.
This command is useable during exams so make sure to experiment with it and understand how to
use it.
5
Download