Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Programming with MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem

Similar presentations


Presentation on theme: "1 Programming with MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem"— Presentation transcript:

1 1 Programming with MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem http://www.cpe.ku.ac.th/~anan anan@cpe.ku.ac.th

2 2 Operation modes in MATLAB Interactive mode (calculator mode) Script file (computer program) >>a+b ans = [ 3 15 ] >>a+b ans = [ 3 15 ] %Example of program %Program Test1.m a = [ 1 10]; b = [ 2 5]; c = a + b %Example of program %Program Test1.m a = [ 1 10]; b = [ 2 5]; c = a + b

3 3 Some key words in programming Algorithm Structured Programming Top-down design (programming strategy) Documentation Pseudocode

4 4 Algorithms Algorithm Ordered sequence of instructions Perform some task in a finite time 3 types of Algorithm Sequential operation Conditional operation Iterative (loop) operation

5 5 Structured programming Compose of hierarchy of modules (functions) Clear program flow Sequential, Conditional, Iterative No unnecessary branches No “ goto ” in BASIC

6 6 Advantage of structured program Easy to write and understand Concentrate on the overall problem first Solve problem in divide and conquer style Easy to debug Easy to modify Reusable Modules (functions) Teamwork environment Each person takes care some certain modules

7 7 Top-down design (Programming Strategy) Don ’ t rush to key in the computer State the problem concisely (Think carefully) Specify “ input ” and “ output ” Solve the problem “ by hand ” Plan your program (module, sequence, etc.) Test by hand if possible “ Writing and Running ” program “ Testing and Debug ” program (sample input) Documentation Maintenance

8 8 Developing structured programming Charts Structure chart Flowchart

9 9 Structure Chart Initial state of top-down design Describe the connection between parts (modules) No (calculation) details

10 10 Structure chart example Writing a game “ Tic-Tac-Toe ” Main Program Player Input (User interface) Player Input (User interface) Game Status (Board display) Game Status (Board display) Strategy Module

11 11 Start Flowchart Show sequence and branches End Read width,length If total 0 Write total No Yes Total := width + length

12 12 Documentation Very Important for understand/debug/modify Good documentation Proper selection of variable names Use of comments within the program Use of structure charts Use of flowcharts Use verbal description (pseudocode)

13 13 Pseudocode To describe program in short and understandable Imitation code (not a real code) Outlining a program (before detailed coding) Use natural language Mathematical expression No detailed syntax

14 14 Pseudocode Example 1.Enter x and y coordinates 2.Compute the z 3.Check for valid output (>= 0) 4.Display the result 5.Stop z = √ (x 2 +y 2 ) To calculate

15 15 Exercise Writing a “ เป่า ยิง ฉุบ ” program Structure Chart Flowchart or Pseducode


Download ppt "1 Programming with MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem"

Similar presentations


Ads by Google