Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1.

Slides:



Advertisements
Similar presentations
Using MatLab and Excel to Solve Building Energy Simulation Problems Jordan Clark
Advertisements

Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
The Web Warrior Guide to Web Design Technologies
Flow Charts, Loop Structures
Good modeling practices AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by Gillig and McCarl. Improved upon by many previous lab.
Computer Science 1620 Programming & Problem Solving.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
Programmer Defined Functions Matthew Verleger. Windows It’s estimated that Window’s XP contains 45 million lines of code (and it’s over 10 years old).
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
An Introduction to Textual Programming
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,
1 -Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. How it works Programmer.
1 Functions Advantages Name, parameters, return info Flow of the data when running functions Menus Functions Applying Functions to Real Projects.
Lecture 4 Input/Output Conditional Statements 1. Data Types 2. Input/Outputs 3. Operators 4. Conditionals 1.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
1. Reminder of Symbols 2. Dialog Boxes 3. listdlg() 4. msgbox() 5. questdlg() 6. menu() Dialog Boxes Applications of Cell-Arrays 1.
Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Some Basic Math Concepts for Statistics
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Asking the USER for values to use in a software 1 Input.
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Programmer-Defined Functions Advantages vs. Disadvantage 1.Definition and general Idea 2.Many Advantages 3.1 Disadvantage 1.
Asking the USER for values to use in a software 1 Input.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Cell Arrays Lecture 2/8/ Data Types (Review) 2. General Concept 3. Using Cell-Arrays 1. Syntax/Symbols 2. Dialog Boxes 1.
2-2 What is a Matlab function? How does the plot function work? What Matlab functions are available to do Data Analysis? How can you create your own function.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Matlab for Engineers Matlab Environment Chapter 2.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Fractions.
More About Objects and Methods
MATLAB – More Script Files
Introduction to Programming
Completing the Problem-Solving Process
Dialog Boxes Applications of Cell-Arrays
Chapter 4 MATLAB Programming
Functions CIS 40 – Introduction to Programming in Python
Scripts & Functions Scripts and functions are contained in .m-files
User-Defined Functions
User Defined Functions
Functions In Matlab.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Using Script Files and Managing Data
Presentation transcript:

Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1

Convert Time. Step1. Problem: Given a time as a whole amount of seconds, convert it to hr:min:sc format using the method shown hereafter. 2

Convert Time. Step2. myTime = seconds To calculate the hours, divide by 3600 (seconds/hr) Find the minutes by converting the (hr) to minutes: Find the seconds by converting the (min) to seconds: (sec) 3600 (sec/hr) = (hr) = 9 full hr (hr) * 60 (min/hr) = (min) = 1 full minute (min) * 60 (sec/min) = 52 (sec)

Convert Time. Step3. 4 ** MAGIC **KeyboardScreen Full time hrs min sc

Convert Time. Step 4a. Algorithm %prompt user for number of full seconds %divide by 3600 to get the hours %separate whole part and decimal part %multiply the decimal part by 60 to get the minutes %separate whole part and decimal part %multiply that decimal part by 60 to get the seconds %display result 5 Code doing the same operation twice, on a different number, but SAME CODE. Let's create a function!

Overall Flow 6 Main script file separate() CALL it once to solve for the minutes. CALL it once to solve for the seconds.

I/O of the function One parameter, 2 return-values 7 separate() One value The whole part The decimal part

Step.4b. Function Definition 8 The keyword function tells Matlab this is a function definition. Open a new script file, and type the following:

Function Definition 9 When more than one return value are present, they MUST be in square brackets. Important: MATLAB does not return a vector with 2 values. This is just how MATLAB wants us to indicate multiple return values. It does return 2 separate values.

Function Definition 10 The parameter goes on the right, in between ()

Function Definition 11 The documentation shows how to use the function, what it does, and any assumptions made.

Saving the function definition 12 SAVE THE FUNCTION. MATLAB offers automatically the correct name, i.e. the name of the function itself. Confirm by clicking Save.

Function Definition 13 What would be the code if this was before you learned what a function was? Suppose: value = 2.45 whole = ? decimal = ?

Function Definition 14

Function Definition

Function Definition

Function Definition 17 ?? ?? Same code, different input.

Function definition: done! Function: done! But before we call our boss and colleague engineers to put the main script file together, we need to test! 18

Step5. Testing. Once again, jump to the Command Window to experiment: (Imagine you were calling a built-in function.. It works the same way as with any other keyword, except you created the keyword separate() ) 19 Two separate scalars, NO VECTOR, even if we use [ ].

Step5. Testing (MISTAKE) Remember that MATLAB only stores 1 value by default, and uses the default variable name ans. This is the same, whether it is a built-in or programmer defined function. 20 MATLAB does get two values returned by the function, BUT there are no variables COLLECTING them. MATLBA uses the default ans for the first one, but ignores the second return-value since it does not have a 2 nd default variable name…

Step5. Testing Note you can also test the documentation!!! (and should) >> doc separate 21

Back to step4b. Now that the function works, your boss/colleagues/you may put together the main script file. 22

Step.4b. Main File %prompt user for number of full seconds %divide by 3600 to get the hours %separate whole part and decimal part %multiply the decimal part by 60 to get the minutes %separate whole part and decimal part %multiply that decimal part by 60 to get the seconds %display result 23 Time to create the main script, and actually solve our conversion problem. REMEMBER: keep the main script file and the function definition in the same folder.

24 1 st call 2 nd call Write the FUNCTION CALLS. These lines "call upon the execution" of the code inside the definition, with various inputs each time.

The 1 st call converts the hrs 25 whole = floor(value); decimal = value-whole;

The 2 nd call converts the min Same function: different ARGUMENT 26 whole = floor(value); decimal = value-whole;

Step5. Testing F5 yields: e+001 is the scientific notation for 52. Due to round off issues, the final number of seconds has round off issues.  Call the client to figure out how s/he wants this handled! 27

Use the function in another project! BankOfAmerica ® has a program called "keep the change". When a transaction is made on the debit card, the amount is rounded up to the nearest whole amount. The difference is automatically deposited in a savings account. For example: transaction = $4.65. $5 is debited from the checking account $1 - $0.65 = $0.35 is credited to the savings account Create a main script file that uses separate() to solve and indicate how much is placed in the savings account. 28

Wrapping Up Please make sure you typed this example as we went along. Returning two or more return-values makes us use [ ]. Do not use [ ] when there is only 1 value returned. It clutters the screen, and has no advantage. MATLAB does not return an array, it is just the notation that requires the symbols. CAUTION: the order of the variables matter. Be courageous not to name all the variables the same everywhere. Parameters are the variables inside the parentheses IN the function definition. Arguments are the variables inside the parentheses IN the function call statement. 29