Presentation is loading. Please wait.

Presentation is loading. Please wait.

User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide.

Similar presentations


Presentation on theme: "User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide."— Presentation transcript:

1 User-defined Matlab functions

2 Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide the option of saving the file as plain text, (look for a ``Save As...'' option in the file menu). When you are writing m-files you will usually want to have the text editor and MATLAB open at the same time. MATLAB m-files must be plain text files. Most word-processors provide the option of saving the file as plain text, (look for a ``Save As...'' option in the file menu). When you are writing m-files you will usually want to have the text editor and MATLAB open at the same time.

3 Function Definition The first line of a function m-file must be of the following form. The first line of a function m-file must be of the following form. function [output_parameter_list] = function_name(input_parameter_list) function [output_parameter_list] = function_name(input_parameter_list) The function_name is a character string that will be used to call the function. The function_name must also be the same as the file name (without the ``.m'') in which the function is stored. In other words the MATLAB function, ``foo'', must be stored in the file, ``foo.m''. Following the file name is the (optional) input_parameter_list.There can exactly be one MATLAB function per m-file. The function_name is a character string that will be used to call the function. The function_name must also be the same as the file name (without the ``.m'') in which the function is stored. In other words the MATLAB function, ``foo'', must be stored in the file, ``foo.m''. Following the file name is the (optional) input_parameter_list.There can exactly be one MATLAB function per m-file. Commenting: MATLAB comment statements begin with the percent character, %. All characters from the % to the end of the line are treated as a comment. Commenting: MATLAB comment statements begin with the percent character, %. All characters from the % to the end of the line are treated as a comment.

4 Simple example Here is a trivial function, addtwo.m function f=addtwo(x,y) % addtwo(x,y) Adds two numbers, ( or vectors) % whatever, and assign the result to an output variable f f = x+y; end Here is a trivial function, addtwo.m function f=addtwo(x,y) % addtwo(x,y) Adds two numbers, ( or vectors) % whatever, and assign the result to an output variable f f = x+y; endaddtwo.m After the file is saved in the current working directory, you can use the function, e.g., >> addtwo(2,3) ans = 5 >> addtwo(addtwo(1,2), addtwo(2,3)) ans = 8 After the file is saved in the current working directory, you can use the function, e.g., >> addtwo(2,3) ans = 5 >> addtwo(addtwo(1,2), addtwo(2,3)) ans = 8

5 Anonymous matlab functions We can use anonymous functions in Matlab to emulate the behavior of mathematical functions like f(x) = 2x^2 -3 We can use anonymous functions in Matlab to emulate the behavior of mathematical functions like f(x) = 2x^2 -3 The general syntax of an anoymous function is >> fhandle = @(arglist) expr fhandle: function name; arglist: the list of the arguments (e.q., x, y..) expr: the body of the function. The general syntax of an anoymous function is >> fhandle = @(arglist) expr fhandle: function name; arglist: the list of the arguments (e.q., x, y..) expr: the body of the function.

6 Example 1 Create a function to emulate the behavior of the mathematical function f(x) = 2x^2 − 3. >> f = @(x) 2*x^2 - 3 Create a function to emulate the behavior of the mathematical function f(x) = 2x^2 − 3. >> f = @(x) 2*x^2 - 3 f = f = @(x) 2*x^2 – 3 >> f(3) ans = 15 @(x) 2*x^2 – 3 >> f(3) ans = 15

7 Example 2 Create a function to emulate the mathematical behavior of the function defined by f(x, y) = 9 − x^2 − y^2 >> f = @(x,y) 9 – x^2 – y^2 >> f(2,3) ans = -4 Create a function to emulate the mathematical behavior of the function defined by f(x, y) = 9 − x^2 − y^2 >> f = @(x,y) 9 – x^2 – y^2 >> f(2,3) ans = -4


Download ppt "User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide."

Similar presentations


Ads by Google