Download Matlab Scripts and Functions Reading Review and more Lecture notes Programming for Engineers in PDF only on Docsity! 6/21/2018 1 C/C++ Programming for Engineers: Matlab Scripts and Functions John T. Bell Department of Computer Science University of Illinois, Chicago 2 Reading Review Which of the following would multiply the cosine of X times the common logarithm of X, when X is given in degrees? A. cos( X ) * log( X ) B. cos( X ) * log10( X ) C. cosd( X ) * log( X ) D. cosd( X ) * log10( X ) E. cos( X degrees ) * commonLog( X degrees ) 6/21/2018 2 3 Matlab Script Files • Any command that can be run in Matlab can be placed in an ordinary text file ( with a .m extension ) and run as a Matlab script. • The easiest way to do this is to use Matlab’s built-in editor and drag commands from command history into the script, & then edit. • Run the script by typing the script name, or by pressing the “run” button in the script editor. 4 Special Characters in Scripts • The % percent sign is used to create comments, from the % to the end of the line. – %{ Large block comments in braces and % %} • The ; semi-colon at the end of the line suppresses the display of the command. • Three dots, … , at the end of a line … are used to continue … long lines onto the next line. 6/21/2018 5 9 Formatted Output in Matlab • The fprintf( ) command prints strings: – fprintf( ‘This program plots sine waves.’ ); • New lines start (only) when \n is printed: – fprintf( ‘This will print’ ); – fprintf( ‘ all on one line.\n’ ); • This will print all on one line. – fprintf( ‘This will print\n on\n three lines.\n’ ); • This will print • on • three lines. 10 Format Specifiers in fprintf( ) • Whenever a % is encountered in the output string, fprintf( ) prints a data value: – fprintf( ‘The result is %f mpg.\n’, mileage ); 6/21/2018 6 11 Format Specifiers in fprintf( ) Operator Prints Description %f fixed-point f is for fixed-point. %d integer d is for decimal integer. The use of 'd' is due to historical reasons, and is slightly misleading because a decimal number could have a fractional part. In MATLAB, %d means decimal integer which does not have a fraction. %i integer i is for integer. This operator is identical to %d when formatting output. %c character c is for character. %s string s is for string. 12 Special Characters Special character Description \n Prints a new line. '' Two single quotation marks print a single quotation mark. Note that a single quotation mark alone would instead indicate the end of the format string. %% Two percent characters print one percent character. Note that a single % character would instead indicate the start of a formatting operator like %f. \\ Two backslash characters print one backslash character. Note that a single backslash character would instead indicate the start of a special character sequence like \n. \t Prints a tab. 6/21/2018 7 13 Floating Point Formats Operator Description %f Fixed-point notation, 109.42 %e Scientific notation with lowercase e, as in 6.02e+23, 3.141593e+00 %g Either %f or %e, whatever is shorter %E Scientific notation with capital E, as in 6.02E+23, 3.141593E+00 %G Either %G or %E, whichever is shorter, 6.02E+23, 3.141593 Table 3.6.1: Floating-point number formatting operators. 14 Preview What happens if a Matlab workspace contains a variable X, and then a script is run that creates a new variable X and gives it a value? A. There will now be two variables named X. B. The workspace X will be wiped out, and replaced with a new X. C. The X created in the script will only exist while the script runs, and will then disappear. D. The script X will be automatically given a different name. E. An error will occur, since two variables can’t have the same name. 6/21/2018 10 19 Matlab Function Outputs • The [ out1, out2, … ] = format is for functions that return multiple results. – If a function returns a single result, the [ ] square brackets are not required. – If a function does not return any results, then the equals sign should also be omitted. • The program that called the function gets its results through the output parameters. 20 Matlab Function Help Comments • Every Matlab function should contain a “help” comment, which is the first comment block after the “function” line. • The help comment is displayed whenever one types “help” for the function in question. ( The first line is displayed by lookfor. ) function [ finBalance, interest ] = BalanceWithInterestBoth( rate, initBalance ) % Calculates the final balance of an investment with compound interest. % Inputs: rate -- Interest rate, initBalance -- Initial balance. % Output: finBalance -- Final balance. 6/21/2018 11 21 Matlab Function Operation • The code within a Matlab function is basically the same as a Matlab script. • Sometime before the function returns, it must assign values to all the output parameters. • The ‘return;’ statement ceases execution of the function, and returns control back to the calling program. No function code is executed after a return statement. 22 Calling Matlab Functions • User-written Matlab functions are called by name, the same way that Matlab library functions are called. • Values or variables must be passed in to the function input parameters. • Variables must be provided to receive the output results.