04-Functions-in-MATLAB-4UP

04-Functions-in-MATLAB-4UP - Function m-files...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Function m-files Workspaces/subfunctions Anonymous functions Functions in M ATLAB Dhavide Aruliah UOIT MATH 2070U c D. Aruliah (UOIT) Functions in MATLAB MATH 2070U 1 / 23 Function m-files Workspaces/subfunctions Anonymous functions Functions in M ATLAB 1 Function m-files 2 Function workspaces and subfunctions 3 Anonymous functions c D. Aruliah (UOIT) Functions in MATLAB MATH 2070U 2 / 23 Function m-files Workspaces/subfunctions Anonymous functions Functions in mathematics Function f : A B is mapping from domain A to range B Black box mapping input data to output data Functions necessary abstraction in calculus, algebra Functions useful also in programming Clean, efficient way to partition tasks into easier subtasks f Input Output c D. Aruliah (UOIT) Functions in MATLAB MATH 2070U 4 / 23 Function m-files Workspaces/subfunctions Anonymous functions Function functions Basic need in scientific computing to pass functions to functions e.g., to obtain a zero of function f ( x ) = x- cos ( x ) , MATLAB function fzero needs to accept function f as input argument Function handle : data type (denoted @ ) for functions (like pointer) Function function : MATLAB a function that operates on another function (i.e., accepts a function handle as an input argument) We will use certain function functions (e.g., fzero ) often f = @(z) z-cos(z); % Create anonymous function x = fzero(f,[0,1]) % Find zero of function f in [0,1] y = fzero(@cot,[1,2]) % Use handle of cotangent function (Confusing for now, but well see a lot of examples later. . . ) c D. Aruliah (UOIT) Functions in MATLAB MATH 2070U 5 / 23 Function m-files Workspaces/subfunctions Anonymous functions Function m-files First executable line: function definition function [ outputs ] = fname( inputs ) I Keyword function lower-case I outputs is list of output variables enclosed in brackets [ ] I inputs is list of input variables enclosed in parentheses ( ) I fname is name of function (follows rules for identifiers) Name of m-file is fname.m ( must match fname ) Input/output variables also called arguments c D. Aruliah (UOIT) Functions in MATLAB MATH 2070U 6 / 23 Function m-files Workspaces/subfunctions Anonymous functions Example: distance in polar coordinates Points P 1 & P 2 have polar coordinates ( r 1 , 1 ) and ( r 2 , 2 ) respectively Distance d (by law of cosines): d = q r 2 1 + r 2 2- 2 r 1 r 2 cos ( 1- 2 ) P 1 P 2 2 1 r 1 r 2 d c D. Aruliah (UOIT) Functions in MATLAB MATH 2070U 7 / 23 Function m-files Workspaces/subfunctions Anonymous functions Example: distance in polar coordinates function d = polardist(r1,theta1,r2,theta2) % POLARDIST computation of distance in polar coordinates...
View Full Document

This note was uploaded on 02/23/2010 for the course MATH 2070 taught by Professor Aruliahdhavidhe during the Spring '10 term at UOIT.

Page1 / 5

04-Functions-in-MATLAB-4UP - Function m-files...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online