Problem Defining Function (Time Shift)

I have a function y1 I coded in matlab as such:
y1 = zeros(size(t));
y1(t<= T/4) = t(t<=T/4);
y1(t> T/4 & t < T/2) = T2 - t(t> T/4 & t < T/2);
y1(t > T/2 & t < 3*T/4) = t(t > T/2 & t < 3*T/4) - T/2;
y1(t>=3*T/4) = T - t(t>=3*T/4);
plot(t,y1)
title("function Λ_0")
I want to create a new function using this, which will basically be y1(t)+y1(t-T) + y(t-2T). So there needs to be a time shift created to get y1(t-T). However, I don't know how to create this besides for re-defining t as t = t-T and t = t - 2T and recalculating y1 for the new t's.
Is there a way to do this? I tried defining the above code as a function but it says "not enough input arguments for the line marked by ***:
function [func] = trigfunc(t,T)
if t <= T/4 (***)
func = t;
elseif t < T/2
func = T/2 - t;
elseif t < 3*T/4
func = t - T/2;
else
func = T-t;
end

1 comentario

When you call the function trigfunc, you must pass two input arguments, t and T. For instance:
yy = trigfunc(0,1);

Iniciar sesión para comentar.

Respuestas (2)

William Rose
William Rose el 12 de Dic. de 2021
@selin oktay, in your first section of code, I think you meant to write "T/2" not T2.
In your second sement of code, add "end" to end function trigfunc.
With trigfunc() defined as above, I get the following output. Does this graph look like like what you want from trigfunc()?
t=0:.01:1;
T=0.5;
for i=1:length(t), y1(i)=trigfunc(t(i),T); end
plot(t,y1,'-r.'); xlabel('t'); ylabel('y1');
You said you want to evaluate y1(t)+y1(t-T) + y(t-2T). Since y1 was defined in your initial post as function trigfunc(), "y1(t)" makes no sense, because it has no second argument, as @Benjamin correctly noted. I think maybe you have not communicated exactly what you have in mind for y1.

1 comentario

y=trigfunc(t,0)+trigfunc(t,T) + trigfunc(t,2*T)
will work. Is it really what you want? If we keep T=0.5 and t=0:.01:1, I do
T=0.5;
for i=1:length(t)
y1(i)=trigfunc(t(i),0)+trigfunc(t(i),T)+trigfunc(t(i),2*T);
end
plot(t,y1,'-r.'); xlabel('t'); ylabel('y1');
which produces
If I repeat with T=0.2, I get
Is this what you want for y=y1(t,0)+y1(t,T)+y1(t,2T)? I kind of doubt it. If it's not, provide a picture, or a more clear description in words, or better yet both, of what you do want.
function trigfunc(), as you have defined it, does not do what you want, if t is a vector. To see that this is true, do
t=0:.01:1;
y1=trigunc(t,0.5); plot(t,y1);
You might want to rewrite trigfunc() so it does do what you want, whether t is a vector or a scalar.

Iniciar sesión para comentar.

William Rose
William Rose el 13 de Dic. de 2021
Here is a rewrite of your function that works like yours worked for scalat t, but it also works if t is a vector. This makes it easier to use.
function func = trigfunc(t,T)
func=zeros(size(t));
for i=1:length(t)
if t(i) <= T/4
func(i) = t(i);
elseif t(i) < T/2
func(i) = T/2 - t(i);
elseif t(i) < 3*T/4
func(i) = t(i) - T/2;
else
func(i) = T-t(i);
end
end
end
If you pass it a vector of times, you get a vector of altered times back. For example, you can do
t=0:.04:1; T=0.8; plot(t,trigfunc(t,T),'-r.'); ylabel('trigfunc(t)');
which makes a plot.
And with this modification, you can use trigfunc() with a vector as an argument to a trig function. For example:
t=0:.025:1; T=1;
plot(t,sin(2*pi*trigfunc(t,T)),'-r.', t,cos(2*pi*trigfunc(t,T)),'-b.');
legend('sin(trigfunc(t,1))','cos(trigfunc(t,1))');
which makes the plot below:

Categorías

Más información sobre Performance and Memory en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 12 de Dic. de 2021

Respondida:

el 13 de Dic. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by