egr 106 – week 8 data files & functions interacting with data files functions – concept –...
Post on 21-Dec-2015
243 views
TRANSCRIPT
![Page 1: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/1.jpg)
EGR 106 – Week 8 Data Files & Functions
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
![Page 2: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/2.jpg)
Saving data:– save filename– save filename array1 array2– save filename –ascii
Reading in (loading) data:– load filename– load filename array1 array2 – load filename –ascii
Importing/Exporting Data
Can Independently Create Data File in Most Any Program such as: Word, WordPad, Notepad
![Page 3: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/3.jpg)
Can Easily Create Data Files in Notepad
Simply Type in Numbers and Save
Save File for Future Use
![Page 4: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/4.jpg)
Function Concept
So far:– Have used Matlab’s 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
![Page 5: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/5.jpg)
Example Function: DEG2RAD
![Page 6: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/6.jpg)
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)
![Page 7: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/7.jpg)
Rules for Functions
First line of the file must be of the form:
function [outputs] = name(inputs)
Identifies a function file
List of function result variables
List of any variables that the function needs
Name of the function and the file (name.m)
![Page 8: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/8.jpg)
inputs:– Used to transfer data into the function from
the workspaceWorkspace variables are unavailable within the
functionAny necessary variables must be brought in
– For multiple inputs:Separate them by commasOrder is important
– Examples of built-in functions with inputs: sum(x) plot(x,y)
![Page 9: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/9.jpg)
outputs:– Used to transfer results back into the
workspace from the function– For multiple outputs:
Separate them by commas in bracketsOrder is important
– Output variables must be assigned – Examples of built-in functions with outputs:
y = sum(x)
[value,location] = max(x)
![Page 10: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/10.jpg)
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)
![Page 11: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/11.jpg)
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
![Page 12: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/12.jpg)
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
![Page 13: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/13.jpg)
Example Functions
Consider a script to compute the area of a triangle
First as a script …
![Page 14: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/14.jpg)
![Page 15: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/15.jpg)
• Now as a function:
![Page 16: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/16.jpg)
Usage doesn’t depend on knowing parameter names internal to the program:
![Page 17: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/17.jpg)
Could add the perimeter calculation as a second, optional output:
![Page 18: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/18.jpg)
My plotting function:
![Page 19: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/19.jpg)
![Page 20: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/20.jpg)
Can stack functions in one file (all must be functions)
Useful for mailing and testing
Must start
m-file with
function . . .that could be a main program used to call other functions
![Page 21: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/21.jpg)
Typical Errors
Too few inputs
![Page 22: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/22.jpg)
Too many inputs
Too many outputs
Wrong input type – funny result
![Page 23: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/23.jpg)
Application f(x) = 2e-xsin(x)+e-x/2sin(2x)
Find its minimum and the first root beyond zero
x = ?
f(x) = ?
![Page 24: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/24.jpg)
Solution using Matlab
Construct a function m-file for f(x)
![Page 25: EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter 4.4-4.6.1, 4.7-4.8](https://reader031.vdocuments.net/reader031/viewer/2022020117/56649d615503460f94a43ad9/html5/thumbnails/25.jpg)
To find the root: fzero('fun',a) (a = nearby value)
To find the minimum: fminbnd('fun',a,b) (a,b = range)