Help Making a Piecewise Function Periodic
Afficher commentaires plus anciens
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);
Réponse acceptée
Plus de réponses (1)
Alexander
le 16 Juin 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 commentaires
Walter Roberson
le 16 Juin 2025
I tested R2024b, and the code posted by @Paul works fine, including modular division by a real number.
Steven Lord
le 16 Juin 2025
What does "doesn't work as expected" mean in this context?
- Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
- Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
- Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
Alexander
le 16 Juin 2025
I understand the problem. By default Matlab assumes the variable is a complex number. So, it is the modular division for complex numbers what is different.
Need to define argument to the periodic function as a real variable explicitly then it works.
syms t real;
Now I tried to define explicitly function argument as a real number, but plotting is still incorrect. My function is even and
periodic, so it is symmetric about y-axis. When doing modular division by
something goes wrong on the negative interval.
clear;clf;clc;
syms t real;
% Define the period and the range for x
L = pi; % Half-Period of the periodic function
tval = -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);
%plot(t,f(t_intervals))
plot(tval,f(mod(tval,2*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
Which produces incorrect plot:

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
le 16 Juin 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:

Catégories
En savoir plus sur Assumptions dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





