matlab - 如何制作从 a 点开始并在 b 点结束的斜坡函数。[a,b]。并且在区间外的值为 0
问题描述
我想构建一个从 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
解决方案
作为一个函数,你有:
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);
无论哪种方式,定义参数a
和b
高度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))
推荐阅读
- c++ - C++ 中用于 Linux 的 NTP 客户端
- asp.net-core - ASP.NET Core 2.1 项目中的 Microsoft.Identity.Web
- postgresql - PostgreSQL:性能调优查询
- java - 在进行身份验证时注入 GoogleSignInClient 时出现空指针异常
- angular - 在 Pipe RxJS 中处理条件
- shell - Makefile:如果已运行任何构建步骤,则执行通知命令
- python - 如何指定在 PyTorch 中获取元素的轴
- asp.net - 线程池统计/性能计数器
- java - 如何解析 JSON 字符串数组?
- github - 如何在 GitHub Actions 中检查多个工作流程是否成功?