SUNY Geneseo Department of Mathematics
Friday, November 7
Math 230 01
Fall 2014
Prof. Doug Baldwin
function [ y ] = piecewise( x )
if x < 0
y = x;
else
y = 2 * x;
end
>> f = @(x)2*x;
>> f(4)
ans =
8
>> g = @(x)if x < 0 x else 2 * x end;
g = @(x)if x < 0 x else 2 * x end;
|
{Error: Illegal use of reserved keyword "if".
}
>> piecewise( -3 )
ans =
-3
>> g = @piecewise;
>> g( -3 )
ans =
-3
If
Statementsif condition
action
end
if condition
action1
else
action2
end
if-elseif