Download presentation
Presentation is loading. Please wait.
Published byTracey Skinner Modified over 9 years ago
1
EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)
2
“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
3
Example Function: DEG2RAD
4
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)
5
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)
6
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)
7
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)
8
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)
9
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
10
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.
11
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
12
Example Function: DEG2RAD
13
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
14
First, just do the area computation:
15
Add the perimeter computation:
16
Example: my personal plotter
18
Typical Errors for Functions Too few inputs
19
Too many inputs Too many outputs Wrong input type – funny result
20
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.
21
Non Steady State Ficks 2 nd Law Non Steady State – Concentration changes at position x as a function of time, eg Cu Ni c/ t=D( 2 C/ x 2 ) Ficks 2 nd 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, m 2 /s t – time in seconds
22
Diffusion and Temperature - Plots Diffusion increased with temperature It is activation controlled so follows:- D=Do exp (-E/kT) Where D = Diffusivity(m 2 /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
23
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 = x 2 t=x 2 /D =(5x10 -4 ) 2 /(5x10 -12 ) t= 25x10 -8 /5x10 -12 =5x10 4 sec =13.8 hours
24
Functions for Project Function to calculate time? What input data? Form of output? Graph, number?
25
TEAMS On Tuesday, March 11, need team names and team members to be handed in.
26
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”.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.