Docsity

MATLAB Functions: Understanding Inputs, Outputs, and User-Defined Functions - Prof. Maria , Study notes of Engineering

Toggle sidebar
bg1
4/13/2009
1
ENGR 1731
Computing for Engineers
Georgia Southern University
Mechanical and Electrical En
g
ineerin
g
Technology
Spring 2009
Prof.๎˜ƒ๎˜ƒAlbaโ€Flores
โ€Functions
MATLAB๎˜ƒFunctions
โ€ขA๎˜ƒfunction๎˜ƒis๎˜ƒa๎˜ƒprogram๎˜ƒthat๎˜ƒcan๎˜ƒbe๎˜ƒcalled๎˜ƒ
iid th
i
ns
id
e๎˜ƒano
th
er๎˜ƒprogram.๎˜ƒ
โ€ขMATLAB๎˜ƒhas๎˜ƒmany๎˜ƒ๎˜ƒbuilt๎˜ƒin๎˜ƒfunctions๎˜ƒ๎˜ƒthat๎˜ƒcan๎˜ƒ
be๎˜ƒcalled๎˜ƒto๎˜ƒcalculate๎˜ƒarithmetic,๎˜ƒlogarithmic,๎˜ƒ
trigonometric๎˜ƒand๎˜ƒspecial๎˜ƒfunctions.
โ€ข
Alist of functions available in MATLAB is given
A
๎˜ƒ
list
๎˜ƒ
of
๎˜ƒ
functions
๎˜ƒ
available
๎˜ƒ
in
๎˜ƒ
MATLAB
๎˜ƒ
is
๎˜ƒ
given
๎˜ƒ
next
pf3
pf4
pf5
pf8

Partial preview of the text

Download MATLAB Functions: Understanding Inputs, Outputs, and User-Defined Functions - Prof. Maria and more Study notes Engineering in PDF only on Docsity!

ENGR 1731

Computing for Engineers

Georgia Southern University Mechanical and Electrical Engineering Technology

Spring 2009

Prof. Albaโ€Flores

โ€ Functions

MATLAB Functions

โ€ข A function is a program that can be called

ii nsideid anotherth program.

โ€ข MATLAB has many built in functions that can

be called to calculate arithmetic, logarithmic,

trigonometric and special functions.

โ€ข A list of functions available in MATLAB is givenA list of functions available in MATLAB is given

next

Common Matlab Built in Functions

sqrt(x) โ€“ Square root function exp(x)p( ) โ€“ Exponentialp function log(x) โ€“ Natural Logarithm log10(x) โ€“ Common logarithm cos(x) โ€“ Cosine function sin(x) โ€“ Sine function tan(x) โ€“ Tangent function acos(x) ( ) โ€“ Inverse cosine ffunction asin(x) โ€“ Inverse sine function atan(x) โ€“ Inverse tangent function

Function Calling

  • In order to execute a function we just put the name of the function and assign its result to a variable:of the function and assign its result to a variable:

y = tan(x);

z = cos(x) โ€“ sin(x);

Function calls

User Defined Functions

  • Any program written in MATLAB can be converted to a function so that we can call that program inside ina function so that we can call that program inside in another program.
  • This is very convenient because it makes the programs more efficient and smaller
  • โ€ข The conditions needed to convert a program into aThe conditions needed to convert a program into a function are given next.

User Defined Functions (cont.)

  • We need to specify a unique name to the function
  • We need to specify the list of input variables (or arguments) needed by the function
  • We need to specify the list of output variables that are returned by the function.

User Defined Functions (cont.)

  • Function Definition Syntax

function [Output Variable List] = function name (Input Variable list) endend Description

function [out1, out2, ...] = funname (in1, in2, ...) defines function funname that accepts inputs in1, in2, etc. and returns outputs out1, out2, etc. All functions should end with the instruction end

You add new functions to the MATLAB vocabulary by expressing themYou add new functions to the MATLAB vocabulary by expressing them in terms of existing functions. The existing commands and functions that compose the new function reside in a text file ( Mโ€file) funname.m

Function Definition

Example 1

function factorial = fac(n) ffactorial t i l = 11 ; for i = 1 : n factorial = factorial * i; end end

Function name: fac Output variable: factorial Input variable: n

Function Definition

Example 3

We can create a Matlab file called stat.m that defines a new function called stat that calculates the mean and standard t d d d d eviationi ti off a vector.t

function [mean, stdev] = stat(x) n = length(x); mean = sum(x)/n; stdev = sqrt(sum(( x โ€“ mean).^2/n)); end

Function name: stat Output variables: mean, stdev Input variable: x