0% found this document useful (0 votes)
18 views37 pages

MATLAB MAS109 Week2

The document is a MATLAB basics guide for a course at KAIST, covering fundamental topics such as variables, matrices, and vectors. It outlines the structure of the course, including assignments and grading, and provides an introduction to MATLAB's functionalities and command usage. The document also includes examples of basic operations and matrix manipulations in MATLAB.

Uploaded by

seungbin6778
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)
18 views37 pages

MATLAB MAS109 Week2

The document is a MATLAB basics guide for a course at KAIST, covering fundamental topics such as variables, matrices, and vectors. It outlines the structure of the course, including assignments and grading, and provides an introduction to MATLAB's functionalities and command usage. The document also includes examples of basic operations and matrix manipulations in MATLAB.

Uploaded by

seungbin6778
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/ 37

Basics of MATLAB

Jiseok Chae

Department of Mathematical Sciences


KAIST

MAS109 Week 2

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 1 / 37


Contents

1 Hello MATLAB!

2 Variables

3 Matrices and Vectors

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 2 / 37


Welcome to the MATLAB session!

Like today, every Friday with a recitation class, a video will be uploaded.

Brief explanations about the functionalities of MATLAB will be discussed


in the MATLAB session.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 3 / 37


There will be about 6 MATLAB assignments, roughly one every two weeks.

We use MATLAB Grader for assignments.

You should have already received a invitation email for MATLAB Grader.

15% of your final grade is based on recitation class activities and


MATLAB assignments.

⋆ The first assignment is out.


Read the descriptions carefully, and follow the instructions.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 4 / 37


About me:
Jiseok Chae (채지석)
[email protected]

If you have questions about the MATLAB materials and/or assignments,


do not hesitate to contact me.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 5 / 37


Hello MATLAB!

Contents

1 Hello MATLAB!

2 Variables

3 Matrices and Vectors

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 6 / 37


Hello MATLAB!

MATLAB is a software package with an interactive environment.

MATLAB is specialized for matrix computations, but also a powerful tool


for function and data plotting, data analysis, mathematical modeling, etc.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 7 / 37


Hello MATLAB!

If you start MATLAB you will see a window that looks something like this:

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 8 / 37


Hello MATLAB!

Each part has the following name:

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 9 / 37


Hello MATLAB!

Commands are expressions you enter so that MATLAB evaluates it to you.

If you first launch MATLAB, the command window will look like:

>>

The symbol >> indicates that the MATLAB is ready to operate.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 10 / 37


Hello MATLAB!

You can use MATLAB as a highly sophisticated calculator.

Let us type in the following expression...

>> 2 + 3

...and press the Enter key in your keyboard.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 11 / 37


Hello MATLAB!

You will see the following result.

>> 2 + 3

ans =

The command window contains both your input and MATLAB’s output.
In the slides we will distinguish them by highlighting the inputs by color.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 12 / 37


Hello MATLAB!

Other basic arithmetic operations can be done similarily.


operation input output
Addition 2 + 3 5
Subtraction 9 - 1 8
Multiplication 6 * 5 30
Division 7 / 4 1.7500
Exponentiation 8 ^ 2 64

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 13 / 37


Hello MATLAB!

There are several predefined functions and constants, not limited to below:

>> sin(pi) + sqrt(2)

ans =

1.4142

>> log(exp(1))

ans =

pi is the constant π, sqrt is the square root, log is the natural log, and
exp is the base-e exponentiation.
Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 14 / 37
Hello MATLAB!

You can use the percent symbol (%) to make a comment.

Anything after the symbol % in that line will be ignored by MATLAB.

A comment is colored green in MATLAB by default, so we will also do so.

>> 9 * 7 % / 2 ; The division will not be executed !

ans =

63

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 15 / 37


Variables

Contents

1 Hello MATLAB!

2 Variables

3 Matrices and Vectors

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 16 / 37


Variables

Variables are like named boxes that contain values.

To declare a variable with the name x which contains the value 5, you
simply do the following.

>> x = 5

x =

Variable names must start with an alphabet, and should be consisted of


alphabets, numbers, and underbars.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 17 / 37


Variables

The simple rule is that MATLAB evaulates the expression on the right
hand side, and stores it in the left hand side.

>> x = 5

x =

>> x = x + 17

x =

22

So = does not really mean an equality; it is rather a substitution.


Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 18 / 37
Variables

A semicolon (;) indicates the end of a command, and suppresses the


printing of the output.

You can define variables with values derived from different variables.
>> hello KAIST = 142; welcome = 857;
>> neopjuk = hello KAIST + welcome

neopjuk =

999

>> welcome = 1;
>> neopjuk

neopjuk =

999

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 19 / 37


Variables

Actually, ans is also a variable, automatically generated by MATLAB.

>> 40 + 1

ans =

41

>> ans * 271

ans =

11111

And yes, 41 × 271 is equal to 11111.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 20 / 37


Matrices and Vectors

Contents

1 Hello MATLAB!

2 Variables

3 Matrices and Vectors

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 21 / 37


Matrices and Vectors

This is how you declare a matrix in MATLAB:

>> A = [1, 9, 7, 1; 2, 0, 2, 4]

A =

1 9 7 1
2 0 2 4

 
1 9 7 1
Now A contains the 2 × 4 matrix .
2 0 2 4

A comma (,) separates entries. A semicolon (;) separates rows.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 22 / 37


Matrices and Vectors

The same can be done as the following, in order to enhance the readability
of your input.

>> A = [1, 9, 7, 1;
2, 0, 2, 4]

A =

1 9 7 1
2 0 2 4

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 23 / 37


Matrices and Vectors

Row vectors are matrices with only 1 row.


Column vectors are matrices with only 1 column.

>> a = [2, 5];


>> b = [1; 0; 9];

 
  1
a contains the row vector 2 5 . b contains the column vector 0.
9

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 24 / 37


Matrices and Vectors

There are functions that help you generate special matrices.


zeros(n) makes an n × n zero matrix.
zeros(n, m) makes an n × m zero matrix.
ones(n) makes an n × n matrix with all entries 1.
ones(n, m) makes an n × m matrix with all entries 1.
eye(n) makes an n × n identity matrix.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 25 / 37


Matrices and Vectors

You can add, subtract, and multiply vectors and matrices with compatible
sizes using +, -, and *.

>> A = [1, -3, 2;


4, 0, -5];
>> x = [3; 1; 2];
>> A * x

ans =

4
2

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 26 / 37


Matrices and Vectors

If you add the dot (.) in front of *, MATLAB computes the elementwise
product.

>> A = [1, 2; 3, 4]; B = [5, 6; 7, 8];


>> A .* B

ans =

5 12
21 32

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 27 / 37


Matrices and Vectors

A dot (.) in general indicates an elementwise operation.

>> A = [1, 2; 3, 4]; B = [5, 6; 7, 8];


>> A ./ B

ans =

0.2000 0.3333
0.4286 0.5000

>> A .^ 2

ans =

1 4
9 16

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 28 / 37


Matrices and Vectors

Some functions, when applied to matrix, is applied elementwise.

>> A = [1, 2; 3, 4];


>> sqrt(A)

ans =

1.0000 1.4142
1.7321 2.0000

Question : what would be the result of sin(A)? exp(A)?

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 29 / 37


Matrices and Vectors

To get the transpose, use an apostrophe (’).

>> A = [1, 2; 3, 4];


>> A’

ans =

1 3
2 4

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 30 / 37


Matrices and Vectors

Let’s see more examples of matrix operations. Let A be a matrix, then


inv(A) computes the inverse of A.
A^k computes the k th power of A.
A^0 will be the identity matrix.
A^-1 is the same as inv(A).
rref(A) computes the reduced row echelon form of A.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 31 / 37


Matrices and Vectors

You can access to the entries using parentheses (()).

>> a = [2, 5]; b = [1; 0; 9];


>> a(1)

ans =

>> b(3)

ans =

a(1) is the first entry of a, hence 2. b(3) is the third entry of b, hence 9.
Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 32 / 37
Matrices and Vectors

You can access to the entries using parentheses (()).

>> A = [1, 2, 3;
4, 5, 6;
7, 8, 9];

>> A(3, 2)

ans =

A(3, 2) means, select from A only the 3rd row and the 2nd column.
Therefore, A(3, 2) means the (3, 2) entry of A, which is A32 = 8.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 33 / 37


Matrices and Vectors

Instead of specifying the index, a colon (:) can be used to say “select all”.

>> A = [1, 2, 3;
4, 5, 6;
7, 8, 9];

>> A(2, :)

ans =

4 5 6

A(2, :) means, select from A only the 2nd row and all the columns.
Therefore, A(2, :) is the 2nd row vector of A.

Question: what would be the result of A(:, 1)?


Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 34 / 37
Matrices and Vectors

More complex selections can be made.

>> A = [1, 2, 3;
4, 5, 6;
7, 8, 9];

>> A([1, 3], 2)

ans =

2
8

A([1, 3], 2) means, select from A only the 1st and 3rd row, and the
2nd column.

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 35 / 37


Matrices and Vectors

A colon (:) can be used to denote a consecutive range of indices.

>> A = [1, 2, 3, 4;
5, 6, 7, 8;
9, 10, 11, 12];

>> A(3, 2:4)

ans =

10 11 12

This is because a:b is automatically translated to the (b − a + 1)-dim’l


row vector [a a + 1 . . . b].

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 36 / 37


Matrices and Vectors

Thank you!

Jiseok Chae (KAIST) Basics of MATLAB MAS109 Week 2 37 / 37

You might also like