MATLAB: Writing and plotting function with multiple conditions

functionMATLABmatlab functionplotplotting

Imagine I have a function like this:
How can I write this function in matlab and plot it between 0 and 1? How can I also compute of this function?
I want to have something like f=@(x) my_func

Best Answer

One possibility:
f = @(x) 9/5*x*(0<=x & x<1/2)+9/5*(1-x)*(1/2<=x & x<=1);
e.g.
f(0.1)
ans =
0.1800
>> f(0.75)
ans =
0.4500