matlab the first steps - university of miskolc · matlab the first steps edited by péter vass....

29
MATLAB The first steps Edited by Péter Vass

Upload: others

Post on 28-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

MATLAB

The first steps

Edited by Péter Vass

Page 2: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

MATLAB

The name MATLAB is derived from the expression MATrix

LABoratory.

It is used for the identification of a software and a

programming language.

As a software, it provides an interactive computational and

program development environment which supports its own

programming language.

It is a commercial software (not free and not cheap) and

developed by the MathWorks Inc.

Page 3: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

The basic features of MATLAB programming

language

It is a 4GL programming language,

an interpreted programming language (the interpreter is built

in the software),

a domain-specific programming language (it is aimed at solving

scientific and engineering problems).

It supports the imperative, the structural, the procedural and

the object-oriented programming.

It has sophisticated data structures.

It provides vast number of built-in functions for linear algebra,

statistics, Fourier analysis, filtering, optimization, numerical

integration and solving ordinary differential equations.

Page 4: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

The basic features of MATLAB programming

language

These functions are collected in packages referred to as

toolboxes.

It provides built-in graphics for visualizing data,

enables the user to create applications with graphical user

interface (GUI),

and also provides interfaces for programs or functions written

in other languages such as C, C++, Java, and FORTRAN.

Page 5: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Matlab Integrated Development

Environment (IDE)

Launching the MATLAB IDE

• double-clicking the MATLAB shortcut on the desktop,

• or find the MATLAB item and click it in the start menu

Panels of the MATLAB desktop (in the default layout)

• Current Folder

• Command Window

• Workspace

• Command History

Page 6: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Matlab Integrated Development

Environment (IDE)

CommandWindow

Current Folder

Workspace

Command

History

Page 7: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Matlab Integrated Development

Environment (IDE)

Current Folder

allows us to view, open, search for folders and files, and make

changes to them.

Command Window

provides the command prompt (>>) which indicates the position of

the command line.

The command to be executed must be entered here.

The entered command is sent to the interpreter which tries to

execute it.

If the command is incorrect or some condition is not fulfilled, the

interpreter sends an error message which is also displayed in this

window.

Page 8: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Matlab Integrated Development

Environment (IDE)

Command Window

Otherwise, it executes the command and the result appears in the

window.

The commands previously entered and the results are listed in

sequence above the command line.

Workspace

shows some information about the variables previously created

and/or read from files.

Command History

lists the commands previously entered in the Command Window.

Any command may be recall from here and rerun in the command

line by double-clicking it.

Page 9: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Matlab Integrated Development

Environment (IDE)

Customizing the environment

Home tab Environment section Preferences

Page 10: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Matlab Integrated Development

Environment (IDE)

Invoking the MATLAB help browser

Home tab Resources section Help icon

Page 11: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Matlab Integrated Development

Environment (IDE)

From the command line

Type doc in the command line and press the enter key ().

>> doc

Getting information about a valid function

>> help funtion_name

>> doc function_name

Example:

>> help exit

>> help quit

>> doc clc

>> clc Watch the content of the Command History window.

>> exit

Page 12: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Matlab Integrated Development

Environment (IDE)

Searching help entries for all MATLAB commands linked

up with a keyword

>> lookfor keyword

Example:

>> lookfor spectrum

There are different levels of working with the MATLAB

environment:

• using it as a high-performance, complex calculator by typing and

running commands one after another,

• writing a script which contains a series of commands arranged in

suitable control structures, and is stored in a script file, then

running it as a single command,

• writing user-defined functions for specific tasks and calling them

in the command line.

Page 13: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Using MATLAB as a calculator

In the simplest case, we use only numerical constants in our

computation.

The symbols used for the notation of basic mathematical operations

(operators):

+ * / ^

To modify the order of operations, use ( )

Example:

>> (4 + 2) / 3

The result of an executed command is stored in a special variable

named ans (answer). See the content of the Workspace panel.

Example:

>> ans^2

Note that the value of ans has changed in the Workspace panel.

Page 14: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Using MATLAB as a calculator

Other special variables for mathematical symbols:

pi

Inf

i and j −1 (imaginary unit)

Nan not a number

Examples:

>> pi

>> -2.5/0

>> (-3) ^ 0.5

>> 2+3j * -(5+6j)

>> 0/0

Page 15: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Using MATLAB as a calculator

Setting the output format

The default output format is the short format which displays

numbers with four decimal place values.

By means of the format command, the output format may be

changed.

Some other output format:

• long format displays 15 digits after the decimal point,

• bank format rounds numbers to two decimal places (for finantial

computation),

• format short e displays numbers in exponential form with a base

of four decimal places and the exponent,

• format long e displays numbers in exponential form with a base

of 15 decimal places and the exponent

Page 16: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Using MATLAB as a calculator

Example:

>> 3 + 4/3 – 6^2.3

>> format long

>> ans

>> format bank

>> ans

>> format short e

>> ans

>> format long e

>> ans

>> format short

>> ans

Page 17: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Using MATLAB as a calculator

Editing the command line

There are some useful keyboard shortcut for editing the command

line such as:

↑ recalls the previous command

↓ recalls the next command

Esc clears the command line

← moves the cursor one character backward

→ moves the cursor one character forward

Ctrl ← moves the cursor one word to the left

Ctrl → moves the cursor one word to the right

Page 18: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Using MATLAB as a calculator

Editing the command line

Home, End, Del and Backspace keys may also be used with their

well-known functions.

Variables

In order to perform more complex computations, we must apply

variables in our commands.

A variable is a storage location in the memory which serves for

keeping the value of some data, and it has an individual name.

We can assign a value to it, and use its value in expressions.

A variable has three main properties:

• its name,

• its type,

• and its value.

Page 19: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Using MATLAB as a calculator

Variable assignment

variable_name = value or expression

Expression

is a combination of different elements.

It may contain:

constants, variables, operators (symbols for the operations), and

function calls.

Examples

>>x = 5 See the content of the Workspace panel.

>>x = x + 2

>>y = 2*x – sin(pi/4)

sin() is a built-in function for implementing the mathematical sine

function in MATLAB

>>help sin

Page 20: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Using MATLAB as a calculator

An important rule

The variable assignment always has to precede the first use of the

variable in a computation.

So, a variable without any value cannot be used in an expression.

Example

>>x = 3

>>x = x + z

Since the variable z has not a definite value, the expression in the

last command is wrong.

MATLAB cannot execute it and we get an error message.

Page 21: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Using MATLAB as a calculator

Semicolon in MATLAB

It may used at the end of the commands.

If we use it, MATLAB will not display the output of the executed

command.

Example

>>x = 3;

>>y = x + 5

Multiple variable assignments

It is possible to perform more than one variable assignment in the

same command line in the following way:

>> x = 3.4; y = 7.2; z = x * y

Page 22: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Using MATLAB as a calculator

Naming rules of variables

• a variable name may contain only letters, numbers and

underscore characters,

• the first character must be a letter,

• since MATLAB is case-sensitive, the capital and small forms of a

letter are differentiated,

• although there is no limit for the length of a variable name, still

only a limited number of characters from the beginning are taken

into account (run the function namelengthmax).

Example

>>varName=5;

>>VarName=8;

>>varName+VarName

>>namelengthmax

Page 23: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Using MATLAB as a calculator

Variable types

MATLAB automatically determines the type of a variable whenever

we assign a value or expression to it.

So, we need not declare the type of any variable unlike other

programming languages such as C, Pascal, Fortran, Java etc.

Clear command

The variables we have created during a MATLAB session are stored

in the memory of the computer.

It may be necessary to free some memory by erasing useless

variables.

Erasing variable x from the memory.

>>clear x

Erasing all the variable from the memory

>>clear

Page 24: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

MATLAB clones

MATLAB is an excellent software but it is not free and not cheap.

This is the reason why someone may consider trying out a free

alternative of MATLAB.

There are some free MATLAB clones or MATLAB-like applications

such as:

GNU Octave, SciLab and Rlab.

The home page of Octave:

https://www.gnu.org/software/octave/

The home page of Scilab:

http://www.scilab.org/en/scilab/about

The home page of Rlab:

http://rlab.sourceforge.net

Octave is the most compatible with MATLAB of all the clones.

Page 25: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Octave

MATLAB is an excellent software but it is not free and not cheap.

This is the reason why someone may consider trying out a free

alternative of MATLAB.

There are some free MATLAB clones or MATLAB-like software

such as:

GNU Octave, SciLab and Rlab.

The home page of Octave:

https://www.gnu.org/software/octave/

The home page of Scilab:

http://www.scilab.org/en/scilab/about

The home page of Rlab:

http://rlab.sourceforge.net

Octave is the most compatible with MATLAB of all the clones.

Page 26: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Graphical User Interface (GUI) of Octave

Launching Octave

• double-click the Octave (GUI) shortcut on the

desktop,

• or find the Octave item and click it in the start menu

Panels of the Octave desktop (in the default layout)

• File Browser

• Command Window

• Workspace

• Command History

Page 27: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Graphical User Interface (GUI) of Octave

CommandWindow

File Browser

Workspace

Command

History

Page 28: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

Graphical User Interface (GUI) of Octave

Setting the current directory (or folder)

The selection of the current directory is important when we want

to read data from files or save data into files or run our own scripts

or call our own functions.

The path of the current directory is displayed on the right side of

the toolbar.

We can take a step upwards in the directory system by clicking the

green upwards arrow (One directory up).

We can directly select any path by clicking the rightmost blue open

folder icon. This action invokes a common dialog window "Browse

directories".

If the current directory has been changed, the content of the

selected directory will be displayed in the "File Browser" window.

Page 29: MATLAB The first steps - University of Miskolc · MATLAB The first steps Edited by Péter Vass. MATLAB The name MATLAB is derived from the expression MATrix ... It provides built-in

References

https://www.tutorialspoint.com/matlab/

http://mayankagr.in/images/matlab_tutorial.pdf

https://www.mathworks.com/support/learn-with-matlab-

tutorials.html