Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

User Defined Functions in MATLAB part 2

882 views

Published on

Previous post we discussed what is a user defined function in MATLAB. This lecture we will discuss how to define a function and how to call the function in a script.
Components of a function is discussed in the previous lecture. The first statement in a function must be function definition.The basic syntax of a function definition is:

function[a, b, c]= basicmath(x,y)

Basically a function accepts an input vector, perform the operation, and returns a result. The sample function given has two input variables and three output variables. But we can also have functions without input or/and output.

Published in: Education
  • Be the first to comment

User Defined Functions in MATLAB part 2

  1. 1. User Defined Functions in MATLAB Shameer A Koya 1 Part 2
  2. 2. Function Definition O 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
  3. 3. 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
  4. 4. Example O Another example is a function to draw unit circle. 4
  5. 5. 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
  6. 6. 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
  7. 7. Example O 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
  8. 8. 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;
  9. 9. 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 arguments O For example consider the function basicmath >> [a,b,c] = basicmath (2,3) a = 5 b = -1 c = 6 9
  10. 10. 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 function result = dist2 (ax, ay, bx, by); % Write out result. fprintf('The distance between points a and b is %fn',result); 10
  11. 11. 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

×