24
4. Week 04.March.2014

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

Embed Size (px)

Citation preview

Page 1: 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”)

4. Week04.March.2014

Page 2: 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”)

2

Use of M-File• Editor/Debugger: text editor, debugger; editor

works with file types in addition to .m (MATLAB “m-files”)

Page 3: 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

Page 4: 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”)

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.

Page 5: 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”)

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

Page 6: 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”)

RUNNING A SCRIPT FILE

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

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.

Page 8: 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”)

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.

Page 9: 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”)

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

Page 10: 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”)

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.

Page 11: 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”)

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.

Page 12: 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”)

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

of the input variable.

Page 13: 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”)

Trigonometry FunctionsNote, 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

Page 14: 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”)
Page 15: 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”)

EXAMPLEPlease find the value for the following function with a given variable x.

Function is

1. Way 2. Way

Page 16: 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”)

Exponential Functionswe use exp(x) to calculate the xth power to e. e =2.718.

>> exp(1)ans = 2.7183>> exp(2)ans =7.3891

Page 17: 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”)

Logaritmic FunctionsFor logarithms,

• the natural logarithm lnx in mathematics is written log(x) in MATLAB, and

lnx=logex (in mathematics)

For x variableLnx in mathematicslog(x) in MATLAB

Example

Page 18: 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”)

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

Page 19: 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”)

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

Page 20: 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”)

Intro MATLAB

Command-Line Help : List of MATLAB Topics

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

Page 21: 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”)

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.

Page 22: 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”)

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

Page 23: 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”)

break command –exits for loop ; used in case of error.

continue command –ends one for loop iteration ; crudely equivalent to GOTO end

Page 24: 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”)

Homework 3

Prepare your script to obtain a table of aformula for different input parameters.

Please deliver until 17.03.2014