Introduction to Programming for Mechanical Engineers

Slides:



Advertisements
Similar presentations
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Advertisements

Lecture 5.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
Using MatLab and Excel to Solve Building Energy Simulation Problems Jordan Clark
Functions in MatLab Create a new folder on your Z:drive called MatLab_Class24 Start MatLab and change your current directory to MatLab_Class24 Topics:
Program Design. Objectives Students should understand the basic steps in the programming process. Students should understand the need for good design.
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.
EGR 106 – Week 5 – Functions Function concept and an example Rules for functions Local and global variables More examples Errors Application: zeroes and.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
Lecture Note 3: ASP Syntax.  ASP Syntax  ASP Syntax ASP Code is Browser-Independent. You cannot view the ASP source code by selecting "View source"
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Function M-File Numerical Computing with. MATLAB for Scientists and Engineers.
ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
EGR 106 – Functions Functions – Concept – Examples and applications Textbook chapter p15-165, 6.11(p 178)
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Introduction to Programming JScript Six Scripting functions Discuss functions Password Example.
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.
OCR GCSE Computing © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 1: Introduction.
EGR 115 Introduction to Computing for Engineers User-Defined Functions1 – Part 1 Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers.
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.
CPS120 Introduction to Computer Science Iteration (Looping)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Recap Functions with No input OR No output Determining The Number of Input and Output Arguments Local Variables Global Variables Creating ToolBox of Functions.
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.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Designing Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
User Defined Functions Spring EE 201. Class Learning Objectives  Achieve Comprehension LOL of User Defined Functions. Spring
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
MATLAB – More Script Files
Introduction to Programming for Mechanical Engineers (ME 319)
CS005 Introduction to Programming
JavaScript is a programming language designed for Web pages.
Matlab Training Session 4: Control, Flow and Functions
Programming Fundamental
Introduction to Programming for Mechanical Engineers (ME 319)
Introduction to Programming for Mechanical Engineers (ME 319)
JavaScript: Functions.
Intro to PHP & Variables
User Defined Functions
CS190/295 Programming in Python for Life Sciences: Lecture 1
Chapter 7 - JavaScript: Introduction to Scripting
JavaScript: Introduction to Scripting
Value returning Functions
Writing functions in MATLAB
Functions In Matlab.
Introduction to Matlab LAB 4
Lecture3.
funCTIONs and Data Import/Export
Experiment No. (1) - an introduction to MATLAB
Using Script Files and Managing Data
JavaScript Basics What is JavaScript?
JavaScript is a scripting language designed for Web pages by Netscape.
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Presentation transcript:

Introduction to Programming for Mechanical Engineers Lecture 5.2

Things you should have known so far! Create simple user define functions. Correctly writing the function definition line and understanding all valid naming combination [outputs] = name(inputs). Know the differences between script files and user defined files. Define variables of interest to be global and know the difference between local and global variables. You should always remember to use the same name for the user defined function and the function file name.

Anonymous and Inline functions Anonymous functions: a user defined function that is defined and written within the computer code (not in a separate function file). You can use it in the code after that.. They can be defined in any part of MATLAB (Command window, script files, in user defined functions). Syntax: name = @ (inputs) expression Example: >> distance = @ (t,v,a) v*t+a*t^2/2 distance = @(t,v,a)v*t+a*t^2/2 To call it: >> distance(8,50,-9.81) 86.06 It is a one-line simple user defined function. The expression can use any built in or user defined functions.

Anonymous and Inline functions a user defined function that is defined and written within the computer code (not in a separate function file). You can use it in the code after that.. They can be defined in any part of MATLAB (Command window, script files, in user defined functions). Syntax: name = inline(‘mathematical expression written as a string’) name = inline(‘mathematical expression written as a string’,’arg1’ ,’arg2’,..) ’arg1’ ,’arg2’,.. Are used to rearrange the order of inputs Example: >> cube = inline('x^3') cube = Inline function: cube(x) = x^3 >> cube(3) ans = 27 >> Balance = inline('x+y-a^2','y','a','x') Balance = Balance(y,a,x) = x+y-a^2 >> Balance(1,2,5) 2

feval command Variable = feval (‘function name’,argument value) Example: >> r = feval('sqrt',9) r = 3 >> A = feval('power',3,3) A = 27

Sub-Functions Here the main function is proposed to calculate the area of a given triangle. It calls a subfunction named angle that returns the angle that is enclosed by the base and one of the sides. It then calculated the height through another subfunction named height. Notice that the order here is not important! Compare this to script files!!

Nested Functions: 1st level nested functions As before, the main function is proposed to find the area. It has two nested functions names angle and height. The difference here, if you are going to use nested functions, you have to use an end after each nested function’s body to tell MATLAB that the nested function is no more in action.

Nested Functions: 2nd level nested functions The nested function (multip) is a 2nd level nested function, since it is nested in the nested function (theta)

Functions: other functions Up to this point, this material should be enough to give students a thorough understanding of function files. Other functions and function definitions are recommended for self reading.

Thank you for the attention Any Questions and Suggestions please…