lecture 4 1.matlab windows 2.arithmetic operators 3.maintenance functions 4.basic math functions...

20
Lecture 4 1. MATLAB Windows 2. Arithmetic Operators 3. Maintenance Functions 4. Basic Math Functions 5. Help/Doc 6. Scripts 7. P: Drive 8. Comments 1

Upload: aron-maxwell

Post on 28-Dec-2015

222 views

Category:

Documents


2 download

TRANSCRIPT

1

Lecture 4

1. MATLAB Windows2. Arithmetic Operators3. Maintenance Functions4. Basic Math Functions5. Help/Doc6. Scripts7. P: Drive8. Comments

2

Fundamental components of MATLAB

• MATLAB stands for Matrix Laboratory

• MATLAB = a calculator + a lot more– A computing system that interprets one instruction at a time.

Instructions MUST conform to a specific syntax and vocabulary.– A large library of modules that provide high-level capabilities for

processing data.– A collection of toolboxes, someproviding graphical capabilities.– A graphical user interface (GUI) to allow you to assemble your ownprofessional looking software.

Starting MATLAB

• Opening MATLAB in LB172: computer should have a direct shortcut on the desktop

• Any other LAB on campus, search:

• If not, use Ernie (directions online)

3

Double Click to bring up the MATLAB Interface

4

DO NOT USE CITRIX IN THE LAB

• Why? …

5

“The Command Window”“Current

Directory”

“Workspace”

“Command History”

“Your Calculator Screen”

You can do exactly as on your calculator: add, subtract, divide, multiple, use parentheses to override the order of operations…

Later on, you will realize you can do a LOT more in this frame.

This frame shows the files (excel, text, MATLAB files) that can immediately be used.

Shows the variables that have been created.

It will show all the commands executed previously.

None of these are helpful to write re-usable code.

6

1. Basic Data Manipulation

• Starting MATLAB leads to the prompt symbols >> in the command window

• If you don’t see the prompt symbols, MATLAB is busy

1. MATLAB is loading2. MATLAB is doing 1,000,000

calculations…3. MATLAB has crashed4. MATLAB is stuck

Overview: most Operators

1. ARITHMETIC

+ Addition

- Subtraction

* Multiplication

/,\ Division

^ Exponentiation, i.e. “To the power of”

• There are 3 primary groups of operators

• One operator is very different in programming compared to math

7

2. RELATIONAL

< strictly less than

> strictly greater than

<= less than or equal to

>= greater than or equal to

== is equal to

~= is not equal to

3. BOOLEAN

&& “AND”

|| “OR”

~ “NOT”

= “the assign operator”

8

MATLAB as a Calculator

• The MATLAB command window can be used as a boring old calculator.

• Precedence is similar to most calculators with a few MATLAB specific things. Left to right based on these things:

Operator Symbol Description

( ) Parenthesis

' ^ Transpose (M.O.T.L.) and Power

+ - Signage (Positive/Negative), not Addition/Subtraction (yet)

* / \ Multiplication & Division

+ - Addition & Subtraction

: Colon (More on this later)

Relational Operators (More on this later)

Logical Operators (More on this later)8

9

MATLAB as a Calculator

• Mistake you’ll make at least once!– Power is before signage:

-3^2 is different than (-3)^2

Operator Symbol Description

( ) Parenthesis

' ^ Transpose (More on this later) and Power

+ - Signage (Positive/Negative), not Addition/Subtraction (yet)

* / \ Multiplication & Division

+ - Addition & Subtraction

: Colon (More on this later)

Relational Operators (More on this later)

Logical Operators (More on this later)9

10

• Demo MATLAB as a calculator in the command window (without variables)

• Any single line contains a command/statement/instruction (different words for the same concept)

• The ans variable is the default location where MATLAB stores results.

11

Maintenance Functions

• clear• clc• close all• commandwindow

12

Basic Math Functions

• sin• cos• tan• sind• sqrt• abs

• help vs. doc• help elfun

13

the “Editor”

• “Open a new script file"

14

A new window: the Editor

Save these type of files (on your P:/ drive )

absolutely re-usable

This is where you’ll create a “script file”

15

A new window: the Editor

“Dock” this window to make it part of the regular windows below

Final common setup!

1616

17

How to write “code”…

1. Type your statements, your code, your instructions,… here

2. Run your statements (or hit F5)

3. See your results here

(Modify your statements, repeat cycle)

18

P: Drive

• The P: drive is space provided by ERAU that you can access from any ERAU computer.

• Getting files from your P: drive to your personal computer can be done through ERNIE/Citrix/Network File Access, but all the transferring must be done from within that program and it is sometimes grouchy.

19

Comments

• Comments are the most important part of your code.• Put the algorithm steps (7a) in comments before you start• Write the code that goes with the comments• Update the comments to make them more detailed where needed.

• Someone else should be able to write your code based on your comments.

• % <- Supported by all versions of MATLAB• %{ <- Must not have anything else on the line with open/close %} (Not supported in versions before 7/R14/2004)

20

Lessons learned

• New Vocabulary– Command window– Workspace– Current Folder/Current Directory– Command History– Command– Statements– Instructions– Prompt symbols– Script file