34
S.CS301: Lecture 05 (Autumn 2015/16) Page 1 System Design S.CS301

System Design S - uranchimeg.comuranchimeg.com/cs301/CS301_Lec05.pdf · S.CS301: Lecture 05 (Autumn 2015/16) Page 2 Өмнөх хичээлээр юу үзсэн бэ? o Матрицыг

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

S.CS301: Lecture 05

(Autumn 2015/16) Page 1

System Design

S.CS301

S.CS301: Lecture 05

(Autumn 2015/16) Page 2

Өмнөх хичээлээр юу үзсэн бэ?

o Матрицыг хөрвүүлэх

o Матрицаас матриц үүсгэх

o Матрицыг автоматаар үүсгэх

o Тусгай матрицууд

o Шугаман тэгшитгэлийг тооцоолох

o Массивын үйлдлүүд

o Матрицын үйлдлүүд

S.CS301: Lecture 05

(Autumn 2015/16) Page 3

Today we will learn

Introduction to programming

M Script file

M Function file

Creation of script file

Build function

Input arguments

Output arguments

Decision making commands

S.CS301: Lecture 05

(Autumn 2015/16) Page 4

Introduction to programming in MATLAB

So far in these lab sessions, all the commands were executed in the Command Window. The problem is that the commands entered in the Command Window cannot be saved and executed again for several times. Therefore, a different way of executing repeatedly commands with MATLAB is:

1. To create a file with a list of commands,

2. save the file, and

3. run the file.

S.CS301: Lecture 05

(Autumn 2015/16) Page 5

Introduction

If needed, corrections or changes can be made to the commands in the file. The files that are used for this purpose are called script files or scripts for short.

This section covers the following topics:

M-File Scripts

M-File Functions

S.CS301: Lecture 05

(Autumn 2015/16) Page 6

Scripts

Script files are program files with .m extension. In these files, you write series of commands, which you want to execute together. Scripts do not accept inputs and do not return any outputs. They operate on data in the workspace.

S.CS301: Lecture 05

(Autumn 2015/16) Page 7

Creating and Running Script File

To create scripts files, you need to use a text editor. You can open the MATLAB editor in two ways:

Using the command prompt

Using the IDE

If you are using the command prompt, type edit in the command prompt. This will open the editor. You can directly type edit and then the filename (with .m extension)

edit or edit <filename>

S.CS301: Lecture 05

(Autumn 2015/16) Page 8

Example 1

S.CS301: Lecture 05

(Autumn 2015/16) Page 9

DOS commands

The above command will create the file in default MATLAB directory. If you want to store all program files in a specific folder, then you will have to provide the entire path.

dir – show current folder

mkdir – create a folder rmdir

cd – show path of the current folder

chdir, cd ..

delete, edit, what – shows m files

S.CS301: Lecture 05

(Autumn 2015/16) Page 10

Example 2

NoOfStudents = 6000;

TeachingStaff = 150;

NonTeachingStaff = 20;

Total = NoOfStudents + TeachingStaff ...

+ NonTeachingStaff;

disp(Total);

What will show MATLAB?

6170

S.CS301: Lecture 05

(Autumn 2015/16) Page 11

Script side effects

All variables created in a script file are added to the workspace. This may have undesirable effcts, because:

Variables already existing in the workspace may be overwritten.

The execution of the script can be affected by the state variables in the workspace.

As a result, because scripts have some undesirable side-effects, it is better to code any complicated applications using rather function M-file.

S.CS301: Lecture 05

(Autumn 2015/16) Page 12

M-File functions

As mentioned earlier, functions are programs (or routines) that accept input arguments and return output arguments. Each M-file function (or function or M-file for short) has its own area of workspace, separated from the MATLAB base workspace.

S.CS301: Lecture 05

(Autumn 2015/16) Page 13

Anatomy of a M-File function

This simple function shows the basic parts of an M-file

The first line of a function M-file starts with the keyword function. It gives the function

Name and order of arguments . In the case of function factorial, there are up to one output argument and one input argument.

S.CS301: Lecture 05

(Autumn 2015/16) Page 14

Function body

S.CS301: Lecture 05

(Autumn 2015/16) Page 15

Function and script

Both functions and scripts can have all of these parts, except for the function definition line which applies to function only.

In addition, it is important to note that function name must begin with a letter, and must be no longer than the maximum of 63 characters.

S.CS301: Lecture 05

(Autumn 2015/16) Page 16

Differences between scripts and functions

S.CS301: Lecture 05

(Autumn 2015/16) Page 17

Input and output arguments

As mentioned above, the input arguments are listed inside parentheses following the function name. The output arguments are listed inside the brackets on the left side. They are used to transfer the output from the function file. The general form looks like this

Function file can have none, one, or several output arguments.

S.CS301: Lecture 05

(Autumn 2015/16) Page 18

Example of input and output arguments

S.CS301: Lecture 05

(Autumn 2015/16) Page 19

Input to a script file

When a script file is executed, the variables that are used in the calculations within the file must have assigned values. The assignment of a value to a variable can be done in three ways.

1.The variable is defined in the script file.

2.The variable is defined in the command prompt.

3.The variable is entered when the script is executed

S.CS301: Lecture 05

(Autumn 2015/16) Page 20

3th way

When the file is executed, the user is prompted to assign a value to the variable in the command prompt. This is done by using the input command.

game1 = input('Enter the points scored in the first game ');

game2 = input('Enter the points scored in the second game ');

game3 = input('Enter the points scored in the third game ');

average = (game1+game2+game3)/3

S.CS301: Lecture 05

(Autumn 2015/16) Page 21

Output commands

As discussed before, MATLAB automatically generates a display when commands are executed. In addition to this automatic display, MATLAB has several commands that can be used to generate displays or outputs. Two commands that are frequently used to generate output are: disp and fprintf.

S.CS301: Lecture 05

(Autumn 2015/16) Page 22

Control flow and operators

MATLAB is also a programming language. Like other computer programming languages, MATLAB has some decision making structures for control of command execution. These decision making or control flow structures include for loops, while loops, and if-else-end constructions. Control flow structures are often used in script M-files and function M-files.

S.CS301: Lecture 05

(Autumn 2015/16) Page 23

The ``if...end'‘ structure

If the expression evaluates to true, then the block of code inside the if statement will be executed. If the expression evaluates to false, then the first set of code after the end statement will be executed.

S.CS301: Lecture 05

(Autumn 2015/16) Page 24

Example 3

S.CS301: Lecture 05

(Autumn 2015/16) Page 25

The ``if...else…end'‘ structure

If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.

S.CS301: Lecture 05

(Autumn 2015/16) Page 26

Example 4

S.CS301: Lecture 05

(Autumn 2015/16) Page 27

The if...elseif...elseif...else...end Statements

An if statement can be followed by one (or more) optional elseif... and an else statement, which is very useful to test various conditions.

When using if... elseif...else statements, there are few points to keep in mind:

An if can have zero or one else's and it must come after any elseif's.

An if can have zero to many elseif's and they must come before the else.

Once an else if succeeds, none of the remaining elseif's or else's will be tested.

S.CS301: Lecture 05

(Autumn 2015/16) Page 28

Example 5

S.CS301: Lecture 05

(Autumn 2015/16) Page 29

The Nested if Statements

It is always legal in MATLAB to nest if-else statements which means you can use one if or elseif statement inside another if or elseif statement(s).

S.CS301: Lecture 05

(Autumn 2015/16) Page 30

Example 6

S.CS301: Lecture 05

(Autumn 2015/16) Page 31

Summary of if statement

S.CS301: Lecture 05

(Autumn 2015/16) Page 32

Solving quadratic formula

S.CS301: Lecture 05

(Autumn 2015/16) Page 33

What we learned today?

Difference between M script and M function files

To create script and function files

Conditional statements for programming

If end

If else end

If elseif else end

Solving quadratic formula

S.CS301: Lecture 05

(Autumn 2015/16) Page 34

End of

Any questions?