Engineering Computation with MATLAB

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Programming Languages and Paradigms
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Chapter 5 Functions.
An Introduction to Programming with C++ Fifth Edition
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 12: Adding Functionality to Your Classes.
C++ for Engineers and Scientists Third Edition
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Chapter 6: User-Defined Functions
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
David Stotts Computer Science Department UNC Chapel Hill.
JavaScript III Functions and Abstraction. 2 JavaScript so far statements assignment function calls data types numeric string boolean expressions variables.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
User-Defined Functions II TK1914: C++ Programming.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Engineering Computation with MATLAB First Edition by David M. Smith Chapter.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Programming Right from the Start with Visual Basic .NET 1/e
Introduction to Object-oriented Programming
TK1924 Program Design & Problem Solving Session 2011/2012
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Chapter 9: Value-Returning Functions
Programming Logic and Design Seventh Edition
Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
IS 350 Application Structure
Abstraction and Functions
Abstract Data Types and Encapsulation Concepts
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Chapter 9 Recursion.
Chapter 3: Using Methods, Classes, and Objects
Organization of Programming Languages
About the Presentations
Function There are two types of Function User Defined Function
C++ for Engineers and Scientists Second Edition
Abstract Data Types and Encapsulation Concepts
CHAPTER FOUR Functions.
Abstract Data Types and Encapsulation Concepts
6 Chapter Functions.
Group Status Project Status.
Object-Oriented Programming Using C++ Second Edition
Coding Concepts (Basics)
Programming Logic and Design Fourth Edition, Comprehensive
Writing Functions.
Scope Rules and Storage Types
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Chapter 6 – Methods Topics are:
Lecture 2 מבוא מורחב.
Lecture 2 מבוא מורחב.
Subprograms General concepts Parameter passing Functions
Scope Rules.
Abstract Types Defined as Classes of Variables
Presentation transcript:

Engineering Computation with MATLAB CHAPTER 5: Functions Engineering Computation with MATLAB Second Edition by David M. Smith

Objectives This chapter discusses the nature, implementation, and behavior of user-defined functions in MATLAB: ■ How to define a function ■ How data are passed into a function ■ How to return data including multiple results ■ How to include other functions not needed except as helpers to your own function

5.1 Concept: Abstraction Procedural abstraction permits a code block that solves a particular sub-problem to be packaged and applied to different data inputs. analogous to the concept of data abstraction where individual data items are gathered to form a collection. We have already used a number of built-in procedural abstractions in the form of functions. They allow us to apply a code block about which we know nothing to data that we provide.

5.1 Concept: Encapsulation Encapsulation is the concept of putting a wrapper around a collection that you wish to protect from outside influence. Functions encapsulate the code they contain in two ways: the variables declared within the function are not visible from elsewhere, and the function’s ability to change the values of variables (otherwise known as causing side effects) is restricted to its own code body.

5.2 Black Box View of a Function <param 1...n> are the formal parameters – the names given to the incoming data inside the function. <item 1...n> are the actual parameters provided to the function by its caller. They may have names different from the formal parameter names They are correlated to the formal parameter names by their position

5.3 MATLAB Implementation The template for defining a function is: function <return info> <function name> (<parameters>) <documentation> <code body> % must return the results The function code must be stored in a file whose name is the name of the function. Functions return data to the caller by assigning values to the return variable(s) MATLAB throws an error if a function does not make assignments to all the return variables.

5.4 Engineering Example—Measuring a Solid Object We need to compute the volume and wetted area of this object. This requires one function that computes the volume and wetted area of a cylinder We have a range of disk heights for which we require this information.

Solution The function stored in cylinder.m The test script function [area, volume] = cylinder(height, radius) % function to compute the area and volume of a cylinder % usage: [area, volume] = cylinder(height, radius) base = pi .* radius.^2; volume = base .* height; area = 2 * pi * radius .* height + 2 * base; The test script clear clc h = 1:5; % set a range of disk thicknesses R = 25; r = 3; [Area Vol] = cylinder(h, R) % dimensions of large disk [area vol] = cylinder(h, r) % dimensions of the hole % compute remaining volume Vol = Vol - 8*vol Area = Area + 8*(area - 2*2*pi*r.^2)

Summary This chapter showed you how to encapsulate a code block to allow it to be re-used: ■ Functions are defined in a file of the same name using the key word function to distinguish them from scripts ■ Parameters are copied in sequence into the function and given the names of the formal parameters ■ Results are returned to the caller by assigning value(s) to the return variable(s) ■ Variables within the function have scope only in the function’s code block unless they are declared global ■ Helper functions accessible only to functions within the same file may be added behind the main function and otherwise obey the same rules as the main function

Questions?