Help Making a Piecewise Function Periodic

35 visualizaciones (últimos 30 días)
Connor Buternowsky
Connor Buternowsky el 24 de Sept. de 2022
Comentada: Alexander el 16 de Jun. de 2025
So I am trying to plot a piecewise function where z(t) = {t , 0 <= t < 1
e^(-5(t-1)) , 1 <= t < 2.5
0 , else
When I run it, it just gives the one period even though mod(t,2.5) should be keeping everything OK. How do I fix this? (I want to make it periodic)
(Oh yeah, my interval is -2 <= t <= 12)
t1 = -2 : 0.01 : 12;
syms t z(t); % defining a symbolic variable
z(t) = (piecewise((mod(t,2.5)>=0)&(mod(t,2.5)<1),t, (mod(t,2.5)>=1)&(mod(t,2.5)<2.5), exp(-5.*(t-1)), 0));
z1 = z(t1);
figure
hold on
plot(t1, z1);

Respuesta aceptada

Paul
Paul el 24 de Sept. de 2022
HI Connor,
Something like this?
syms t real
z(t) = piecewise(0 <= t <= 1,t, 1 < t <= 2.5, exp(-5*(t-1)),0);
fplot(z(t),[-2 12])
z1(t) = z(mod(t,2.5));
fplot(z1(t),[-2 12])
tval = -2:.01:12;
plot(tval,z1(tval))
  4 comentarios
Anoop Kiran
Anoop Kiran el 10 de Oct. de 2022
Hi Paul,
I tried your code, unfortunately it doesn't result in the periodic behavior at every 2.5 interval like the second and third plots that you have.
Paul
Paul el 10 de Oct. de 2022
You'll need to show the exact code you ran and the results obtained before going any further.

Iniciar sesión para comentar.

Más respuestas (1)

Alexander
Alexander el 16 de Jun. de 2025
In the most recent R2024b Update 5 (24.2.0.2871072) version modular division by real number doesn't work as expected to plot periodic functions extentions. I tried the following approach and it worked. In short, do low level in special function.
clear;clf;clc;
% Define the period and the range for x
L = pi; % Half-Period of the periodic function
t = -3*L:L/100:3*L;
% Define the given function
fl1 = @(t) (pi/2.*(t >= -pi & t <-pi/2));
fl2 = @(t) (-t.*(t >= -pi/2 & t < 0));
fr1 = @(t) (pi/2.*(t >= pi/2 & t <pi));
fr2 = @(t) (t.*(t >= 0 & t < pi/2));
f = @(t) (fl1(t)+fl2(t)+fr1(t)+fr2(t));
g = @(t) (-L + ((-L+t)/(2*L)-floor((-L+t)/(2*L)))*(2*L));
t_intervals = g(t);
%figure;
plot(t,f(t_intervals))
hold on;
title('f(t)', 'Interpreter','latex');
xticks([-2*pi -pi 0 pi 2*pi])
xticklabels({'-2\pi','-\pi','0','\pi','2\pi'})
ylim([-.1 pi/2*1.1])
yticks([0 pi/4 pi/2])
yticklabels({'0','\frac{\pi}{4}','\frac{\pi}{2}'})
xlabel('t'); %ylabel('f(t)');
set(gca,'TickLabelInterpreter', 'latex');
set(gca,'fontsize',12)
grid on;
hold off
Which should produce
  6 comentarios
Walter Roberson
Walter Roberson el 16 de Jun. de 2025
L = pi; % Half-Period of the periodic function
tval = -3*L:L/100:3*L;
plot(tval,mod(tval,2*L));
Notice that this mod result is nowhere negative.
plot(tval,rem(tval,2*L));
rem() does produce negatives, but only for the negative input range.
It is an error to think that mod() will produce negative outputs when the divisor is positive.
Compare to
plot(tval,mod(tval+L,2*L)-L);
Alexander
Alexander el 16 de Jun. de 2025
Confirmed. I also cleaned my code, which produces correct plot for even function by modulo division. No need to declare a function argument as real number.
clear;clf;clc;
% Define the period and the range for x
L = pi; % Half-Period of the periodic function
t = -3*L:L/100:3*L;
% Define the given function
fl1 = @(t) (pi/2.*(t >= -pi & t <-pi/2));
fl2 = @(t) (-t.*(t >= -pi/2 & t < 0));
fr1 = @(t) (pi/2.*(t >= pi/2 & t <pi));
fr2 = @(t) (t.*(t >= 0 & t < pi/2));
f = @(t) (fl1(t)+fl2(t)+fr1(t)+fr2(t));
plot(t,f(mod(t+L,2*L)-L));
hold on;
title('f(t)', 'Interpreter','latex');
xticks([-2*pi -pi 0 pi 2*pi])
xticklabels({'-2\pi','-\pi','0','\pi','2\pi'})
ylim([-.1 pi/2*1.1])
yticks([0 pi/4 pi/2])
yticklabels({'0','\frac{\pi}{4}','\frac{\pi}{2}'})
xlabel('t'); %ylabel('f(t)');
set(gca,'TickLabelInterpreter', 'latex');
set(gca,'fontsize',12)
grid on;
hold off;
The resulting plot is symmetric about y-axis:

Iniciar sesión para comentar.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by