没有预定义间隔的智能功能(Piece wise function without predefined intervals)

编程入门 行业动态 更新时间:2024-10-05 05:23:22
没有预定义间隔的智能功能(Piece wise function without predefined intervals)

我想创建一个.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; 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.

最满意答案

这个功能?

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

enter image description here

更多推荐

本文发布于:2023-08-07 11:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间隔   功能   智能   Piece   intervals

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!