SUNY Geneseo Department of Mathematics
Friday, March 25
Math 230 02
Spring 2016
Prof. Doug Baldwin
feval
calls a function via a handle with inputssqrt
function>> sqrt( 4 )
ans =
2
>> s = @sqrt
s =
@sqrt
>> s
s =
@sqrt
>> s(4)
ans =
2
>> s(2)
ans =
1.4142
>> s(-2)
ans =
0.0000 + 1.4142i
>> feval( s, 4 )
ans =
2
>> s + 3
{Undefined operator '+' for input arguments of type 'function_handle'.}
>> s = 3
s =
3
>> s(4)
{Index exceeds matrix dimensions.}
>> s + 3
ans =
6
feval
to call a handlesumFirst(f)
= f(1) +
f(2) + f(3)function [ y ] = sumFirst( f )
%SUMFIRST computes y as f(1) + f(2) + f(3)
y = f( 1 ) + f( 2 ) + f( 3 );
end
sumFirst
>> % add up square roots of 1, 2, 3
>> sumFirst( @sqrt )
ans =
4.1463
>> % add up (sqrt(x) + 1) for x = 1, 2, 3
>> sumFirst( @(x) sqrt(x) + 1 )
ans =
7.1463
>> % add up fib(1), fib(2), fib(3)
>> sumFirst( @fib )
ans =
4
1e-10
in Matlabif
statement to give h a small constant
value if x is close to 0, and to calculate h from x if x is not close to 0