我想创建一个.m文件,它代表一个明智的函数,并返回一个包含所有离散值计算的向量。
为了更清楚一点,我想要一个函数(我已经命名为Iapp并且是时间相关的,所以Iapp(t))在前100秒返回零,然后在100-120s时返回0.5,然后在120-220s时再次返回0对于220-240秒为0.5 + 0.2并且继续。
我知道一个明智的功能可以使用逻辑索引来定义,但我的问题是我想要的功能的时间间隔不是预定义的。 所以我不知道逻辑索引是如何工作的......如果时间间隔不是120的倍数,它就不起作用。
我已经尝试了以下内容:
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但算法并不总是像它应该那样工作。 关于如何定义这个功能的任何想法? 请注意,我还希望能够以某种方式为tspan提供标量值,并使函数仅返回标量值。
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; endBut 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.
最满意答案
这个功能?
x=linspace(0,600,1000); y=Iapp(x); plot(x,y) function y=Iapp(t) r=mod(t,120); c=floor(t/120); VAL1=0; VAL2=0.5 + 0.2*c; y=VAL1.*(r<=100) + VAL2.*(r>100); end
This function?
x=linspace(0,600,1000); y=Iapp(x); plot(x,y) function y=Iapp(t) r=mod(t,120); c=floor(t/120); VAL1=0; VAL2=0.5 + 0.2*c; y=VAL1.*(r<=100) + VAL2.*(r>100); end更多推荐
发布评论