• 沒有找到結果。

Introduction to Matlab Programming with Applications

N/A
N/A
Protected

Academic year: 2022

Share "Introduction to Matlab Programming with Applications"

Copied!
41
0
0

加載中.... (立即查看全文)

全文

(1)

Introduction to Matlab Programming with Applications

Zheng-Liang Lu

Department of Computer Science and Information Engineering National Taiwan University

Matlab-251 Spring 2015

(2)

Class Information

Official Web page

http://www.csie.ntu.edu.tw/˜d00922011/matlab.html

Lecture notes will be uploadedright before class.

Note that the lecture notes are organized in English.

Contact information

d00922011@ntu.edu.tw

If you have any question, please contact me via email.

facebook

Zheng-Liang Lu 1 / 40

(3)

Prerequisites

No programming experience required.

It would be helpful if you have some programming experiences.

Linear Algebra is strongly recommended.

Matlab refers toMatrixlaboratory.

Linear Algebra is widely used in numerical analysis and quantitative methods.

Please be informed that you should consider other classes if you want to learn any advanced computer science courses.

I promise to keep everything

simple

in this class.

Zheng-Liang Lu 2 / 40

(4)

Grading

To pass the class, you need at least

60 pts

in the end of class.

Programming assignment (40 pts) Final project/exam (60 pts)

Please be noted that you preserve the right1 to refund before the 5th lecture.

1http://www.csie.ntu.edu.tw/train/?page=refund.html

Zheng-Liang Lu 3 / 40

(5)

Final Project

Topic: anything if you are interested in or working on.

Requirement: you should use Matlab as the main programming language.

Your grade depends on the following aspects:

Problem definition Innovation2 Completion Goal:

Solve a problem by formulating the problem, organizing into an algorithm, implementing a program by Matlab

Get familiar with programming concepts

Gain the experiences in order to learn more programming language by yourself.

2No copy-and-paste.

Zheng-Liang Lu 4 / 40

(6)

Final Exam

On-site programming 10 questions

Open everything

No communication with classmates and your friends

Zheng-Liang Lu 5 / 40

(7)

Who/How/Why

Zheng-Liang Lu 6 / 40

(8)

Articles about Learning Programming

5 REASONS YOU NEED TO LEARN TO PROGRAM The reason everyone should learn to code

Why Learn to Program?

為什麼要學各種程式語言?

Zheng-Liang Lu 7 / 40

(9)

Programs

A (computer) program is a sequence ofinstructions, written in an artificiallanguage, to perform aspecified taskwith a computer.

They are almost everywhere such that you can name it.

For example, Computer virus Games

Operating systems, such as Windows 8.1, UNIX/Linux, Android (family), OS X and iOS

Social networking, such as facebook, twitter, and so on The differences are the goal andfunctionality.

Zheng-Liang Lu 8 / 40

(10)

How and Where The Programs Run

The programs are activated from the disk into themain memory.

Now we call them theprocesses.

All the instructions with data are executed in the registersin CPUs.

The results are written back to the main memory and further stored into the disk after the processes are terminated.

Zheng-Liang Lu 9 / 40

(11)

Memory Hierarchy

3

3See Bryant, p. 14.

Zheng-Liang Lu 10 / 40

(12)

Programming Languages

Aprogramming languageis an artificial language designed to communicatewith a machine by instructions.

Programming languages → syntax+semantics Used to express algorithms

Used to control the behavior of a machine

Zheng-Liang Lu 11 / 40

(13)

How Many Programming Languages?

More than 100!!!

In historical perspective,

1st generation: machine code 2nd generation: assembly code

3rd generation: high-level programming languages Beyond 3rd generation

Matlab4 belongs to post-3rd-generation programming languages.

You can refer to TIOBE Programming Community Index.

41984

Zheng-Liang Lu 12 / 40

(14)

Zheng-Liang Lu 13 / 40

(15)

1st-Generation Programming Languages

A 1st-generation programming language is a machine-level programming language.

Machine-dependent

The instructions are made of binary numbers, represented by 1s and 0s.

There was originallyno compileror assembler to process the instructions.

Zheng-Liang Lu 14 / 40

(16)

Pros:

Most efficientfor machines Cons:

Hard to program for human Not portable

Still widely (and occasionally) used in programming lower level functions of the system, such as drivers, interfaces with firmware and hardware devices.

Aka machine code.

Zheng-Liang Lu 15 / 40

(17)

2nd-Generation Programming Languages

2nd-generation programming language is a generational way to categorizeassembly languages.

The code can be read and written by a programmer.

Yet, it is still machine-dependent.

To run on a computer, it must be converted into a machine readable form, a process called assembly.

More often find use in extremely intensive processing such as games, video editing, graphic manipulation/rendering.

Zheng-Liang Lu 16 / 40

(18)

3rd-Generation Programming Languages

A 3rd-generation language aims to be closer to the human domain.

Instructions operate at a higher, abstract level, closer to the human way of thinking.

Each individual instruction can be translated into a number of machine-level instruction via a specificcompiler.

Summary:

More friendly to understand, easier to learn Portable, machine-independent

Supportstructured programming Such as C5, C++6, and Java7

Aka high-level programming languages

51973

61983

71995

Zheng-Liang Lu 17 / 40

(19)

What Can A Program Do?

Aprogram is an implementation of an algorithmexpressed in a specificprogramming language.

Zheng-Liang Lu 18 / 40

(20)

Algorithm In A Nutshell

Analgorithm8 is a sequence of unambiguous instructions for solving a problem, that is, for obtaining a required output for any legitimate input in afinite amount of time.

Simply put, an algorithm is a procedure that solves a particular type of problems, such as a cookbook.

8SeeAlgorithmin Wikipedia.

Zheng-Liang Lu 19 / 40

(21)

Properties of Algorithms

Typical properties of any algorithm are summarized as follows:

Input

Statements: definiteness Instructions: effectiveness Number of steps: finiteness9 Output: correctness

Note that an algorithm isnotnecessarily expressed in a specific programming language.

Instead, one can express an algorithm in some human language using a programming language format.

9Alan Turing (1912–1954)

Zheng-Liang Lu 20 / 40

(22)

Example

Organize an algorithm that finds the greatest element in the input list.

Input: A (a list of n integers)

Output: x (the greatest element in A)

1 x <-- A(1)

2 for i <-- 1 ~ n

3 if A(i) > y

4 x <-- A(i)

5 end

6 end

7 return x

Zheng-Liang Lu 21 / 40

(23)

“Computers are good at following instructions, butnotat reading your mind.”

– Donald Knuth (1938-)

Zheng-Liang Lu 22 / 40

(24)

Matlab In A Nutshell

In my view, Matlab is a powerful and simple10 math software:

Powerful: programmable, fruitful toolboxes11 available

Simple: don’t need to worry about the machine, easy to draw a graph

The only thing you need to provide is ...

10Simple is not easy.

11http://www.mathworks.com/products/

Zheng-Liang Lu 23 / 40

(25)

Algorithms!

Zheng-Liang Lu 24 / 40

(26)

To Whom It May Concern,

If Matlab is your first programming language, Pros: easy to learn

Cons: may feel frustrated when learning other (lower) languages

If Matlab is not your first programming language,

Pros: extremely easy to learn(Warning: It may be boring.) Cons: cannot get rid of it because it is too easy...

The core concepts of programming are the same among various programming languages.

Zheng-Liang Lu 25 / 40

(27)

Conclusions

Program design ≈ algorithm + programming language Algorithm ← idea, steps

Programming language ← Chinese, English

In this class, we will learn Matlab and some algorithms which are the core of programming world.

Zheng-Liang Lu 26 / 40

(28)

Father of Matlab

Cleve Moler

Zheng-Liang Lu 27 / 40

(29)

Get started to

implement your idea with Matlab.

Zheng-Liang Lu 28 / 40

(30)

1 >> Lecture 1

2 >>

3 >> -- Overview of Matlab

4 >>

Zheng-Liang Lu 29 / 40

(31)

“Learning anew language is harmless. It gives younew ideas and insights.”

–Yukihiro Matsumoto (松本行弘)(1965–)

Zheng-Liang Lu 30 / 40

(32)

Contents

Environment Script editor Variable types

Scalar variable Numeric arrays

Character arrays (String) Cell arrays

Structure arrays

Built-in arithmetic functions

Zheng-Liang Lu 31 / 40

(33)

Working Environment

Zheng-Liang Lu 32 / 40

(34)

Command Window

You can give a command in the Command Window.

Let’s try a greeting, “Hello, Matlab.”

1 >> disp('Hello, Matlab.');

disp prints thestringas input in the parenthesis.

A string issingle-quoted in Matlab.

Convention in the slides:

Boxes are the listings for programs.

Words and sentences highlighted in red are important.

Words in blue are reserved words.

Bold words in black are functions.

Zheng-Liang Lu 33 / 40

(35)

Tips for Learning Matlab

Don’t be frustrated: 紅字是必經之路。

犯錯是為了下一次的成功作預演。

Matlabinterruptsyour program when asyntax erroroccurs.

lookfor is used to look for the commands related to the key word you give.

1 >> lookfor display

2 ...

3 disp - Display array.

4 display - Display array.

5 error - Display message and abort function.

6 warning - Display warning message; disable or ...

enable warning messages.

7 ...

help returns the usage of a command

Zheng-Liang Lu 34 / 40

(36)

1 >> help disp

2 ...

Help browser provides detail of commands with examples.

Google is your friend.

Zheng-Liang Lu 35 / 40

(37)

Script Editor

We often write a program in the script editor.

Save the program in .m file.

.m files are used as scriptsand functions.

A scriptis a complete program which contains the input data and can be executed directly.

A functionis a program which may need input data when being called in other programs. (why?)

Zheng-Liang Lu 36 / 40

(38)

Zheng-Liang Lu 37 / 40

(39)

helloworld.m

Let’s create a .m file with the following code lines:

1 % This is the comment section.

2 clear; % Clear all variables stored in Workspace.

3 clc; % Clear the screen.

4 % main program

5 disp('Hello, world.');

Note that

The lines which begins with % are regarded as the comments.

Comments are not executed.

clear is used to release the variables.

clc cleans the command window.

Zheng-Liang Lu 38 / 40

(40)

Save12 and press F513 to run the script.

Do not give a script file the same name as a variable. (Why?) Do not give a filename with blanks in the middle.

If the .m file is not at the current folder when you execute it, Matlab will ask:

1 Change folder

2 Add to Path Pool

You can run helloworld.m later just by calling helloworld in the command window.

1 >> helloworld

2

3 Hello, world.

12ctrl + s

13Autosave before execution.

Zheng-Liang Lu 39 / 40

(41)

Comments In Scripts

Anything following a % is seen as a comment.

Press ctrl + r to comment.

ctrl + t to de-comment.

To comment a block of lines, %{ · · · %}.

The first contiguous comment becomes the script’s help document.

1 >> help helloworld

2

3 This is the comment section.

Why comments?

Document is helpful for both the programmers and users.

Comment thoroughly to avoid wasting time later.

Zheng-Liang Lu 40 / 40

參考文獻

相關文件

Sometimes called integer linear programming (ILP), in which the objective function and the constraints (other than the integer constraints) are linear.. Note that integer programming

All variables defined as the result of entering statements in the command window, exist in the Matlab workspace. At the beginning of a Matlab session, the workspace

We are going to learn Alishan Forest Culture.. We hope that Alishan will be registered as a world

“Some ‘pictures’ are not really pictures, but rather are windows to Plato’s heaven.”,見 Philosophy of Mathematics — An Introduction to the World of Proofs and

Students are asked to collect information (including materials from books, pamphlet from Environmental Protection Department...etc.) of the possible effects of pollution on our

In this paper, we extended the entropy-like proximal algo- rithm proposed by Eggermont [12] for convex programming subject to nonnegative constraints and proposed a class of

Programming languages can be used to create programs that control the behavior of a. machine and/or to express algorithms precisely.” -

• A cell array is a data type with indexed data containers called cells, and each cell can contain any type of data. • Cell arrays commonly contain either lists of text