Matlab Chapter 5 slides, with some basic skill., Slides of Computer Science

This is Matlab slides that include some basic skill and knowledge.

Typology: Slides

2020/2021

Uploaded on 12/03/2021

1-63l
1-63l 🇹🇼

5

(1)

13 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 5 User-Defined Functions
Introduction to MATLAB functions
Variables Passing in MATLAB
The Pass-By-Value Scheme
Optional Arguments
Sharing Data Using Global Memory
Preserving Data Between Calls to a Function
Function Functions
Subfunctions, Private Functions, and Nested
Functions
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download Matlab Chapter 5 slides, with some basic skill. and more Slides Computer Science in PDF only on Docsity!

Chapter 5 User-Defined Functions

  • Introduction to MATLAB functions
  • Variables Passing in MATLAB
    • The Pass-By-Value Scheme
  • Optional Arguments
  • Sharing Data Using Global Memory
  • Preserving Data Between Calls to a Function
  • Function Functions
  • Subfunctions, Private Functions, and Nested

Functions

Review

• Top-down design (Chapter 3)

  • Starts with a statement of the problem to be solved and

the required inputs and outputs.

  • Describes the algorithm to be implemented by the

program in broad outline

  • Applies decomposition to break the algorithm down

into logical subdivisions called sub-tasks.

  • Breaks down each sub-task until you winds up (使結束)

with many small pieces

  • Does a simple and clearly understandable job
  • The individual pieces are turned into MATLAB code.
  • Data hiding
    • The variables in the main program are not

visible to the function (except for those in the

input argument list)

  • The variables in the main program cannot be

accidentally modified by anything occurring in

the function.

5.1 Introduction to MATLAB

functions

  • Script files
    • all of the M-files
    • Collection of MATLAB statements that are

stored in a file.

  • Share the Command Windows’s workspace
  • No input arguments and returns no results
  • Can communicate with other script files

through the data left behind in the workspace

Dummy argument actual arguments

No effect on the values in the calling program In C language: scalar values are passed by call-by-value arrays are passed by call-by-address In MATLAB: scalars and arrays are passed by call-by-value

Example 5.1—Rectangular-to-Polar

Conversion (page 211)

x y ry P x

x = r cos

y = r sin 2 2 r = x + y x y 1 tan −  = Write two functions: rect2polar polar2rect

 is expressed in degree

The relationship between degrees and radians is

180 。^ =  radians

Example 5.2—Sorting Data (page

swap

An example problem demonstrating the selection sort algorithm

If we are sorting N values, this sorting algorithm requires N- 1 scans through the data to accomplish the sort.

5.3 Optional Arguments

  • Many MATLAB functions support optional input arguments and
output arguments.
  • plot function
    • 2~7 input arguments
  • max function
    • One or two output arguments
      • If one output argument, max returns the maximum value of an array
      • If two output arguments, max returns both maximum value and the location of the maximum value in an array.
  • How do MATLAB functions know how many input and output
arguments are present?
  • How do they adjust their behavior accordingly?
  • There are eight special functions that can be used by MATLAB
functions to get information about their optional arguments and to
report errors in those arguments.

Example 5.3—Using Optional

Arguments (page 223)