1 / 25

EGR 106 Functions

bennett
Download Presentation

EGR 106 Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    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)

    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

    22. Diffusion and Temperature - Plots

    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 = x2 t=x2/D =(5x10-4) 2/(5x10-12) t= 25x10-8/5x10-12 =5x104sec =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”.

More Related