I want to make an .m file that represents a piece wise function and returns a vector with all the discrete values calculated.
To be a bit more clear, I want a function (which I have named Iapp and is time dependant, so Iapp(t)) that returns zero for the first 100s then returns 0.5 for 100-120s then again zero for 120-220s then 0.5+0.2 for 220-240s and that goes on.
I know a piece wise function can be defined using logical indexing, but my problem is that my time interval for which I want the function is not predefined. So I don't know how logical indexing could work...if the time interval is not a multiple of 120 it doesn't work.
I have tried the following:
function Vect_Iapp = Iapp_morceaux(tspan, h)
i = 1;
j = 1;
t = tspan(1):h:tspan(2);
while t(i) < tspan(2)
while(t(i)< (j*100 + (j-1)*20))
Iapp(i) = 0;
i = i + 1;
end
while (t(i)>j*100 && t(i) < j*100 + j*20)
Iapp(i) = 0.5 + j*0.2;
i = i + 1;
end
j = j + 1;
end
Vect_Iapp = Iapp;
end
But the algorithm does not always work like it should. Any ideas as to how this function could be defined? Note that I would also like to be able to somehow give a scalar value for tspan and make the function return just a scalar value back.