Title: E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS
1E77 INTRODUCTION TO COMPUTER PROGRAMMING FOR
SCIENTISTS AND ENGINEERS
- Lecture Outline
- Matlab basics
- 2. Matlab as a calculator
- 3. Saving and loading files in matlab
2Matlab basis
- The default MATLAB Desktop
3Matlab basis
- Starting Matlab
- double-click the Matlab icon, or
- in a terminal window, type matlab, and return
- The Command Window and the
- Matlab prompt gtgt
4Matlab basis
- The Desktop menu
- undocking command window
- tiling other windows (command history, workspace,
current directory, profiler) - The File menu
- Preferences
- The Help menu
5Matlab as a calculator
- Type expressions at the gtgt, and press return
- Result is computed, and displayed as ans
- Use numbers, , , /, -, (), sin, cos, exp, abs,
round,
6Entering Commands and Expressions
- MATLAB retains your previous keystrokes.
- Use the up-arrow key to scroll back back through
the commands. - Press the key once to see the previous entry, and
so on. - Use the down-arrow key to scroll forward. Edit a
line using the left- and right-arrow keys the
Backspace key, and the Delete key. - Press the Enter key to execute the command.
7Scalar Arithmetic Operations
Symbol Operation MATLAB form
exponentiation ab ab multiplication
ab ab / right division a/b a/b \ left
division b/a a\b addition a b a
b - subtraction a - b a - b
8Order of Precedence
Precedence Operation
First Parentheses, evaluated starting with the
innermost pair. Second Exponentiation,
evaluated from left to right. Third Multiplicatio
n and division with equal precedence, evaluated
from left to right. Fourth Addition and
subtraction with equal precedence, evaluated
from left to right.
9Examples of Precedence
gtgt 8 35 ans 23 gtgt 8 (35) ans
23 gtgt(8 3)5 ans 55
gtgt 4212 8/42 ans 0 gtgt (42)12
(8/4)2 ans 0 gtgt 4212 8/(42) ans
3
10Special Variables and Constants
Symbol Description
ans most recent answer. eps Specifies the
accuracy of floating point precision. i,j The
imaginary unit Ö-1. Inf Infinity. NaN Indicates
an undefined numerical result. pi The number p.
11Variables and the assignment operator ()
- Use names to assign result of an expression to a
variable - i.e. assign the value 3 to the variable x
- Variables do not need to be declared before
assignment - The right-hand-side needs to be a legal Matlab
expression - The left-hand-side needs to be a single variable
name
12Examples of Variables and Assignment
means
gtgt A sqrt(4) A 2
gtgt C tan(pi/4) A C 3
means
(variable A must have been defined)
13Examples of Variables and Assignment
gtgt A sqrt(4) gtgt A tan(pi/4) A
gtgt A 3
- A semicolon at the end of the RHS expression
suppresses the display. - However, the assignment still takes place.
14The workspace
- All variables that you create are accessible from
the prompt gtgt - Variables are accessed using their name as a
reference - Builtin Matlab commands
- gtgt who
- gtgt whos
- are used to see what is in the workspace.
- You can clear (i.e., erase) variables with the
clear command - gtgt clear A
- clears the variable A from the workspace.
15The workspace
- All variables that you create are accessible from
the prompt gtgt - Variables are accessed using their name as a
reference - Builtin Matlab commands
- gtgt who
- gtgt whos
- are used to see what is in the workspace.
-
- gtgt clear all
- clears ALL the variables from the workspace.
16Quiting Matlab
- Type quit at the prompt, or
- Select Exit Matlab from the File menu
- However
17Saving the workspace
- When you quit Matlab, the variables in the
workspace are erased from memory. - If you need them for later use, you must save
them. - gtgt save
- saves all of the variables in the workspace into
a file called matlab.mat (it is saved in the
current directory)
18Saving the workspace
- gtgt save Roberto
- saves all of the variables in the workspace into
a file called Roberto.mat - gtgt save Important A B C D
- saves the variables A, B, C and any variable
beginning with D into a file called Important.mat
19Loading from a .mat file
- load is the opposite of save.
- It reads a .mat file, putting all variables
contained in the .mat file into the workspace - gtgt load
- loads all of the variables from the file
matlab.mat
20Loading from a .mat file
- gtgt load Roberto
- loads all of the variables from the file
Roberto.mat - There are no known security problems with load.
- Hence, you can safely send (as attachment),
receive and use .mat files from others.
21Summary
- MATLAB can be used as a high-end calculator
- variables can be defined and manipulated.
- Precedence left to right and , / ,
- When in doubt for precedence, use parentheses!
- Variables can be cleared, saveed and loaded.
- MATLAB has special symbols e.g. pi , ANS , j ,
etc.