【问题标题】:How to make a ramp function that starts in point a and finishes in point b. [a,b]. And with value 0 outside the interval如何制作从 a 点开始并在 b 点结束的斜坡函数。 [a,b]。并且在区间外的值为 0
【发布时间】:2020-02-04 13:39:38
【问题描述】:

我想建立一个从点 a 开始的斜坡函数 并在 b 点结束。 [a,b]

我已经构建了一个斜坡函数的代码,它从 0 开始并在点 b.[0,b] 结束。

这是代码:

hold on
b =2  % time in wich the ramp finish
a=1  %time in which the ramp start
t = [0:0.01:15]';  %timespan of the ramp function
for impAmp = [3 5 10]  %height of the ramp 
function
rampOn = @(t) t<=b;
ramp =@(t) t.*impAmp.*rampOn(t);
plot(t,ramp(t))
end

但我想定义区间 [a,b] 中定义的斜坡函数的新代码。并且在区间外的值为 0。

我已经尝试过这段代码。但它修改了所有的斜坡函数。

hold on
b =2 % time in wich the ramp finish
a=1 %time in which the ramp start
t = [0:0.01:15]';%timespan of the ramp function
for impAmp = [3 5 10]%height of the ramp function
rampOn = @(t) a<t<=b;
ramp =@(t) t.*impAmp.*rampOn(t);
plot(t,ramp(t))
end

【问题讨论】:

    标签: matlab for-loop plot


    【解决方案1】:

    作为一个函数,你有:

    function y=ramp(x,a,b,h)
        y = h/(b-a)*(x-a).*(x>a).*(x<b);
    end
    

    作为函数句柄,

    f = @(x,a,b,h) h/(b-a)*(x-a).*(x>a).*(x<b);
    

    无论哪种方式,定义参数ab和高度h,并根据需要使用函数:

    a = 1; 
    b = 4;
    h = 7;
    x = 0:0.1:5;
    y = ramp(x,a,b,h);
    plot(x,y)
    
    a = 2; 
    b = 2.5;
    h = 10;
    x = 0:0.1:5;
    plot(x,ramp(x,a,b,h))
    

    【讨论】:

      猜你喜欢
      • 2013-08-08
      • 2016-01-13
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      相关资源
      最近更新 更多