Using repmat to plot a periodic function

In summary: I did something like this:f = @(x)[1.*(-pi<x & x<0) + 0.*(0<x & x<pi)];x = linspace(-pi,pi);intvl = [-9 9];pfx = repmat(f(x),1,diff(intvl)/3);px = linspace(intvl(1),intvl(2),length(pfx));plot(px, pfx);hold on; grid on; grid minor;xticks([-4*pi:1:4*pi]);xticklabels({'-4\pi', '-3\pi',
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,116
2,691
Consider the following function:
$$f(x) = \begin{cases}
1 & \text{when} & -\pi<x<0\\
0 & \text{when} & 0<x<\pi
\end{cases}$$
Beyond ##-\pi## and ##\pi##, the function just repeats itself; it is periodic.

I want to plot this function for values beyond ##-\pi## and ##\pi##. The graph should look something like this:

1597848733284.png

This answer to a similar question in MATLAB central says I have to use repmat, but I can't understand how to use it in my specific case.

Any help is appreciated.
 
Physics news on Phys.org
  • #2
First try running the example as is.

Notice they define an f function via the @(x) scheme

Repmat then replicates the function f so you need to define a similar function for your plot maybe something like this (a guess):

f = @(x) [ 0 * (sin(x)<=0) + 1 * (sin(x)>0) ]

where x is in radians and the sin(x)<=0 and sin(x)>0 factors control the 0 1 output that you're looking for. I've not tested this though.
 
  • #3
jedishrfu said:
First try running the example as is.

Notice they define an f function via the @(x) scheme

Repmat then replicates the function f so you need to define a similar function for your plot maybe something like this (a guess):

f = @(x) [ 0 * (sin(x)<=0) + 1 * (sin(x)>0) ]

where x is in radians and the sin(x)<=0 and sin(x)>0 factors control the 0 1 output that you're looking for. I've not tested this though.
I did something like this:
Matlab:
f = @(x)[1.*(-pi<x & x<0) + 0.*(0<x & x<pi)];
x = linspace(-pi,pi);
intvl = [-9 9];
pfx = repmat(f(x),1,diff(intvl)/3);
px = linspace(intvl(1),intvl(2),length(pfx));
plot(px, pfx);
hold on; grid on; grid minor;
xticks([-4*pi:1:4*pi]);
xticklabels({'-4\pi', '-3\pi', '-2\pi', '-\pi', '0', '\pi', '2\pi', '3\pi', '4\pi'});
xlim([-4*pi 4*pi]);
After executing till line 7, I get the following figure:
1597851451213.png
After executing lines 8, 9 & 10, I get this one:

1597851561939.png
Doesn't quite match the figure I want. Where is the problem?
 
  • #4
Your anonymous function looks suspicious only when x<-pi will you get a 1

Did you try the one I wrote for f = @(x) ?
 

1. What is the purpose of using repmat to plot a periodic function?

Using repmat allows us to create multiple copies of a given array, which is useful for plotting periodic functions as it allows us to easily repeat the function over a desired interval.

2. How do I use repmat to plot a periodic function?

To use repmat for plotting, we first need to define the function we want to plot and the interval over which we want to repeat it. Then, we can use repmat to create multiple copies of the function and plot it using a suitable graphing tool.

3. Can repmat be used for any type of periodic function?

Yes, repmat can be used for any type of periodic function as long as it can be defined in terms of an array or vector.

4. How does using repmat affect the accuracy of the plotted function?

Using repmat does not affect the accuracy of the plotted function. It simply allows us to repeat the function over a desired interval, making it easier to visualize the periodic nature of the function.

5. Are there any limitations to using repmat for plotting periodic functions?

One limitation of using repmat for plotting periodic functions is that it may not be suitable for functions with very high frequencies. In these cases, other methods such as interpolation may be more appropriate.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
886
Back
Top