SUNY Geneseo Department of Mathematics
Wednesday, January 25
Math 230 02
Spring 2017
Prof. Doug Baldwin
Let me know if you prefer a name other than what’s on College rosters, etc.
Output vs input variables?
Math has a notion of “input variable” for a function, e.g., in f(x) = x + 3, x represents the “input” value on which the function operates.
For example, the 5 in f(5)
Mathematical functions also implicitly have an output, for example in evaluating f(5) you would calculate that f(5) = 5 + 3 = 8, i.e., the result, or “output,” is 8
Matlab makes you name output as an explicit variable, e.g., output_args
Then you define a function’s output value via a statement of the form output_args = ...
We saw a quick example, sin
, last time
Almost every mathematical function is available in Matlab, including
sin
(radians) vs sind
(degrees))asin
, acos
, atan
, etc.)exp
(ex), log
(natural log), log10
)sqrt
)abs
)Some examples
>> sin( 17.9 ) ans = -0.8132 >> asin( -0.8132 ) ans = -0.9496 >> asind( 1 ) ans = 90
Example: Define a pair of functions, both of which take the polar coordinates of a point as arguments, and one of which returns the corresponding x Cartesian coordinate, the other returns the y Cartesian coordinate.
Helpful ideas
function .... end
Algorithm
Matlab definitions of the functions and some example uses
function [ xCoord ] = f( r, theta )
% Compute x coordinate corresponding to polar coordinates
% r and theta
xCoord = r * cos( theta );
end
>> f( 1, 0 ) ans = 1 >> f( 1, 3.14159 ) ans = -1.0000
function [ yCoord ] = g( r, theta )
% Compute the y coordinate for polar point (r, theta).
yCoord = r * sin( theta );
end
>> g( 1, 3.14159 / 2 ) ans = 1.0000 >> g( 1, 0 ) ans = 0
It turns out Matlab functions can return more than one result, so you could define both parts of this calculation in a single function:
function [ x, y ] = cartesian( r, theta )
% Compute Cartesian coordinates for polar point (r,theta).
x = r * cos( theta );
y = r * sin( theta );
end
Receiving both results requires some new syntax when calling the function though:
>> cartesian( 1, 0 ) ans = 1 >> [ x, y ] = cartesian( 1, 0 ) x = 1 y = 0 >> [ piglet, wombat ] = cartesian( 1, 0 ) piglet = 1 wombat = 0
See handout for instructions
Lab day — work on Lab 1