How to plot a piecewise periodic function? Please Help
Show older comments
f(x)=2*sqrt(x) for 0<=x<=1 and f(x)=3-(x) for 1<=x<=3
How would I plot this function on the range -9<=x<=9? The questions states to make use of the "floor function".
Please Help
2 Comments
Azzi Abdelmalek
on 20 Nov 2014
Your function is defined from 0 to 3, what about the other ranges?
Accepted Answer
More Answers (1)
Sally Al Khamees
on 21 Feb 2017
If you have R2016b and the Symbolic Math Toolbox installed, you can use the piecewise function to recreate this example:
syms y(x) a(x) b(x);
y(x) = piecewise(0<=x <1, 2*sqrt(x), 1 <= x <= 3, 3-x);
interval = [-6 6];
pw=y;
for i=1:diff(interval/6)
a(x)= piecewise(i*3<=x<1+(i*3),2*sqrt(x-3*i),1+(i*3)<=x<=3+(i*3),3-(x-3*i));
b(x)= piecewise(i*-3<=x<1+(i*-3),2*sqrt(x+3*i),1+(i*-3)<=x<=3+(i*-3),3-(x+3*i));
pw = [pw a b];
end
pw
fplot(pw,interval)


You can read more about the piecewise function in Symbolic Math Toolbox here https://www.mathworks.com/help/symbolic/piecewise.html
1 Comment
Puech gabriel
on 2 Aug 2017
Hi,
Thanks for giving this code. Here are some parameters added to your code and using just one function
syms a(x);
T = pi; % period value
i = -2; % number of periods, must be integer!
interval = [i*T -i*T];
pw = [];
while i<=diff(interval/(2*T))
a(x)= piecewise(i*T<=x<1+(i*T),2*sqrt(x-T*i),1+(i*T)<=x<=3+(i*T),3-(x-T*i),3+(i*T)<=x<=T+(i*T),0); %+diff(interval/6)-floor(diff(interval/6))
i = i +1;
pw = [pw a ]; % concatenation des periodes
end
pw
fplot(pw,interval)
Would it have a way of preallocating the pw symfun matrix before the loop?
Kind regards, Gabriel
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
