SUNY Geneseo Department of Mathematics
Wednesday, October 28
Math 230 01
Fall 2015
Prof. Doug Baldwin
functionname = @(arguments) expression
feval
function?feval( functionName, inputs )
@functionName
>> double = @(x) 2*x
double =
@(x)2*x
>> double( 3 )
ans =
6
>> feval( double, 3 )
ans =
6
>> feval( 'sqrt', 9 )
ans =
3
function [ twof ] = doublef( f, x )
%DOUBLEF Takes f and x as arguments and returns 2 f(x)
twof = 2 * f(x);
end
>> doublef( @sin, pi/2 )
ans =
2
>> doublef( @sqrt, pi/2 )
ans =
2.5066
>> doublef( sin, pi/2 )
{Error using sin
Not enough input arguments.}
function f = makeIncrementer( a )
f = @(x) x + a;
end
>> add2 = makeIncrementer( 2 )
add2 =
@(x)x+a
>> 17^2 % add2 can run whenever wanted; unrelated stuff first doesn’t hurt
ans =
289
>> add2( 3 )
ans =
5
>> add17= makeIncrementer( 17 )
add17 =
@(x)x+a
>> add17( 8 )
ans =
25
>> add2( 8 )
ans =
10
syms
!