4. Week 04.March.2014. 2 Use of M-File Editor/Debugger: text editor, debugger; editor works with file types in addition to.m (MATLAB “m-files”)

Slides:



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

Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
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:
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
1 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi Today's Agenda ● Introduce Matlab User Functions.
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.
Introduction to MATLAB MATLAB in Science, Engineering, and Mathematics Instruction John Sebeson DeVry University.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
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.
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
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.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Introduction to MATLAB for Engineers Third Edition William J. Palm III Chapter 11 MuPAD PowerPoint to accompany Copyright © The McGraw-Hill Companies,
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
IMS 3253: Math 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Five Fundamental Math Operations Precedence of Math.
Hydroinformatics: Session4 Dr Ivan Stoianov Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
INTRODUCTION TO MATLAB LAB# 01
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
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.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Scientific Computing Introduction to Matlab Programming.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
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.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Digital Image Processing Introduction to M-function Programming.
Introduction to Matlab
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
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 Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
Matlab for Engineers Matlab Environment Chapter 2.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Week 10: Introduction to important software and tools SEE1012: Introduction to Electrical Engineering.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
전자장 1 실험 - Matlab 사용법 - Photonic Systems Laboratory School of EE, Seoul National University Photonic Systems Lab School of EECS, S.N.U.
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
Introduction to MATLAB
Introduction to MATLAB for Engineers, Third Edition
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Introduction to Matlab
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.
Matlab Basics Tutorial
Presentation transcript:

4. Week 04.March.2014

2 Use of M-File Editor/Debugger: text editor, debugger; editor works with file types in addition to.m (MATLAB “m-files”)

3 Use of M-File There are two kinds of M-files: – Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace. – Functions, which can accept input arguments and return output arguments. Internal variables are local to the function. Click to create a new M-File

A MATLAB SCRIPT FILE Never name your script the same as the name of a variable it computes otherwise MATLAB will have problems accessing the file. Avoid writing anything in your script that clashes with built-in functions. Save your script as a m-file. Save it always in the ‘WORK’ directory/folder.Otherwise MATLAB will have problems in accessing that file. Avoid any space between the letters or numbers when you name your m-file. MATLAB doesn't like it and will create unnecessary problems.

5 M-File as script file Save file as filename.m Type what you want to do, eg. Create matrices If you include “;” at the end of each statement, result will not be shown immediately Run the file by typing the filename in the command window

RUNNING A SCRIPT FILE

Function Files in MATLAB A function file is also an m-file, like a script file. Function files are like programs or subroutines in FORTRAN, procedures in PASCAL and functions in C or C++. A function file begins with a function definition line, which has a well defined list of inputs and outputs. Without this line the file becomes a script file.

function [output variables] = function_name (input variables); Examples: Function Definition Line File name function [rho, H, F] = motion (x, y, t); motion.m function [theta] = angleTH (x, y); angleTH.m function theta = THETA (x, y, z); THETA.m function [] = circle (r); circle.m function circle (r) circle.m A function definition line may look slightly different depending on whether there is no output, single output, or multiple outputs.

Executing a Function There are two ways in which a function can be executed, weather it is in built or user written. (1)With explicit output, (2) Without any output. (1)With explicit output: This is the full syntax of calling a function. Both the output and the input list are specified in the call. Example: if the function reads: function [rho, H, F] = motion (x, y, t); then all the following commands represent legal call (execution) statements: [r, angmom, force] = motion (xt, yt, time); Input variables must be specified before hand [r, h, f] = motion (rx, ry, [0:100]);[r, h, f] = motion (2, 3.5, 0.001); Input variables are already specified

Executing a Function-cont. (2) Without any output: The output list can be omitted entirely if the computed quantities are not of immediate interest. This might be the case when the function displays the desired result graphically.

FUNCTIONS In addition to the basic operations, we can also call the internal functions to use, such as the trigonometry functions, logarithmic, exponential functions etc. A function is use as “functionname()”, where the input value to the function is given inside the parentheses.

>> sqrt(4) ans = 2 “sqrt()” is a function that takes the square root of the input variable.

Trigonometry Functions Note, when you use trigonometric functions, such as sine and cosine, the input is an angle measured in radiance. If you know the angle measured in degrees, you can do it as >> sin(30*pi/180) ans = Where pi (π) = (built in constant). The above express converts the angle in degrees to radiances first and then evaluated by the sine function. Similarly you can do >> cos(30*pi/180) ans = >> tan(30*pi/180) ans =

EXAMPLE Please find the value for the following function with a given variable x. Function is 1. Way 2. Way

Exponential Functions we use exp(x) to calculate the xth power to e. e = >> exp(1) ans = >> exp(2) ans =

Logaritmic Functions For logarithms, the natural logarithm lnx in mathematics is written log(x) in MATLAB, and lnx=logex (in mathematics) For x variable Lnxin mathematics log(x)in MATLAB Example

log ten of x in mathematics is log10(x) in MATLAB Example We know that ln(1) and lg(1) are both 0!!! İn mathematics

round, floor, ceiling. Rounding of number round(number) - rounding to the closest integer floor(number)-rounding towards lesser integer ceil(number) -rounding towards greater integer Example math:round(45.50) -Will equal 46 math:floor(45.60) -Will equal 45 math:ceil(45.20) -Will equal 46 math:round(-4.5) -Will equal -4 math:floor(-4.6) -Will equal -5 math:ceil(-4.20) -Will equal -4

Intro MATLAB Command-Line Help  : List of MATLAB Topics >> help HELP topics: matlab\general - General purpose commands. matlab\ops - Operators and special characters. matlab\lang - Programming language constructs. matlab\elmat - Elementary matrices and matrix manipulation. matlab\elfun - Elementary math functions. matlab\specfun - Specialized math functions. matlab\matfun - Matrix functions - numerical linear algebra. matlab\datafun - Data analysis and Fourier transforms. matlab\polyfun - Interpolation and polynomials. matlab\funfun - Function functions and ODE solvers. matlab\sparfun - Sparse matrices. matlab\scribe - Annotation and Plot Editing. matlab\graph2d - Two dimensional graphs. matlab\graph3d - Three dimensional graphs. matlab\specgraph - Specialized graphs. matlab\graphics - Handle Graphics. …etc...

Intro MATLAB Command-Line Help  : List of Topic Functions >> help matfun Matrix functions - numerical linear algebra. Matrix analysis. norm - Matrix or vector norm. normest - Estimate the matrix 2-norm. rank - Matrix rank. det - Determinant. trace - Sum of diagonal elements. null - Null space. orth - Orthogonalization. rref - Reduced row echelon form. subspace - Angle between two subspaces. …

Intro MATLAB Command-Line Help  : Function Help >> help det DET Determinant. DET(X) is the determinant of the square matrix X. Use COND instead of DET to test for matrix singularity. See also cond. Overloaded functions or methods (ones with the same name in other directories) help laurmat/det.m Reference page in Help browser doc det

break command –exits for loop ; used in case of error. continue command –ends one for loop iteration ; crudely equivalent to GOTO end

Homework 3 Prepare your script to obtain a table of a formula for different input parameters. Please deliver until