How to plot a piecewise periodic function? Please Help

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 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 11 月 20 日
Your function is defined from 0 to 3, what about the other ranges?
john
john 2014 年 11 月 20 日
編集済み: john 2014 年 11 月 20 日
It is a periodic function with period 3, so it would just repeat itself for x<0 and x>3.

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2014 年 11 月 20 日
編集済み: Star Strider 2014 年 11 月 20 日

1 投票

I don’t understand where the floor function comes into it. This is how I would code it:
f = @(x) [2*sqrt(x).*(0<=x & x<1) + (3-x).*(1<=x & x<=3)];
x = linspace(-9,9);
figure(1)
plot(x, f(x))
grid
Note that your function does not exist for x<0 and x>3, so while the plot defaults to zero outside that region, I would only plot it for [0,3].
EDIT —
‘Periodic function’ ... this works:
f = @(x) [2*sqrt(x).*(0<=x & x<1) + (3-x).*(1<=x & x<=3)];
x = linspace(0,3);
intvl = [-6 6];
pfx = repmat(f(x),1,diff(intvl)/3);
px = linspace(intvl(1),intvl(2),length(pfx));
figure(1)
plot(px, pfx)
grid
I had to revise my code every time you updated your Question. This (EDIT) code reproduces the plot you posted. I did the function in a one-line anonymous function for simplicity and efficiency. It shouldn’t be difficult for you to follow its logic and write a function .m-file to do the same calculation.

6 件のコメント

john
john 2014 年 11 月 20 日
How would I make this periodic with period 3? I need to get a graph that looks like this:
john
john 2014 年 11 月 20 日
Thank you very much.
Star Strider
Star Strider 2014 年 11 月 20 日
My pleasure!
Waranga Ratnayake
Waranga Ratnayake 2018 年 9 月 7 日
How to add a 0, elsewhere in to those functions?
Diptanshu De
Diptanshu De 2021 年 6 月 10 日
編集済み: Diptanshu De 2021 年 6 月 10 日
How to change my code if my function is
f(x) = -x ; -5<=x<0
x^2+1; 0<=x<5
x = linspace(-6,6);
f = zeros(size(x));
mask = -5 <= x & x < 0;
f(mask) = -x(mask);
mask = 0 <= x & x < 5;
f(mask) = x(mask).^2 + 1;
plot(x, f)

サインインしてコメントする。

その他の回答 (1 件)

Sally Al Khamees
Sally Al Khamees 2017 年 2 月 21 日

0 投票

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 件のコメント

Puech gabriel
Puech gabriel 2017 年 8 月 2 日
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

サインインしてコメントする。

カテゴリ

質問済み:

2014 年 11 月 20 日

コメント済み:

2021 年 6 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by