1 / 24

4. Week 04. March .2014

4. Week 04. March .2014. Use of M-File. Editor/Debugger: text editor, debugger; editor works with file types in addition to .m (MATLAB “m-files”). Use of M-File. Click to create a new M-File. There are two kinds of M-files:

lukas
Download Presentation

4. Week 04. March .2014

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4. Week04.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 Click to create a new 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.

  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. 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. A function definition line may look slightly different depending on whetherthere is no output, single output, or multiple outputs. 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

  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. • 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: Input variables must be specified before hand Input variables are already specified [r, h, f] = motion (rx, ry, [0:100]); [r, h, f] = motion (2, 3.5, 0.001); [r, angmom, force] = motion (xt, yt, time);

  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. EXAMPLE Pleasefindthevalueforthefollowingfunctionwith a givenvariable x. Function is 1. Way 2. Way

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

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

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

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

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

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

  21. 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 Intro MATLAB

  22. breakcommand –exits for loop ; used in case of error. continuecommand –ends one for loop iteration ; crudely equivalent to GOTO end

  23. Homework 3 Prepareyourscripttoobtain a table of a formulafordifferentinputparameters. Please deliver until 17.03.2014

More Related