egr 106 – functions functions – concept – examples and applications textbook chapter...
TRANSCRIPT
![Page 1: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/1.jpg)
EGR 106 – Functions
Functions – Concept – Examples and applications
Textbook chapter 6.1-6.7p15-165, 6.11(p 178)
![Page 2: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/2.jpg)
“Function” concept
So far, you have:– used Matlab’s built-in functions– written scripts
Function: – Reusable script, sometimes called a subprogram– Building block for larger programs– Often computes an output from an input
![Page 3: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/3.jpg)
Example Function: DEG2RAD
![Page 4: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/4.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 5: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/5.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 6: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/6.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 7: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/7.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 8: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/8.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 9: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/9.jpg)
Global Variables
To use variables in function and also
workspace, use “Global variables”
Command “global temperature time weight”
MUST DECLARE THEM “GLOBAL” IN YOUR FUNCTION FILE
![Page 10: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/10.jpg)
Comments in lines 2, …:– Words in line 2 are searched when lookfor is
employed – called the “H1” line.– Comments in lines 2, 3, … are returned when
help name is executed, helpful to remember.
![Page 11: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/11.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 12: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/12.jpg)
Example Function: DEG2RAD
![Page 13: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/13.jpg)
Another Example
Compute the area and perimeter of a right triangle as a function of its base and height:
Area = 0.5 * base * height Perimeter = base + height + hypotenuse
![Page 14: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/14.jpg)
• First, just do the area computation:
![Page 15: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/15.jpg)
• Add the perimeter computation:
![Page 16: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/16.jpg)
Example: my personal plotter
![Page 17: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/17.jpg)
![Page 18: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/18.jpg)
Typical Errors for Functions
Too few inputs
![Page 19: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/19.jpg)
Too many inputs
Too many outputs
Wrong input type – funny result
![Page 20: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/20.jpg)
DIFFUSIONDiffusion – is the movement of matter driven by
chemical and thermal processes such as
concentration gradients and heating. Both are
needed as it is an activation controlled
process.
Atoms will diffuse down a concentration gradient
provided they have overcome the
activation energy needed for the process.
Copper atoms will diffuse into the
Nickel until an equal concentration is
Achieved. Remember that Cu-Ni system
Is one of complete solid solubility.
System is NON STEADY STATE .
![Page 21: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/21.jpg)
Non Steady State Ficks 2nd Law
Non Steady State – Concentration changes at position x as a function of time, eg Cu Ni
c/t=D(2C/x2) Ficks 2nd Law
Solution to this :-
Cx-Co/Cs-Co= 1- erf(x/2((Dt)-1/2))
Cx – concentration at depth x at time t, wt%
Co – concentration in average in bulk, wt %
Cs – concentration at surface, fixed with time t, wt%
Co- concentration in average in bulk, wt%
Erf – error function – look up in tables.
x – distance below surface, m
D – diffusion coefficient, m2/s
t – time in seconds
![Page 22: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/22.jpg)
Diffusion and Temperature - Plots
Diffusion increased with temperature
It is activation controlled so follows:-
D=Do exp(-E/kT)
Where D = Diffusivity(m2/sec)
Do = Constant
E = activation energy
k = Boltzman’s Constant
T = temperature in oK
k= 13.8x10-24 J/atom.K
lnD=lnDo – Q/RT
Q – cal/mole
R – 1.987 cal/mole.K
y= c +mx
Slope = Q/R if ln D plotted against 1/T
![Page 23: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/23.jpg)
Example
Time for the carbon concentration at 500C to reach half way between the steel composition level and the external level at 0.5mm below the surface.
Using Fick’s second law Cx-Co/Cs-Co= 1- erf(x/2((Dt)-1/2)) The left hand side is 0.5. 0.5= 1- erf(x/2((Dt)-1/2))
Rearranging 0.5 = erf(x/2((Dt)-1/2)) 0.5 = erf(0.5205)
So 0.5=(x/2 ((Dt)-1/2))
Dt = x2
t=x2/D =(5x10-4) 2/(5x10-12)
t= 25x10-8/5x10-12
=5x104sec =13.8 hours
![Page 24: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/24.jpg)
Functions for Project
Function to calculate time? What input data? Form of output? Graph, number?
![Page 25: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/25.jpg)
TEAMS
On Tuesday, March 11, need team names
and team members to be handed in.
![Page 26: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)](https://reader035.vdocuments.mx/reader035/viewer/2022081603/56649f145503460f94c28419/html5/thumbnails/26.jpg)
Thursdays March 6 and March13
This Thursday meet in DISCOVERY LAB(ECL)
Thursday March 13, QUIZ on all form last quiz.
Today – go to discovery lab and run function “degrad”.