E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS

Description:

Input/output data can be of various types, e.g., numbers, strings, arrays, etc. x1 ... (e.g., size, sort, sum, max) String functions (e.g., strmatch, upper) ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 12
Provided by: pack3
Category:

less

Transcript and Presenter's Notes

Title: E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS


1
E77 INTRODUCTION TO COMPUTER PROGRAMMING FOR
SCIENTISTS AND ENGINEERS
  • Lecture Outline
  • 1. The concept of a function
  • 2. MATLAB functions
  • 3. User-defined functions in MATLAB

2
The concept of a function
  • 1. The concept of a function
  • A broad definition of a function a rule which
    assigns a set
  • of known quantities (the input data) to a set
    of outcomes
  • (the output data).

3
The concept of a function
  • 1. The concept of a function
  • A broad definition of a function a rule which
    assigns a set
  • of known quantities (the input data) to a set
    of outcomes
  • (the output data).
  • Symbolically

4
The concept of a function
  • 1. The concept of a function
  • A broad definition of a function a rule which
    assigns a set
  • of known quantities (the input data) to a set
    of outcomes
  • (the output data).
  • Symbolically

5
The concept of a function
  • Pictorially
  • Input/output data can be of various types, e.g.,
    numbers,
  • strings, arrays, etc.

x1
y1
x2
y2
FUNCTION
.
.
.
.
xn
ym
6
MATLAB functions
  • 2. MATLAB functions
  • MATLAB contains a number of useful functions,
    such as
  • Simple mathematical functions (e.g., sin, atan,
  • log, exp, sqrt, abs)
  • Array functions (e.g., size, sort, sum, max)
  • String functions (e.g., strmatch, upper)
  • Cell functions (e.g., celldisp, cellplot)
  • Structure functions (e.g., fieldnames, rmfield)

7
MATLAB functions
  • Existing MATLAB functions are distinguished into
    two
  • categories
  • Built-in functions (i.e., implemented in a
    programming
  • language other than MATLAB)
  • MATLAB-programmed functions
  • MATLAB-programmed functions are contained in
    M-files
  • which include a description of the function and
    all MATLAB
  • instructions needed to perform the requisite
    operations.
  • Use the type command to access the M-file that
    contains
  • a MATLAB-programmed function.

8
User-defined functions in MATLAB
  • 3. User-defined functions in MATLAB
  • One of the most powerful features in MATLAB is
    the ability
  • to create user functions. Some important rules
  • Function files must always possess the extension
    .m
  • (function M-files)
  • Functions can call other functions!
  • Unlike script files, all variables in a function
    file are by
  • default local, namely they are neither
    recognized nor
  • stored in the MATLAB workspace.

9
User-defined functions in MATLAB
  • There exist several efficient ways to evaluate an
    existing or
  • user-defined function xyz123, where x1,x2,...,xn
  • are the input arguments
  • Simply type xyz123(x1,x2,...,xn).
  • Type feval('xyz123',x1,x2,...,xn),
  • where the function is identified by the
    character string
  • 'xyz123'.
  • Type feval(_at_xyz123,x1,x2,...,xn),
  • where the function is identified by the function
    handle
  • _at_xyz123. Function handles are MATLAB data types
  • used in referencing functions.

10
User-defined functions in MATLAB
  • Example 1 A function fun1 that evaluates
  • INSERT EQN
  • for any given real number x.
  • Example 2 A function fun2 that prints
  • I LIKE X BECAUSE IT IS Y
  • on the screen, for any given strings X and Y.
  • Example 3 A function fun3 that inputs a
    two-dimensional
  • array A and outputs the maximum and minimum
    values
  • over all of its elements.
  • Example 4 A function fun4 plotting the motion of
    a
  • projectile.

11
Summary
  • What did we learn today?
  • Functions are understood as rules that use
    existing
  • information (input) to produce new information
    (output).
  • A large number of scientific problems can be cast
    in
  • the form of function development and evaluation.
  • MATLAB already contains a large number of useful
  • functions, both built-in and MATLAB-programmed.
  • User-defined functions can be easily written in
  • MATLAB and constitute a powerful programming
    tool.
  • All variables inside a MATLAB function are
    treated by
  • default as local.
Write a Comment
User Comments (0)
About PowerShow.com