Matlab Scripts and Functions Reading Review, Lecture notes of Programming for Engineers

It uses either fixed-point or exponential notation, whichever “fits best” for the value being printed. E. It generates an error, since the % is ...

Typology: Lecture notes

2022/2023

Uploaded on 03/01/2023

themask
themask 🇺🇸

4.4

(17)

310 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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 )
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Matlab Scripts and Functions Reading Review and more Lecture notes Programming for Engineers in PDF only on Docsity!

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 )

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.

7

Preview

What does the %g format specifier do in fprintf( )? A. It uses fixed-point format. B. It uses exponential notation. C. It uses engineering notation. ( Exponential in powers of 3, e.g. kilo, Mega, micro, etc. ) D. It uses either fixed-point or exponential notation, whichever “fits best” for the value being printed. E. It generates an error, since the % is for comments. 8

Simple Matlab Format Options

  • format long / short – Adjusts # of digits shown.
  • format compact / loose – single/double spacing.
  • format bank – Dollars and cents
  • format rat – Ratio of two integers
  • help format – Shows more options.

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 );

13

Floating Point Formats

Operator Description %f Fixed-point notation, 109. %e Scientific notation with lowercase e, as in 6.02e+23, 3.141593e+ %g Either %f or %e, whatever is shorter %E Scientific notation with capital E, as in 6.02E+23, 3.141593E+ %G Either %G or %E, whichever is shorter, 6.02E+23, 3. 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.

15

A Problem with Scripts

  • Commands executed from a script run in the same scope as the command window. - Script commands can clobber command-window variables with the same variable names. - Script variables can clutter up the workspace unnecessarily. - Solve this by writing functions instead of scripts. 16

Function Scope

  • Functions operate in their own space.
  • Variables defined within functions are totally independent of any variables defined in the command window, even if they happen to have the same name.
  • Because of this, values have to be passed in to functions, and the results passed back.

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.

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.