EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter...

26
EGR 106 – Functions Functions Concept Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)

Transcript of EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter...

Page 1: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)

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)

“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)

Example Function: DEG2RAD

Page 4: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)

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)

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)

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)

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)

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)

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)

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)

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)

Example Function: DEG2RAD

Page 13: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)

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)

• 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)

• 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)

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)
Page 18: EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter 6.1-6.7p15-165, 6.11(p 178)

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)

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)

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)

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)

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)

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)

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)

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)

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”.