Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1R. Smith - University of St Thomas - Minnesota Today’s Class RecapRecap Schedule for rest of classesSchedule for rest of classes Matlab Functions.

Similar presentations


Presentation on theme: "March 2005 1R. Smith - University of St Thomas - Minnesota Today’s Class RecapRecap Schedule for rest of classesSchedule for rest of classes Matlab Functions."— Presentation transcript:

1 March 2005 1R. Smith - University of St Thomas - Minnesota Today’s Class RecapRecap Schedule for rest of classesSchedule for rest of classes Matlab Functions and Control StructuresMatlab Functions and Control Structures Matlab and C on the ExamMatlab and C on the Exam

2 March 2005 2R. Smith - University of St Thomas - Minnesota Recap Homework GradedHomework Graded Hard DrivesHard Drives File StructureFile Structure File NamesFile Names Matlab ScriptsMatlab Scripts

3 Schedule Today is the Last Lecture – rest will be labsToday is the Last Lecture – rest will be labs Final schedule is posted on the web pageFinal schedule is posted on the web page Extra lab time this Friday, same time as classExtra lab time this Friday, same time as class Assignment 15 due MondayAssignment 15 due Monday Projects Demonstrated next ThursdayProjects Demonstrated next Thursday Extra lab day Next FridayExtra lab day Next Friday Assignment 16 due Exam DayAssignment 16 due Exam Day March 2005 3R. Smith - University of St Thomas - Minnesota

4 March 2005 4R. 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

5 March 2005 5R. 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

6 Matlab and Search Paths You can rename built in functionsYou can rename built in functions –You can control which functions get used: builtins or cusom Look at ‘Search Paths’ box under ‘File’ menuLook at ‘Search Paths’ box under ‘File’ menu –Can control the order lookfor commandlookfor command –Tells you which version of a function you are using March 2005 6R. Smith - University of St Thomas - Minnesota

7 March 2005 7R. 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

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

9 March 2005 9R. 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

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

11 March 2005 11R. 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

12 FOR THE EXAM Any numerical or text-oriented function you can write in C, you should be able to write in Matlab!Any numerical or text-oriented function you can write in C, you should be able to write in Matlab! And Vice Versa!And Vice Versa! Text string examples:Text string examples: –copy, append, find or replace a character, compare strings Arithmetic examples:Arithmetic examples: –sum, min, max, average, sigma-based things March 2005 12R. Smith - University of St Thomas - Minnesota

13 March 2005 13R. 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

14 March 2005 14R. 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

15 March 2005 15R. 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

16 March 2005 16R. 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

17 March 2005 17R. 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 (!!!)

18 March 2005 18R. 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

19 March 2005 19R. 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

20 March 2005 20R. 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 RecapRecap Schedule for rest of classesSchedule for rest of classes Matlab Functions."

Similar presentations


Ads by Google