How to plot graphs with the specific time domain

Hi, i am trying to plot 3 graphs with the specific time domian. Here's my code:
t = -2*pi:0.01:2*pi;
function y = f(t)
y = (abs(t)./t+1)/2 - 2*((abs(t-pi)./(t-pi)+1)/2) + (abs(t-2*pi)./(t-2*pi)+1)/2;
endfunction
plot(t = 0:2*pi,f(t))
title("f(t)");
plot(t = -2*pi:0,-f(-t))
title("-f(-t)");
plot(t = 0:2*pi,-f(t+pi))
title("-f(t+T)");
Here's the error i got:
Below are the results that I would like to obtain. Please help. Thanks!

Respuestas (1)

Star Strider
Star Strider el 7 de Abr. de 2021
I am not certain what you want to do.
Try this:
figure
subplot(3,1,1)
plot(t((t>=0) & (t<= 2*pi)),f(t((t>=0) & (t<= 2*pi))))
ylim([-1 1])
title("f(t)");
subplot(3,1,2)
plot(t((t>=-2*pi) & (t<=0)),-f(-t((t>=-2*pi) & (t<=0))))
ylim([-1 1])
title("-f(-t)");
subplot(3,1,3)
plot(t((t>=0) & (t<= 2*pi)),-f(t((t>=0) & (t<= 2*pi))+pi))
ylim([-1 1])
title("-f(t+T)");
hold off
Experiment to get the results you want. Remember that changes in the ‘t’ vector as the independent variable must be the same in the argument to ‘f’.

6 comentarios

Jinquan Li
Jinquan Li el 7 de Abr. de 2021
I tried it but no graphs came out. here's what i just tried:
t = -2*pi:0.01:2*pi;
function y = f(t);
y = (abs(t)./t+1)/2 - 2*((abs(t-pi)./(t-pi)+1)/2) + (abs(t-2*pi)./(t-2*pi)+1)/2;
subplot(3,1,1)
fplot(f(t((t>=0) & (t<= 2*pi))))
ylim([-1 1])
title("f(t)");
subplot(3,1,2)
fplot(-f(-t((t>=-2*pi) & (t<=0))))
ylim([-1 1])
title("-f(-t)");
subplot(3,1,3)
fplot(-f(t((t>=0) & (t<= 2*pi))+pi))
ylim([-1 1])
title("-f(t+T)");
hold off
end
Star Strider
Star Strider el 7 de Abr. de 2021
I did not use fplot in my code.
Copy and paste my code to a new script in your Editor, and run it. It will work.
Jinquan Li
Jinquan Li el 7 de Abr. de 2021
i tried this. No graphs came out:
t = -2*pi:0.01:2*pi;
function y = f(t);
y = (abs(t)./t+1)/2 - 2*((abs(t-pi)./(t-pi)+1)/2) + (abs(t-2*pi)./(t-2*pi)+1)/2;
figure
subplot(3,1,1)
plot(t((t>=0) & (t<= 2*pi)),f(t((t>=0) & (t<= 2*pi))))
ylim([-1 1])
title("f(t)");
subplot(3,1,2)
plot(t((t>=-2*pi) & (t<=0)),-f(-t((t>=-2*pi) & (t<=0))))
ylim([-1 1])
title("-f(-t)");
subplot(3,1,3)
plot(t((t>=0) & (t<= 2*pi)),-f(t((t>=0) & (t<= 2*pi))+pi))
ylim([-1 1])
title("-f(t+T)");
hold off
end
This code:
t = -2*pi:0.01:2*pi;
% function y = f(t)
% y = (abs(t)./t+1)/2 - 2*((abs(t-pi)./(t-pi)+1)/2) + (abs(t-2*pi)./(t-2*pi)+1)/2;
% endfunction
f = @(t) (abs(t)./t+1)/2 - 2*((abs(t-pi)./(t-pi)+1)/2) + (abs(t-2*pi)./(t-2*pi)+1)/2;
figure
subplot(3,1,1)
plot(t((t>=0) & (t<= 2*pi)),f(t((t>=0) & (t<= 2*pi))))
ylim([-1 1])
title("f(t)");
subplot(3,1,2)
plot(t((t>=-2*pi) & (t<=0)),-f(-t((t>=-2*pi) & (t<=0))))
ylim([-1 1])
title("-f(-t)");
subplot(3,1,3)
plot(t((t>=0) & (t<= 2*pi)),-f(t((t>=0) & (t<= 2*pi))+pi))
ylim([-1 1])
title("-f(t+T)");
hold off
ran for me without error and produced:
I have no idea what the problem is with your running it.
Jinquan Li
Jinquan Li el 8 de Abr. de 2021
I see. thank you so much!
Star Strider
Star Strider el 8 de Abr. de 2021
My pleasure!

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Etiquetas

Preguntada:

el 7 de Abr. de 2021

Comentada:

el 8 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by