I've been trying to plot a piecewise function:
y(t)=a*sin(2*pi *f *t) for 0 < t <= 1/(2f)
y(t)=0 for 1/(2f) < t < 1/f
ranging from t=0 to 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?
in matlab, usually plots are done by computing the x/y values in a discretized grid.
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. 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)
for t = (0:3)
if t <= (1 / (2 * f))
rate = a * sin(2 * pi * f * t);
else
rate = 0;
end
end
end
and then call: plot (t, y(a,f,t)) to plot the graph. Could you correct me if I'm wrong?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.