Help Making a Piecewise Function Periodic

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);

 采纳的回答

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 个评论

Note by the way that piecewise is the only function that permits using 0 <= t <= 1 kind of syntax at the user level (at least that I can think of.)
(There are some symbolic functions that can return those structures.)
Also, bear in mind that that even though z1(t) is periodic, it is not the periodic extension of z(t). Rather z1(t) is the periodic extension of the product of z(t) and a rectangular window from t = 0 to t = 2.5.
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.
You'll need to show the exact code you ran and the results obtained before going any further.

请先登录,再进行评论。

更多回答(1 个)

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 个评论

I tested R2024b, and the code posted by @Paul works fine, including modular division by a real number.
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.
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);
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:

请先登录,再进行评论。

类别

产品

版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by