Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Successfully reported this slideshow.

Like this presentation? Why not share!

- User defined Functions in MATLAB Pa... by Shameer Ahmed Koya 1136 views
- User Defined Functions in MATLAB Pa... by Shameer Ahmed Koya 785 views
- MATLAB Programming - Loop Control P... by Shameer Ahmed Koya 907 views
- User Defined Functions by praveenjigajinni 1014 views
- Anonymous and Inline Functions in M... by Shameer Ahmed Koya 1248 views
- Matlab Script - Loop Control by Shameer Ahmed Koya 1049 views

882 views

Published on

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

No Downloads

Total views

882

On SlideShare

0

From Embeds

0

Number of Embeds

359

Shares

0

Downloads

40

Comments

0

Likes

1

No embeds

No notes for slide

- 1. User Defined Functions in MATLAB Shameer A Koya 1 Part 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. 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. Example O Another example is a function to draw unit circle. 4
- 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. 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. 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. 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. 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. 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. 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

No public clipboards found for this slide

Be the first to comment