简体   繁体   English

在 MATLAB 中绘制分段 function

[英]Plotting a piecewise function in MATLAB

I've been trying to plot a piecewise function:我一直在尝试 plot 分段 function:

y(t)=a*sin(2*pi *f *t) for 0 < t <= 1/(2f) y(t)=a*sin(2*pi *f *t) 对于 0 < t <= 1/(2f)

y(t)=0 for 1/(2f) < t < 1/f y(t)=0 对于 1/(2f) < t < 1/f

ranging from t=0 to t=3.范围从 t=0 到 t=3。

Can anyone help me plot this without using the "piecewise" command and instead creating a function possibly with for loops and if statements?任何人都可以在不使用“分段”命令的情况下帮助我 plot 而是使用 for 循环和 if 语句创建 function 吗?

in matlab, usually plots are done by computing the x/y values in a discretized grid.在 matlab 中,通常通过计算离散网格中的 x/y 值来绘制绘图。

f=2;
a=1;
t=0:0.01:3;
y=zeros(size(t));
y(t<=1/(2*f))=a*sin(2*pi*f*t(t<=1/2/f));
plot(t,y)

another way to create such a piece-wise function is to create a dedicate function or anonymous function to compute this in real time.创建这种分段 function 的另一种方法是创建专用 function 或匿名 function 来实时计算。 For example例如

y=@(t,f,a) (t<=1/(2*f) & t>=0).*sin(2*pi*f*t)*a;
plot(t,y(t,f,a))

I was trying sth like this:我正在尝试这样的事情:

function [rate] = y(a,f,t) function [速率] = y(a,f,t)
for t = (0:3)对于 t = (0:3)
if t <= (1 / (2 * f))如果 t <= (1 / (2 * f))
rate = a * sin(2 * pi * f * t);率 = a * sin(2 * pi * f * t);
else别的
rate = 0;率 = 0;
end结尾
end结尾
end结尾

and then call: plot (t, y(a,f,t)) to plot the graph.然后调用: plot (t, y(a,f,t)) 到 plot 图表。 Could you correct me if I'm wrong?如果我错了,你能纠正我吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM