Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.

29
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1

Transcript of Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.

2

Fundamental components of MATLAB

MATLAB stands for Matrix Laboratory

MATLAB = a calculator + a lot more A computing system that accepts 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, providing graphical capabilities. A graphical user interface (GUI) to allow you to assemble your

own professional looking software.

5

Frame #1“The Command Window”

Frame #2“Current

Directory”

Frame #3“Workspace”

Frame #4“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.

It will show the variables that have been created.

It will show all the commands executed previously.

1. Basic Data Manipulation

A variable is a name given for a memory location “Assigning a value to a variable” means to place a value

in the memory location associated with the variable name

MATLAB usually shows what the variables are - in the Command Window, but also in the Workspace.

15

1. Basic Data Manipulation

How about solving equations?

Normal algebra ≠ programming Assume the following mathematic equation:

z = x + y

In algebra: when z is 10 and y is 7, what is x equal to?

z = x + y

10 = x + 7

Solve for x, obtain _____ Look at MATLAB:

18

1. Basic Data Manipulation

In MATLAB, all the known variables must be on the right side of the equal sign before executing the command.

At any time, only one variable can be on the left.

Assign values to variables z and y

Once x is on the left side, and all known variables (z and y) are on the right, MATLAB executes the command.

19

2. Basic Data Understanding

How exactly is the data stored in the memory? binary (i.e. machine) language: 0 and 1’s

How is the number 2 represented then?

Remember that the symbol we see is NOT the value represented by it.

For example: what number do these represent?

V |||| 5

2.1 The Binary System

As shown on the previous slide, a number can be represented using different symbols used for different bases. In fact, any set of symbols can be created to represent numbers.

In the “binary” system (base 2), there are only two symbols:

0 and 1

With just these two symbols, a computer can represent any number we want!

26

The limit between calculator and programming software Although MATLAB looks like a calculator, data can be stored in ASCII

format as well. Be careful: MATLAB will execute mathematical operations on characters.

What does this mean?

For example, in the command window, Do It Yourself (2.3) radius = 49 <enter> radius + 1 <enter>

radius = ‘radius of a circle’ <enter> radius + 1 <enter>

What is the expected outcome?

What is the un-expected outcome?

27

3. Creating Good Variables

This last example should make you realize the importance of good variable names: those that

describe their content.

Do not name a variable radius when it will contain a name, or a sentence, prefer title_radius in the example above.

In the long term, it will save you time debugging (fixing errors!)

28

3. RULES

To create a variable in MATLAB, simply assign it a value.

When coming up with a name, follow these RULES: It cannot contain spaces: Use the underscore, or

capitalizeTheFirstLetterOfAdditionalWord It cannot start with a digit. However, it can have digits in the rest

of it. It cannot contain any special characters (other than the

underscore of course!) It should not be any keywords MATLAB knows, or It should not be the filename of any saved files

29

3. Good HABITS

Good Programming Habits applied in EGR115

It must represent its content NO one letter long variables, except for loop counters: v, t, p, x Keep it short, it will avoid typos later on If you can, include units IN MATLAB: lower case i (Square root of -1) and j already have

values. Other letters are better choices.