0% found this document useful (0 votes)
53 views34 pages

Matlab For Beginners (Chap 1)

This document provides an overview and introduction to MATLAB. It outlines the course description, assessment, references, and covers starting MATLAB including the desktop interface and entering commands. It also discusses MATLAB sessions including arithmetic operators, order of precedence, variables, and formatting numbers. The goal is to provide students with a gentle introduction to using MATLAB for data analysis.

Uploaded by

Lan Anh Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views34 pages

Matlab For Beginners (Chap 1)

This document provides an overview and introduction to MATLAB. It outlines the course description, assessment, references, and covers starting MATLAB including the desktop interface and entering commands. It also discusses MATLAB sessions including arithmetic operators, order of precedence, variables, and formatting numbers. The goal is to provide students with a gentle introduction to using MATLAB for data analysis.

Uploaded by

Lan Anh Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

MATLAB overview

Tan Do

Vietnamese-German University

Lecture 1

IMPORTANT : Install MATLAB on your laptop


&

bring it to the lectures for this course

Tan Do (VGU) Introduction to MATLAB Lecture 1 1 / 34


Study guide

Lecturer

Do Duc Tan
Email [email protected]

Oce A.105

Oce hours By appointments

Tan Do (VGU) Introduction to MATLAB Lecture 1 2 / 34


Study guide

Course description and assessment

Aim This course provides a gentle introduction to data analysis using

MATLAB for FA and BA students.

Assessment Attendance and tutorials (5%) and exam (45%).

Attendance and tutorials: Don't be late. There are 4 tutorials.

Exam: To be announced.

Look through the Syllabus for more information.

Tan Do (VGU) Introduction to MATLAB Lecture 1 3 / 34


Study guide

References

Some useful books for this course are the following.

1 MATLAB An Introduction with Applications by Amos Gilat.

2 An Introduction to Matlab by Ahlersten Krister.

3 Introduction to Linear Optimization and Extensions with MATLAB by

Roy H. Kwon.

Tan Do (VGU) Introduction to MATLAB Lecture 1 4 / 34


Content

In this lecture

MATLAB desktop: Command window, Workspace, Current folder, . . .

MATLAB session: entering commands and expressions, arithmetic

operators, order of precedence, assignment operator and naming

variables.

Managing work session: clear, clc, who, format, . . . commands.

Getting help with MATLAB.

Save and load a MATLAB session.

Tan Do (VGU) Introduction to MATLAB Lecture 1 5 / 34


Introduction

MATLAB overview

MATLAB = Matrix Laboratory.

High-level language designed for scientic computing.

Very popular in science community.

Can be used for both data analysis and programming.

Tan Do (VGU) Introduction to MATLAB Lecture 1 6 / 34


Introduction

High-level language vs. low-level language

(b) MATLAB

(a) x86 assembly language

Figure: Calculating Fibonacci sequence


Tan Do (VGU) Introduction to MATLAB Lecture 1 7 / 34
Starting MATLAB

MATLAB desktop

Default MATLAB desktop

Command window is used to communicate with MATLAB.

Workspace window displays variables created in the Command window.

Current folder window shows all MATLAB les in the current folder.

Other useful windows include Command history and Details window.

Tan Do (VGU) Introduction to MATLAB Lecture 1 8 / 34


MATLAB session

Entering commands and expressions

Command prompts () appear in Command window.

Type commands after the prompts to interact with MATLAB.

Examples:

» 1 + 1
ans =
2
» 3*ans
ans =
6
» a = 8
a =
8
Tan Do (VGU) Introduction to MATLAB Lecture 1 9 / 34
MATLAB session

Semicolon and spacing

Putting semicolon ; at the end of the command will suppress the display.

» a = 8;
»

Spacing in the commands is not important, but improves readability.

» a=8
a =
8

» a = 8
a =
8
Tan Do (VGU) Introduction to MATLAB Lecture 1 10 / 34
MATLAB session

Comma

You can use comma , to separate commands on the same line.

» 1 + 1, a = 8; b = 5, a + b
ans =
2
b =
5
ans =
13

Note that the value of the variable a was not displayed.

Tan Do (VGU) Introduction to MATLAB Lecture 1 11 / 34


MATLAB session

Strings

You can assign strings to a variable.

» name = ’Tan’
name =
Tan
You can also concatenate strings together.

» name = [name ’ Do’]


name =
Tan Do
To access parts of the string, type

» name(1)
ans =
T
Tan Do (VGU) Introduction to MATLAB Lecture 1 12 / 34
MATLAB session

» name(5)
ans =
D

To display letters from positions 2 to 5 in the variable name, type

» name(2:5)
ans =
an D

Your interaction with MATLAB is called an interactive session or a session

for short.

Tan Do (VGU) Introduction to MATLAB Lecture 1 13 / 34


MATLAB session

Arithmetic operators

There are 5 arithmetic operators: addition +, subtraction -, multiplication

*, division / and exponentiation b.


» x = 3 + 5*4
x =
23
» 2b3 - 10
ans =
-2
» r = 3/10
r =
0.3000
Tan Do (VGU) Introduction to MATLAB Lecture 1 14 / 34
MATLAB session

» s = 30*r
s =
9

Typing * is essential. The following command results in a error message.

» s = 30r
s = 30r
|

Error: Unexpected MATLAB expression.

Tan Do (VGU) Introduction to MATLAB Lecture 1 15 / 34


MATLAB session

Right and left division

» 15/3
ans =
5

/ is called forward slash. It represents right division.

There is also backward slash \ which represents left division.

» 15\3
ans =
0.2000

Note that the slash slants toward the denominator, i.e., 15\3 = 3/15 = 0.2.

Tan Do (VGU) Introduction to MATLAB Lecture 1 16 / 34


MATLAB session

Order of precedence

Question How to evaluate

4b2 − 12 − 8/4 ∗ 2?
The arithmetic operators +, -, *, /, \ and b follow a set of rules called

precedence.

Precedence Operation
First Parentheses, evaluated from the innermost pair

Second Exponentiation, evaluated from left to right

Third Multiplication and division with equal precedence,

evaluated from left to right

Fourth Addition and subtraction with equal precedence,

evaluated from left to right

Tan Do (VGU) Introduction to MATLAB Lecture 1 17 / 34


MATLAB session

» 8 + 3*5
ans =
23

» (8+3)*5
ans =
55

» 4b2 - 12 - 8/4*2
ans =
0

» 4b2 - 12 - 8/(4*2)
ans =
3

Tan Do (VGU) Introduction to MATLAB Lecture 1 18 / 34


MATLAB session

» 3*4b2 + 5
ans =
53

» (3*4)b2 + 5
ans =
149

Tan Do (VGU) Introduction to MATLAB Lecture 1 19 / 34


MATLAB session

» 27b(1/3) + 32b(0.2)
ans =
5

» 27b(1/3) + 32b0.2
ans =
5

» 27b1/3 + 32b0.2
ans =
11

Tan Do (VGU) Introduction to MATLAB Lecture 1 20 / 34


MATLAB session

Assignment operator

The = is called assignment operator or replacement operator in MATLAB.

The following command assigns value 6 to the variable x.

» x = 6
x =
6
We can also type

» x = x + 2
x =
8
The above command tells MATLAB to add 2 to the current value of x

(which is 6), and then to replace the current value of x with this new value

(which is 8).
Tan Do (VGU) Introduction to MATLAB Lecture 1 21 / 34
MATLAB session

The order in the command

» x = 6
is important.

The variable on the left-hand side of = is assigned to the value generated

by the right-hand side.

Therefore the following commands are not possible.

» 6 = x
6 = x
|

Error: The expression to the left of the equals sign is


not a valid target for the assignment.

Tan Do (VGU) Introduction to MATLAB Lecture 1 22 / 34


MATLAB session

» x + 2 = 10
x + 2 = 10
|

Error: The expression to the left of the equals sign is


not a valid target for the assignment.

» x = 6 + y
Undefined function or variable y.

Tan Do (VGU) Introduction to MATLAB Lecture 1 23 / 34


MATLAB session

Variable names

Rules for naming variables:

1 Beginning with a letter, the rest can contain letters, digits and

underscore characters.

2 MATLAB is case-sensitive. So speed, Speed, SPEED, Speed_1


and Speed_2 are all dierent variables.

3 Variable names must not be longer than 63 characters.

4 Employing meaningful variable names.

5 Variable names should not coincide with MATLAB built-in commands,

functions, predened constants and variables.

Note that these rules are also applied to naming MATLAB les.

Tan Do (VGU) Introduction to MATLAB Lecture 1 24 / 34


MATLAB session

Predened constants and variables

Name Description
ans Temporary variable containing the most recent answer

i, j Imaginary unit −1
Inf Innity

NaN Indicates an undened numerical result

pi Number π

Tan Do (VGU) Introduction to MATLAB Lecture 1 25 / 34


MATLAB session

Control how numbers look like

format command controls how numbers appear on the screen.

The default format is format short, which gives numbers 4 signicant

gures.

» x = pi
x =
3.1416
» format short
» x
x =
3.1416

Tan Do (VGU) Introduction to MATLAB Lecture 1 26 / 34


MATLAB session

format long command gives numbers 15 signicant gures.

» format long
» x
x =
3.141592653589793
Other format commands include

» format bank
» x
x =
3.14
» format rat
» x
x =
355/113
Tan Do (VGU) Introduction to MATLAB Lecture 1 27 / 34
MATLAB session

format short e and format long e commands force the output to

be in scientic notation.

» y = 32/2017
y =
0.0159
» format short e
» y
y =
1.5865e-02
» format long e
» y
y =
1.586514625681705e-02
Here e stands for 10 to the power of . For example, e-02 equals 10−2 .
Tan Do (VGU) Introduction to MATLAB Lecture 1 28 / 34
MATLAB session

Managing work sessions

The following are some useful commands for managing a work session.

Command Description
clc clears the Command window, variables retain values
clear removes all variables from memory
clear var1 var2 removes the variables var1 and var2 from memory
exist(‘name’) determines if a le or variable exists having the name `name'
quit quit MATLAB
who lists all variables currently in memory
whos lists current variables and sizes
... continues a line (called ellipsis)

Tan Do (VGU) Introduction to MATLAB Lecture 1 29 / 34


MATLAB help

help function

The help function is the most basic way to determine the syntax and

behavior of a particular commands or functions in MATLAB.

For example, if we want to nd out about sine function, type

» help sin
in MATLAB Command window

Tan Do (VGU) Introduction to MATLAB Lecture 1 30 / 34


MATLAB help

help function

This will return

sin Sine of argument in radians.


sin(X) is the sine of the elements of X.

See also asin , sind .

Overloaded methods:
codistributed/sin
gpuArray/sin
sym/sin

Reference page in Help browser


doc sin

Tan Do (VGU) Introduction to MATLAB Lecture 1 31 / 34


MATLAB help

lookfor function

The help command is useful only when you know the exact MATLAB

names for the commands or functions.

So for example, if you type

» help sine
MATLAB returns

sine.m not found


In this case it is better to use the lookfor function which searches for

commands and functions on the basis of keywords.

Tan Do (VGU) Introduction to MATLAB Lecture 1 32 / 34


MATLAB help

» lookfor sine
xfourier - Square Wave from Sine Waves
acos - Inverse cosine, result in radians.
acosd - Inverse cosine, result in degrees.
acosh - Inverse hyperbolic cosine.
asin - Inverse sine, result in radians.
asind - Inverse sine, result in degrees.
asinh - Inverse hyperbolic sine.
cos - Cosine of argument in radians.
cosd - Cosine of argument in degrees.
cosh - Hyperbolic cosine.
sin - Sine of argument in radians.
sind - Sine of argument in degrees.
sinh - Hyperbolic sine.
...

Tan Do (VGU) Introduction to MATLAB Lecture 1 33 / 34


Save and load MATLAB session

Save and load sessions

To save the current session, type save in the Command window.

This will cause MATLAB to save the Workspace variables in a le

called matlab.mat.
You can also specify the le name for the session that you want to

save (instead of using the default le name matlab).


For this, type save filename.
To load a session saved under the name filename, type load
filename in the Command window.

Typing load only will cause MATLAB to load the default le

matlab.mat.

Tan Do (VGU) Introduction to MATLAB Lecture 1 34 / 34

You might also like