260 likes | 372 Views
EGR 106 – Functions . Functions Concept Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178). “Function” concept. So far, you have: used Matlab’s built-in functions written scripts Function: Reusable script, sometimes called a subprogram
E N D
EGR 106 – Functions • Functions • Concept • Examples and applications • Textbook chapter 6.1-6.7p15-165, 6.11(p 178)
“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
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)
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)
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)
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)
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)
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
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.
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
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
Typical Errors for Functions Too few inputs
Too many inputs Too many outputs Wrong input type – funny result
DIFFUSION Diffusion – 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 .
Non Steady State Ficks 2nd Law Non Steady State – Concentration changes at position x as a function of time, eg Cu Ni dc/dt=D(d2C/dx2) 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
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
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
Functions for Project • Function to calculate time? • What input data? • Form of output? Graph, number?
TEAMS On Tuesday, March 11, need team names and team members to be handed in.
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”.