11
User Defined Functions in MATLAB Shameer A Koya 1 Part 2

User Defined Functions in MATLAB part 2

Embed Size (px)

Citation preview

Page 1: User Defined Functions in MATLAB part 2

User Defined Functions in MATLAB

Shameer A Koya1

Part 2

Page 2: User Defined Functions in MATLAB part 2

Function DefinitionO function[a, b, c]= basicmath(x,y)O The first statement in a function must be function

definition.O Components of a function is discussed in the

previous lecture.O Basically a function accepts an input vector, perform

the operation, and returns a result.O The sample function given has two input variables

and three output variables.O But we can also have functions without input or/and

output.

2

Page 3: User Defined Functions in MATLAB part 2

Functions with no Input and Output

O Under some special occasions we may need some functions without any input or output.

O An example is a function used to clear workspace while writing code. It clears command window, workspace from all variables and closes all figures. O Just a simple code, but useful.

O Tanks to Jakub Mrowka

3

function cll() clc evalin('base', 'clear all') close all end

Page 4: User Defined Functions in MATLAB part 2

ExampleO Another example is a function to draw unit

circle.

4

Page 5: User Defined Functions in MATLAB part 2

Functions with only Inputs(No Output)

O Some functions will take some input arguments but will not return any output arguments.

O Functions to plot some shapes or curves and functions to display some message are examples.

5

Page 6: User Defined Functions in MATLAB part 2

Function with one Output

O The function have only one out put you don’t need to put it inside the square brackets [ ]

O Example:O The function in a file named average.m that accepts

an input vector, calculates the average of the values, and returns a single result.

function y = average(x) if ~isvector(x) error('Input must be a vector') end y = sum(x)/length(x); end

6

Page 7: User Defined Functions in MATLAB part 2

ExampleO The function FACTORIAL(N) will find the

factorial of input N, i.e. the product of all the integers from 1 to N.

function f = factorial(n)

if (length(n)~=1) | (fix(n) ~= n) | (n < 0)

error('N must be a positive integer');

end

f = prod(1:n);

7

Page 8: User Defined Functions in MATLAB part 2

Functions with more Outputs

O When there are more than one output arguments put them in a square bracket.

O Example

8

function [a, b, c] = basicmath (x, y) % BASICMATH Basic Mathematical function % algeb(x,y) is a sample matlab function to perform basic mathematical % operation of input variables x and y. % output of the function are sum, difference and product of the input arguments. a = x+y; b = x-y; c = x*y;

Page 9: User Defined Functions in MATLAB part 2

Calling a user-defined function

O A function can be called from the command window or inside a script or function.

O To run a function in command window, just type the name of comment with proper input and output argumentsO For example consider the function basicmath

>> [a,b,c] = basicmath (2,3)a = 5b = -1c = 6

9

Page 10: User Defined Functions in MATLAB part 2

Calling a user-defined function …O Consider the function to calculate distance between two

points.function distance = dist2(x1, y1, x2, y2)distance = sqrt((x2-x1).^2 + (y2-y1).^2);

O This function can be called in another script.

% Get input data.ax = input('Enter x value of point a: ');ay = input('Enter y value of point a: ');bx = input('Enter x value of point b: ');by = input('Enter y value of point b: ');

% Evaluate functionresult = dist2 (ax, ay, bx, by);

% Write out result.fprintf('The distance between points a and b is %f\n',result);

10

Page 11: User Defined Functions in MATLAB part 2

Calling a user-defined function …

O The function to be called in a script can be included in the same script.

r = input('Enter radius of the sphere: ');vol = volume_sphere(r) % Compute the volume. xmin = 0; xmax = pi; ….…..function y = volume_sphere(x) y = (4/3)*pi.*(radius^3);end

11