CS005 Introduction to Programming

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Introduction to C Programming
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Probability 2 Compound Probability.  Now lets consider the following:  2 dice are rolled and the numbers are added together.  What are the numbers.
STEP 1 Multiply the digits in the ones place. Write the product in the ones place of the answer box. If the product is greater than ten, carry the number.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Introduction to Computer Programming
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
The Hashemite University Computer Engineering Department
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Precedence Operators Error Types
User-Written Functions
More on Functions (Part 2)
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Introduction to Python
CS005 Introduction to Programming
Chapter 2 - Introduction to C Programming
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Chapter 2 - Introduction to C Programming
CS005 Introduction to Programming
Learning to program with Logo
Tutorial Tutorial Read all the directions before proceeding
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Learning to Program in Python
Mr Barton’s Maths Notes
Number and String Operations
Advanced Programming Lecture 02: Introduction to C# Apps
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Fundamentals of Programming
Chapter 2 - Introduction to C Programming
CS005 Introduction to Programming
Alice Variables Pepper.
Introduction to Computer Programming
CISC101 Reminders Assn 3 due tomorrow, 7pm.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Introduction to TouchDevelop
Chapter 2 - Introduction to C Programming
More on Functions (Part 2)
How to Start This PowerPoint® Tutorial
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.
Chapter 2 - Introduction to C Programming
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Introducing JavaScript
Chapter 7 Empowering Programs with Math
CISC101 Reminders Assignment 3 due today.
Presentation transcript:

CS005 Introduction to Programming Matlab Eamonn Keogh eamonn@cs.ucr.edu

Review: Using Built-In Functions We can use some of Matlabs built-in functions to do calculations Here sqrt is a function 9 is a parameter argument input argument 3 was the returned value result output Here we say that sqrt was called invoked EDU>> sqrt(9) ans = 3

Review: Using Built-In Functions We sometimes talk about a black box view of a function EDU>> sqrt(9) ans = 3 9 3

We can write our own Functions! The process is very similar to how we wrote scripts However we need to tell matlab what are our Input parameter(s) (can be zero, one, two …) Returned value (Unlike most languages, matlab allows multiple returned values, we will not be doing this in this class) 9 3

Designing Functions A name for the function Let us imagine we want to make a function that takes in three numbers and returns their sum. We need A name for the function This name should be descriptive It should not be currently used The naming rules are the same for variable names Let us use SumOfThreeNums 1 7 2 10

Designing Functions A list of input variable names Let us imagine we want to make a function that takes in three numbers and returns their sum. We need A list of input variable names These name should be descriptive It should not be currently used The naming rules are the same for normal variable names Let us use Number1, Number2, Number3 1 7 2 10

Designing Functions A name for the returned value variable Let us imagine we want to make a function that takes in three numbers and returns their sum. We need A name for the returned value variable These name should be descriptive It should not be currently used The naming rules are the same for normal variable names Let us use ReturnedSum 1 7 2 10

We will be able to use the function just like any built in function. 1 7 2 Number1, Number2, Number3 SumOfThreeNums ReturnedSum 10

We begin by opening an editable file, just like we did for scripts…

The first word must be function, matlab will highlight it in blue. The second word is our chosen return variable name; ReturnedSum Next comes the assignment operator = Then our function name; SumOfThreeNums Next, we have, in parenthesis, a list of our input arguments; Number1, Number2, Number3 2 10 7 1 ReturnedSum Number1, Number2, Number3 SumOfThreeNums

Now we can write the actual code that does the work of the function. In this case it is a single line, more generally it could be hundreds of lines or even more. It is critical that the last line of our function, contains the return value variable being assigned some value (i.e, on the LHS of a = sign) (This does not have to be true, but it is a very good idea in 99.99% of code, so we will insist on it)

We need to save the file before we use it… We should name the file, the same name as the function (Matlab does not insist on this, but doing otherwise can cause huge problems. We will always use the function name for the file name) As with scripts, the file name always ends with .m

We can now use the function just like a built in function…

EDU>> SumOfThreeNums(1,2,max(1,2)) ans = 5 EDU>> SumOfThreeNums(1, 2, 2 + 4) 9 EDU>> SumOfThreeNums(1,2,SumOfThreeNums(4,2,0))

EDU>> StateTax = 17; EDU>> FedTax = 23; EDU>> CityTax = 102; EDU>> EamonnsTax = SumOfThreeNums(StateTax,FedTax,CityTax) EamonnsTax = 142 EDU>> (values left blank below, lets fill them in during lecture) StateTax EamonnsTax CityTax FedTax 3 inf 23 44 -2

Review: we have seen this slide before What is Eamonn’s BMI, using imperial units? He is 74 inches tall and 220 lbs in weight It is a little different to the metric version, due to rounding

Let write a function to get a users BMI What would be a good name for this function, how about GetBMI ? What are the input arguments? There are none! What is the returned value, it is a real number. Let us call the returned value variable, UsersBMI ? 21.2

List of input arguments is empty Function name and file name are the same The last line of code assigns the return variable some value

Minor point: Matlab prefers that you end your function with the word end It makes no difference, we will see why this is later…

EDU>> Fink_Nottles_BMI = GetBMI; How tall are you in inches : 65 How heavy are you in pounds : 134 EDU>> It would also be legal to say EDU>> Fink_Nottles_BMI = GetBMI();

Lets write a function to toss a dice EDU>> EamonnsRoll = TossDice EamonnsRoll = 2 EDU>> Fink_NottlesRoll = TossDice Fink_NottlesRoll = 3

Mia This is a very old and ancient, bluffing dice game, played with two dice and a flat bottomed container with a lid, for any number of players. Play: All players start the game with three lives. The first player rolls the dice without revealing what was rolled. This initial player then has three choices of what to announce to the others: Announce truthfully what has been rolled, Announce (by lying) a greater value than that rolled.  Announce (by lying) a lesser value than that rolled. The dice values are ranked by multiplying the higher value of the two by 10 and adding the lower value to produce a two digit number. All possible results are ranked in order as follows: 21, 11, 22, 33, 44, 55, 66, 65, 64, 63, 62,61, 54, 53, 52, 51, 43, 42, 41, 32, 31 http://en.wikipedia.org/wiki/Mia_(game)

The dice values are ranked by multiplying the higher value of the two by 10 and adding the lower value to produce a two digit number. EDU>> Dice1 = TossDice; EDU>> Dice2 = TossDice; EDU>> EamonnsMaiScore = max(Dice1,Dice2)*10 + min(Dice1,Dice2) EamonnsMaiScore = 54

Or we could write a function… EDU>> Dice1 = TossDice; EDU>> Dice2 = TossDice; EDU>> EamonnsMaiScore = max(Dice1,Dice2)*10 + min(Dice1,Dice2) EamonnsMaiScore = 54 EDU>> EamonnsMaiScore = MiaScore EamonnsMaiScore = 51 EDU>> Note that we have one function we wrote (MiaScore), calling another function we wrote (TossDice)

I have not commented my teaching slides for space reasons, but remember that we should always comment our code

The red and blue codes below do the exact same thing Why write functions? The red and blue codes below do the exact same thing EDU>> Dice1 = TossDice; EDU>> Dice2 = TossDice; EDU>> EamonnsMaiScore = max(Dice1,Dice2)*10 + min(Dice1,Dice2); EDU>> BingosMaiScore = max(Dice1,Dice2)*10 + min(Dice1,Dice2); EDU>> GussiesMaiScore = max(Dice1,Dice2)*10 + min(Dice1,Dice2); EDU>> BarmysMaiScore = max(Dice1,Dice2)*10 + min(Dice1,Dice2); EDU>> EamonnsMaiScore = MiaScore; EDU>> BingosMaiScore = MiaScore; EDU>> GussiesMaiScore = MiaScore; EDU>> BarmysMaiScore = MiaScore; 1) Brevity. 2) Less likely to introduce mistakes. 3) Readability. 4) Ease of debugging