Presentation is loading. Please wait.

Presentation is loading. Please wait.

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”)

Similar presentations


Presentation on theme: "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”)"— Presentation transcript:

1 4. Week 04.March.2014

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

3 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

4 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 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

6 RUNNING A SCRIPT FILE

7 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.

8 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.

9 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

10 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.

11 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.

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

13 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 = 0.5000 Where pi (π) =3.1416 (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 = 0.8660 >> tan(30*pi/180) ans = 0.5774

14

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

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

17 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

18 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

19 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

20 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...

21 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. …

22 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

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

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


Download ppt "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”)"

Similar presentations


Ads by Google