资源描述:
1 沈志忠 機械與機電工程學系 Programming in MatlabProgramming in Matlab Chapter 3 – Branching Statements and Program Design 2 Programming in MatlabProgramming in Matlab al program design procedure -- top-down design Algorithm development tool -- pseudocode 2 3 Programming in MatlabProgramming in Matlab 3.1 Top-Down Design Techniques 4 Programming in MatlabProgramming in Matlab Program Design ”Programming is easy. It’s knowing what to program that’s hard.” Program design On the Fly Approach sit down at a keyboard and start programming without ”wasting” a lot of time thinking about the progblem. Suitable for small problems Top-Down Design Approach Start with a large task and break it down into smaller, more easily understandable pieces subtasks Each subtask may be subdivided into smaller pieces if necessary. Each piece can be coded and tested indepently. The subtasks will not be combined to the complete task untill the subtasks has been verified to work properly by itself. 3 5 Programming in MatlabProgramming in Matlab Problem decomposition Stepwise refinement pseudocode Replace pseudocode with the corresponding MATLAB statements. Verify that it works for all legal data sets and under every possible circumstance. Write a program to solve a system of simultaneous linear equations having real coefff.’s and with up to 20 equations the data in a pre- existing order. Write the answers in required at 6 Programming in MatlabProgramming in Matlab 4 7 Programming in MatlabProgramming in Matlab Time Spent in a Large Project 1/3 Planning what to do step 1 3 1/6 Writing the program step 4 1/2 Testing and debugging the program step 5 Follow the steps of the program design process to produce reliable, understandable MATLAB programs. Good programming practices will reduce the number of bugs in the program and will make the ones that do creep easier to find. Follow the steps of the program design process to produce reliable, understandable MATLAB programs. Good programming practices will reduce the number of bugs in the program and will make the ones that do creep easier to find. 8 Programming in MatlabProgramming in Matlab 3.2 Use of Pseudocode A description of the algorithm in a standard Easy for both you and other people to understand Aid you in turning your concept into MATLAB code The standard s are called constructs or structures Structured code Structured program 5 9 Programming in MatlabProgramming in Matlab Pseudocode A hybrid mix of MATLAB and English Each line should describe its idea in plain, easily understandable English. Useful for developing algorithm flexible and easy to modify help organize your thoughts before converting them into MATLAB code 10 Programming in MatlabProgramming in Matlab Prompt the user for the temperature. temp_f Enter the temperature in degrees Fahrenheit ; Convert to kelvins. temp_k 5/9 * temp_f - 32 273.15; Write out the result. fprintf6.2f degrees Fahrenheit 6.2f kelvins.\n, ... temp_f,temp_k; Example 2.3 Prompt user to enter temperature in degrees Fahrenheit Read temperature in degrees Fahrenheit temp_f Temp_k in kelvins 5/9*temp_f – 32 273.15 Write temperature in kelvins 6 11 Programming in MatlabProgramming in Matlab 3.3 The Logical Data Type Two types of operations producing either true 1 or false 0 in MATLAB relational operators and logic operators. It is legal in MATLAB to mix numerical and logical data in expressions. If a logical value is used where a numerical value is expected, true values are converted to 1 and false values are converted to 0. 12 Programming in MatlabProgramming in Matlab Relational Operators General of a logical operator a1op a2 7 13 Programming in MatlabProgramming in Matlab Relational Operators a b ans 1 0 0 1 Relational operators can only compare two strings of equal lengths. Relational operators are uated after all arithmetic operators have been uated. −− − 12 20 , 12 01 ba a b ans 1 0 1 1 0, 12 01 − ba 14 Programming in MatlabProgramming in Matlab A Caution about the and Operators Be cautious about testing for equality with numeric values, since roundoff errors can cause two variables that should be equal to fail a test for a test for equality. Instead, test to see if the variables are nearly equal within the roundoff error to be expected on the computer you are working with. Be cautious about testing for equality with numeric values, since roundoff errors can cause two variables that should be equal to fail a test for a test for equality. Instead, test to see if the variables are nearly equal within the roundoff error to be expected on the computer you are working with. a 0; b sinpi b 1.2246e-016 a b ans 0 absa - b 10.0 What if b 0 x b 0 value2 0; value3 -10; Logical ExpressionResult avalue10 bvalre1 | value21 cvalue1 elseif grade 86.0 dispThe grade is B.; elseif grade 76.0 dispThe grade is C.; elseif grade 66.0 dispThe grade is D.; else dispThe grade is F,; end b Nested if constructs if grade 95.0 dispThe grade is A.; else if grade 86.0 dispThe grade is B.; else if grade 76.0 dispThe grade is C.; else if grade 66.0 dispThe grade is D.; else dispThe grade is F.; end end end end For branches in which there are many mutually exclusive options, use a single if construct with multiple elseif clauses in preference to nested if constructs. For branches in which there are many mutually exclusive options, use a single if construct with multiple elseif clauses in preference to nested if constructs. 28 Programming in MatlabProgramming in Matlab The switch Construct switch switch_expr case case_expr_1 Statement 1 Statement 2 case case_expr_2 Statement 1 Statement 2 otherwise Statement 1 Statement 2 end Block 1 Block 2 Block n 15 29 Programming in MatlabProgramming in Matlab The switch Construct switch switch_expr case {case_expr_1, case_expr_2, case_expr_3}, Statement 1 Statement 2 case case_expr_n-1 Statement 1 Statement 2 otherwise Statement 1 Statement 2 end Block 1 Block n-1 Block n If the switch expression matches more than one case expression, only the first one of them will be cuted. If the switch expression matches more than one case expression, only the first one of them will be cuted. 30 Programming in MatlabProgramming in Matlab Example switch value case {1, 3, 5, 7, 9}, disp‘The value is odd.’ case {2, 4, 6, 8, 10} disp‘The value is even.’ otherwise disp‘The value is out of range.’ end 16 31 Programming in MatlabProgramming in Matlab The try/catch Construct Designed to trap errors. Can handle errors within the program without causing the program to stop. try Statement 1 Statement 2 catch Statement 1 Statement 2 end Try Block Catch Block 32 Programming in MatlabProgramming in Matlab A try/catch Example 17 33 Programming in MatlabProgramming in Matlab 3.5 Additional Plotting Features 34 Programming in MatlabProgramming in Matlab Enchaced Control of Plots LineWidth – in points MarkerEdgeColor – color of the marker or the edge color for filled markers MarkerFaceColor – color of the face of filled markers MarkerSize – size of the marker in points Example plotx,y,PropertyName,value,... 18 35 Programming in MatlabProgramming in Matlab Enchanced Control of Text Strings Special Greek and mathematical symbols can be used in text strings TEX language. \bf – Boldface \rm – Restore normal font. \fontname{fontname} – Specify the fontname to use. \fontsize{fontsize} – Specify the font size. _{xxx} – Subscripts {xxx} – Superscripts 36 Programming in MatlabProgramming in Matlab
展开阅读全文