Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1R. Smith - University of St Thomas - Minnesota Today’s Class Office hours canceled todayOffice hours canceled today RecapRecap Matlab ProgrammingMatlab.

Similar presentations


Presentation on theme: "March 2005 1R. Smith - University of St Thomas - Minnesota Today’s Class Office hours canceled todayOffice hours canceled today RecapRecap Matlab ProgrammingMatlab."— Presentation transcript:

1 March 2005 1R. Smith - University of St Thomas - Minnesota Today’s Class Office hours canceled todayOffice hours canceled today RecapRecap Matlab ProgrammingMatlab Programming –Functions –IF/Then –Loops Assignment 16Assignment 16 Why C? Why Matlab?Why C? Why Matlab? Lab TimeLab Time

2 March 2005 2R. Smith - University of St Thomas - Minnesota Recap of Yesterday Course ScheduleCourse Schedule –Extra credit Quiz If you have questions about answers on the last examIf you have questions about answers on the last exam –ASK ME DURING THIS LAB PERIOD –Review on Wednesday –Demos on Thursday –Last Matlab homework due Exam Week Do it BEFORE the exam as part of your studying!Do it BEFORE the exam as part of your studying! Work on labs/projectsWork on labs/projects

3 March 2005 3R. Smith - University of St Thomas - Minnesota Matlab Functions Syntax:Syntax: function answers = name ( arguments ) % comment on how to use it statements ; File name.m must match function nameFile name.m must match function name List of zero or more arguments separated by commasList of zero or more arguments separated by commas May define answers as a list of valuesMay define answers as a list of values –function [min, max] = minmax (vect) –could find both min and max values of the vector vect Must set answers to legitimate valuesMust set answers to legitimate values Executes statements till the end of the function fileExecutes statements till the end of the function file

4 March 2005 4R. Smith - University of St Thomas - Minnesota Function example Add some numbersAdd some numbers Return quotient and remainderReturn quotient and remainder The ‘help’ command for functionsThe ‘help’ command for functions

5 March 2005 5R. Smith - University of St Thomas - Minnesota Matlab IF/Else Typical Syntax:Typical Syntax: if ( expression1 ) statement1 ; elseif ( expression2 ) statement2 ; else statement3 ; end ALWAYS needs the end statement to mark the endALWAYS needs the end statement to mark the end No curly bracesNo curly braces

6 March 2005 6R. Smith - University of St Thomas - Minnesota IF Function Example True/false tests like even/oddTrue/false tests like even/odd Recursive FibonacciRecursive Fibonacci

7 March 2005 7R. Smith - University of St Thomas - Minnesota Matlab FOR Syntax:Syntax: for variable = vector statements ; end Goes through vector, element by elementGoes through vector, element by element –Assigns an element to variable –The vector can be a colon statement, like 1:length(x) Runs the statements until end, then repeatsRuns the statements until end, then repeats ALWAYS needs the end statement to mark the endALWAYS needs the end statement to mark the end No curly bracesNo curly braces

8 March 2005 8R. Smith - University of St Thomas - Minnesota For loop examples Sum of a vectorSum of a vector More efficient FibonacciMore efficient Fibonacci

9 March 2005 9R. Smith - University of St Thomas - Minnesota Matlab WHILE Syntax:Syntax: while expression statements ; end Tests expression for non-zero result, loops if soTests expression for non-zero result, loops if so Runs the statements until end, then repeatsRuns the statements until end, then repeats ALWAYS needs the end statement to mark the endALWAYS needs the end statement to mark the end No curly bracesNo curly braces

10 March 2005 10R. Smith - University of St Thomas - Minnesota Assignment 16 Do Kaplan's Exercises 8.5, and 8.6, on page 178. E-mail the functions to the instructor.Do Kaplan's Exercises 8.5, and 8.6, on page 178. E-mail the functions to the instructor. –Be sure to include COMMENTS FOR THE EXAMFOR THE EXAM –Any numerical or text-oriented function you can write in C, you should be able to write in Matlab! –Text string examples: copy, append, find a character, compare stringscopy, append, find a character, compare strings –Arithmetic examples: sum, min, max, average, sigma-based thingssum, min, max, average, sigma-based things

11 March 2005 11R. Smith - University of St Thomas - Minnesota Why C? Why Matlab? The exam will ask you to explain why it’s better to use one or the other for various problemsThe exam will ask you to explain why it’s better to use one or the other for various problems

12 March 2005 12R. Smith - University of St Thomas - Minnesota How is C different from Matlab? C is compiled; Matlab is interpretedC is compiled; Matlab is interpreted –A special program, the compiler, converts the C program from ASCII source code into binary coded computer instructions –In Matlab, the computer has to do that conversion whenever we type in an expression – we say Matlab is interpreted C Errors arise on two different occasionsC Errors arise on two different occasions –Syntax errors and some usage errors are found in compilation In a perfect world, this finds all possible errorsIn a perfect world, this finds all possible errors In practice, lots of errors slip through (a trade-off)In practice, lots of errors slip through (a trade-off) –Other errors arise when the program actually ‘runs’ Matlab errors only arise during executionMatlab errors only arise during execution –Errors may involve either syntax, logic, or both

13 March 2005 13R. Smith - University of St Thomas - Minnesota Why is C popular? It is portableIt is portable –Easy to modify to create programs for another computer –The compiler itself is easily ‘hosted’ on other computers A fairly complete programming languageA fairly complete programming language –A rich set of data types and operations on them –Standard control structures, allows structured programming –Only missing object technology “Close to the machine”“Close to the machine” –Easy to write highly technical programs for operating systems –Mechanisms map easily to the machine level = efficient Highly efficient – optimizing compilersHighly efficient – optimizing compilers

14 March 2005 14R. Smith - University of St Thomas - Minnesota Problems with C C programs can be hard to readC programs can be hard to read –Obfuscated C Contest Data types are not enforcedData types are not enforced –Compiler doesn’t catch every error it could Some operations have ‘side effects’Some operations have ‘side effects’ –Expressions don’t evaluate consistently to the same answer –In this class, we note and avoid operations with side effects Compared to MatlabCompared to Matlab –Requires more steps, more typing to yield a simple result –Unsophisticated output: no graphing –Rigid handling of input text – programmer must write code to interpret and process typed inputs

15 March 2005 15R. Smith - University of St Thomas - Minnesota Who uses C today? Operating systemsOperating systems –Windows is mostly written in C –Linux/Unix is mostly written in C – that’s where C started –Macintosh software is mostly written in C –C++ is an object-oriented version that sees lots of use CompilersCompilers –C and C++ compilers are written in C Application packages, like ExcelApplication packages, like Excel –Many are being written in Java these days – GUIs are easier Embedded ApplicationsEmbedded Applications –Gas pumps, USB hubs, cell phones, lab equipment (!!!)

16 March 2005 16R. Smith - University of St Thomas - Minnesota What about Matlab? It’s a proprietary commercial packageIt’s a proprietary commercial package –You have to buy it to use it, or borrow it from your school –C is available for free It is more flexible in handling numeric dataIt is more flexible in handling numeric data –Arrays can resize themselves to fit the data –Easy to add or remove elements or even dimensions Built in I/O to convert files to usable dataBuilt in I/O to convert files to usable data –C requires a lot of special code to read in data –Matlab converts files to vectors/matrices, all ready to use

17 March 2005 17R. Smith - University of St Thomas - Minnesota Choosing between C and Matlab Matlab is Better for…Matlab is Better for… Vector and matrix mathVector and matrix math –When sizes aren’t known ahead of time Graphing – it’s built inGraphing – it’s built in Processing standard file formats – built inProcessing standard file formats – built in –Images and spreadsheets C is Better for… Group programming –Established ways of sharing the work Tight quarters –When the computer is too small to run Matlab Little or no OS support –Computer doesn’t have graphics, not very interactive Access to I/O hardware –Easy to manipulate I/O control registers from C

18 March 2005 18R. Smith - University of St Thomas - Minnesota Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by- sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "March 2005 1R. Smith - University of St Thomas - Minnesota Today’s Class Office hours canceled todayOffice hours canceled today RecapRecap Matlab ProgrammingMatlab."

Similar presentations


Ads by Google