24
Eng.Ahmed Farouk 01026409833

Matlab 1 level_1

Embed Size (px)

Citation preview

Page 1: Matlab 1 level_1

Eng.Ahmed Farouk 01026409833

Page 2: Matlab 1 level_1

Session 1

Content

What is Matlab ?

Introducing the Matlab Development Environment

Performing Basic Arithmetic,

Performing Logical and Relational Operations

Eng.Ahmed Farouk 01026409833

Page 3: Matlab 1 level_1

What is Matlab?

The name Matlab stands for matrix laboratory.

It is a high-performance language for technical computing.

Matlab can deal with Modeling ,simulation , Data analysis , Scientific and

engineering graphics,…….

Matlab can deal with sounds, images ,text, excel data,…… .

Matlab is an interactive system whose basic data element is an array

that does not require dimensioning. This allows you to solve many

technical computing problems, especially those with matrix and vector

formulations, in a fraction of the time it would take to write a program in a

scalar non-interactive language such as C or Fortran.

Eng.Ahmed Farouk 01026409833

Page 4: Matlab 1 level_1

Introducing the Matlab Development

Environment

If you happen to get employed in a new place, the first thing you would

probably do is to take a brief tour to experience your new

workplace and its subdivisions, or what we alternatively refer to by

the term “Development Environment”.

For someone working in a factory, the development environment refers

to the work place with all its subdivisions and all the resources and

tools it contains.

For Matlab users, the development environment refers similarly to the

Matlab window and all the resources and tools it contains.

The first step in understanding how Matlab works is to experience its

development environment.

Eng.Ahmed Farouk 01026409833

Page 5: Matlab 1 level_1

When you start Matlab, the figure on the next page depicts what you

will see.

Eng.Ahmed Farouk 01026409833

Page 6: Matlab 1 level_1

Eng.Ahmed Farouk 01026409833

Page 7: Matlab 1 level_1

The Command Window

The command window is an interpreter where you can write and

execute instructions one at a time or few at a time.

You know where to write your next instruction from the „>>‟ mark,

which is called the command prompt.

The Workspace Browser

The workspace is the group of variables (arrays) you are working with.

The workspace browser provides you with an overview of the workspace, meaning that it shows you the variable you have created so far.

It shows you the variable name, size, data type and its value if the value is compact enough to be displayed, such as that of a single number or few characters.

By double clicking on any variable, you get to inspect this variable in detail.

Eng.Ahmed Farouk 01026409833

Page 8: Matlab 1 level_1

The Command History

It is an archive of commands previously executed in the command window.

You may use it to retrieve and re-execute previous commands.

The command window doesn‟t log your commands from the day you first

opened Matlab; it has a maximum buffer size.

Once reached, any new command will append to the bottom of the buffer,

causing the topmost command to be lost.

By setting the buffer size to 10,000 for instance, you can make the command

window “remember” your last 10,000 commands.

Eng.Ahmed Farouk 01026409833

Page 9: Matlab 1 level_1

The Current Directory

Below the workspace, you can see a tab selection that

lets you select either the workspace or the current

directory to view.

Click on the “Current Directory” tab.

The current directory is a folder you choose to be your

default working folder.

Whenever Matlab requires a user file, it will start its

search by looking into this folder.

Whenever Matlab needs to save a user file, it will save it

in this folder by default.

Eng.Ahmed Farouk 01026409833

Page 10: Matlab 1 level_1

The rest of the elements of the development environment

do not appear at startup by default, but you can call

them whenever you need them. We continue our

discussion of the development environment by looking at the

rest of its elements like:

The Array Editor.

The M-File Editor.

The Figure Window.

The GUI Development Environment (GUIDE).

Simulink.

Eng.Ahmed Farouk 01026409833

Page 11: Matlab 1 level_1

The Array Editor

The array editor is a window that opens to show you the contents of a

variable (array) and possibly make you change these contents.

The array editor opens when you double click on a variable in the workspace

browser.

The figure below shows an instant of the array editor opened to show us

variable “ImA”, a matrix resulting from loading an image into the workspace.

Page 12: Matlab 1 level_1

The M-File Editor

The M-File Editor is a text-entry window where you can write down complete

programs.

The following figure depicts the M-File editor.

You can start the M-File editor by typing edit at the command prompt (>>edit)

or by clicking the New M-File button on the startup screen.

The M-File editor contains facilities for writing, debugging and running your

programs.

Eng.Ahmed Farouk 01026409833

Page 13: Matlab 1 level_1

The Figure Window

A figure window is a window used with graphics.

A graphic cannot be displayed in the command window.

Therefore it opens in a separate window equipped with facilities for handling

graphic, which is the figure window.

Page 14: Matlab 1 level_1

The GUI Development Environment(GUIDE) The GUIDE is a tool for building GUIs like the one shown above.

The GUIDE may be started by typing guide at the command prompt (>>guide)

or by going to New -> GUI in the Matlab File menu.

The development environment for GUIs will then open. The GUI tool looks as

depicted in the figure below.

Page 15: Matlab 1 level_1

Simulink

Simulink is a Matlab-associated package used for system design, modeling and

system analysis.

The figure below shows an example of a house thermodynamic subsystem

designed using Simulink.

Eng.Ahmed Farouk 01026409833

Page 16: Matlab 1 level_1

Basic Arithmetic,

Relational and Logical

Operations

Eng.Ahmed Farouk 01026409833

Page 17: Matlab 1 level_1

Basic Arithmetic, Relational and Logical

Operations

Matlab always starts by evaluating the right-hand-side (RHS) of the

expression, which is (2 + 3).

This operation evaluates to (5).

So Matlab will try to assign the value 5 to the left-hand-side (LHS), which is

(E).

However, the variable (E) does not exist yet, so Matlab will create a variable

called (E) then carry out the assignment.

Hence the result of this expression is that E equals 5.

Eng.Ahmed Farouk 01026409833

Page 18: Matlab 1 level_1

Basic Arithmetic, Relational and

Logical Operations

Note:

If the variable E already existed, its previous contents will be replaced

by the result of evaluating the new expression.

If the expression contains an operation without a LHS variable to which the

result may be assigned, Matlab automatically uses a default variable

called “ans‟ standing for “answer”. To try this, write (>> 2+3).

If you append any expression by a semicolon (;), the expression will

be evaluated normally, but the output of its evaluation won’t be

reported in the command prompt.

Eng.Ahmed Farouk 01026409833

Page 19: Matlab 1 level_1

Arithmetic Operations

The following table shows a list of the available arithmetic operations and

their description.

Eng.Ahmed Farouk 01026409833

Page 20: Matlab 1 level_1

Logical Operations

In arithmetic operations, the operands may have any numerical values.

In logical operations, the operands may have one of two values: either

nonzero or zero (also called true or false).

In input operands, 0 is used to indicate (logic 0), while any number other than

0 may be used to indicate (logic 1).

In the results of a logical operation, a 0 is always used to indicate (logic 0)

while a 1 is always used to indicate (logic 1).

Eng.Ahmed Farouk 01026409833

Page 21: Matlab 1 level_1

Logical OperationsNote:

Other logical operations such as NANDing, NORing and XNORing may be

performed by combining two or more of the previous operators.

Example: c=~(A|B) is equivalent to NORing A with B then assigning the result to c.

Logic 0 is also referred to as „False‟, and logic 1 is also referred to as „True‟.

Examples:

>>a=-5;b=2;c=0;

>>x=a & b ;

%x=1

>>y=c & b ;

%y=0

>>z=c | b ;

%z=1

>>w=~b ;

%w=0

Note:

You can get up to the previous commands by using the up and down arrows.

Page 22: Matlab 1 level_1

Relational Tests

Relational operations are simple in their concept. A relational operation is a

test done on a certain expression. Hence, they are also called relational

tests.

The test evaluates to (Logic 1) if the expression is true and evaluates to

(Logic 0) if the expression is false.

Eng.Ahmed Farouk 01026409833

Page 23: Matlab 1 level_1

Relational Tests

Examples:

>>a=5;b=2;

>>x=a>b;

%x=1

>>x=(a~=b)

%x=1

Note:

Many users confuse the double equality sign (==) used in relational tests with

the equality sign (=) used in assignments.

When a user uses (=) instead of (==), Matlab usually reports that an expected

relational operator wasn’t found.

Eng.Ahmed Farouk 01026409833

Page 24: Matlab 1 level_1

Combining Different Operations

Let us assume that we want to operate an alarm mechanism if an “automatic

temperature alarm” button is pressed and the room temperature exceeds the

nominal by 5 degrees.

The status of the “automatic temperature alarm” button is 0 if the button is

not pressed and is 1 if the button is pressed.

The temperature is read by a sensor.

We may use Matlab interfacing techniques to read the status of the button

and the value of the temperature into 2 variables “STATUSon” and “Temp”

respectively.

We want to create a variable „Alarm‟ which is 1 if the button is pressed and

the temperature exceeds the nominal temperature „Nominal‟ by 5.

The expression used to compute such variable is:

Eng.Ahmed Farouk 01026409833