EGR 106 Week 5 More on Files - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

EGR 106 Week 5 More on Files

Description:

EGR 106 Week 5 More on Files. Debugging errors in scripts. Interacting with data files ... asv = auto save. ascii = regular text files. ... – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 31
Provided by: gatew318
Category:
Tags: egr | autosave | files | more | week

less

Transcript and Presenter's Notes

Title: EGR 106 Week 5 More on Files


1
EGR 106 Week 5 More on Files
  • Debugging errors in scripts
  • Interacting with data files
  • Functions
  • Concept
  • Examples and applications
  • Textbook chapter 4.4-4.6.1, 4.7-4.8 and chapter
    6.1-6.7, 6.10

2
Debugging Errors
  • Syntax errors

Note red text bad news
But tells you where
3
  • Run-time errors inf or NaN results

Note black text okay, just a warning
4
  • Logical errors in your program hard to find
  • Example quadratic equation solver
  • But x22x1 (x1)2 ? x 1
  • Use the built-in debugger

Missing parentheses around 2a and around b
sqrt( ..)
5
MATLABs Debugger
6
  • The red dot at line 6 indicates a breakpoint
  • The green arrow shows which line will next be
    evaluated

7
Matlab Data Files (not in the text)
  • Types
  • .asv auto save
  • ascii regular text files
  • .mat Matlabs proprietary format (multiple
    variables)

8
  • Saving data
  • save filename
  • save filename array1 array2
  • save filename ascii
  • Reading in (loading) data
  • load filename
  • load filename array1 array2
  • load filename ascii

9
Function concept
  • So far
  • Have used Matlabs built-in functions
  • Have started writing scripts
  • Function computes an output from an input
  • Reusable script
  • Sometimes called a subprogram
  • Building block for larger programs
  • Example converting degrees to radians

10
Example Function DEG2RAD
11
Usage
  • Syntax is just like a built-in functions
  • Application is independent of the variable names
    within the function (x,y)
  • Executed by typing name (input)

12
Rules for Functions
  • First line of the file must be of the form
  • function outputs name(inputs)

List of any variables that the function needs
Identifies a function file
List of function result variables
Name of the function and the file (name.m)
13
  • inputs
  • Used to transfer data into the function from the
    workspace
  • Workspace variables are unavailable within the
    function
  • Any necessary variables must be brought in
  • For multiple inputs
  • Separate them by commas
  • Order is important
  • Examples of built-in functions with inputs
  • sum(x) plot(x,y)

14
  • outputs
  • Used to transfer results back into the workspace
    from the function
  • For multiple outputs
  • Separate them by commas in brackets
  • Order is important
  • Output variables must be assigned
  • Examples of built-in functions with outputs
  • y sum(x)
  • value,location max(x)

15
  • Note brackets on the left only make sense for
    functions
  • value,location max(x) is okay
  • value,location 1, 2 is not
  • Default output is the first one
  • value,location max(x)
  • value max(x)

16
  • Comments in lines 2,
  • Words in line 2 are searched when lookfor is
    employed
  • Comments in lines 2, 3, are returned when help
    name is executed

17
Variables Local vs Global
  • Usually, once created, variables are available in
    the workspace until cleared
  • Functions create their own workspace with their
    own local variables distinct from those in the
    original workspace
  • Functions cannot modify variables within the
    original workspace except through outputs
  • Exception global variables can span both
    workspaces and be manipulated in both

18
Example Functions
  • Consider a script to compute the area of a
    triangle (from week 2)
  • First as a script

19
(No Transcript)
20
Now as a function
21
  • Usage doesnt depend on knowing parameter names
    internal to the program

22
  • Could add the perimeter calculation as a second,
    optional output

23
  • My plotting function

24
(No Transcript)
25
  • Can stack functions in one file (all must be
    functions)
  • Useful for mailing and testing

26
Typical Errors
Too few inputs
27

Too many inputs
Too many outputs
Wrong input type funny result
28
Application f(x) 2e-xsin(x)e-x/2sin(2x)
  • Find its minimum and the first root beyond zero

x ?
f(x) ?
29
Solution using Matlab
  • Construct a function m-file for f(x)

30
  • To find the root fzero('fun',a)
  • To find the minimum fminbnd('fun',a,b)
Write a Comment
User Comments (0)
About PowerShow.com