Help Making a Piecewise Function Periodic

조회 수: 38 (최근 30일)
Connor Buternowsky
Connor Buternowsky 2022년 9월 24일
댓글: Alexander 2025년 6월 16일
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);

채택된 답변

Paul
Paul 2022년 9월 24일
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
Anoop Kiran
Anoop Kiran 2022년 10월 10일
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 2022년 10월 10일
You'll need to show the exact code you ran and the results obtained before going any further.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alexander
Alexander 2025년 6월 16일
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
Walter Roberson
Walter Roberson 2025년 6월 16일
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 2025년 6월 16일
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:

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by