periodic functions
Show older comments
I would like to know how to construct a 1 periodic function that is the same as the below link, http://www.flickr.com/photos/58977081@N02/5637186489/ the x1 is any values that could be adjusted by me. could anyone guide me to the result? thanks
Answers (5)
Paulo Silva
on 20 Apr 2011
amp=1; %amplitude of the wave
xp1=0.5; %this is your x1 value
r=0.1;n=3; %distance between points (r) and number of periods (n)
x0=0;y0=amp;x1=xp1;y1=0; %points to calculate m
m=(y1-y0)/(x1-x0); %declive of the wave
x=0:r:xp1; %x points of the wave where y value is not zero
xt=xp1:r:1; %x points of the wave where y value is zero
xx=[x xt]; %all the x points together
yy=[m*x+amp 0*xt]; %the y value of the wave
yyy=repmat(yy,1,n); %n periods of the wave
xxx=[]; %make the time value for all the wave periods
for a=0:n-1 %the use of the for can be avoided somehow
xxx=[xxx a+xx]; %I used the for just to make it work for now
end
%xxx=xxx-floor(n/2); %just in case you want to shift the x values
plot(xxx,yyy,'r') %plot it just to see if its working
4 Comments
Matt Fig
on 20 Apr 2011
This introduces some strange artifacts into the function. Try, for example,
x1 = .75;
n = 4;
amp = 6;
Paulo Silva
on 20 Apr 2011
Inded it does but that can be interpreted as Slew Rate
Matt Fig
on 20 Apr 2011
Okie-dokie! I had to look that one up, it has been so long...
Paulo Silva
on 20 Apr 2011
We still need to know if li wants a nice graph or values for something else, maybe li wants a signal generator
Clemens
on 20 Apr 2011
I would write it as function:
function y = myfun(x,x1) %definition of function
xx = mod(x,1); % remainder to start of period 1
k=1/x1; % slope of first part
if xx<x1, y = 1-k*xx; % calculate value for part x<x1
else y=0; end % calculate for x>=x1
of course there would be many ways to describe the curve.
3 Comments
Matt Fig
on 20 Apr 2011
This does not produce the graph. You are using an IF statement to pick out elements of an array, which it does not do... Logical indexing could be made to work.
Clemens
on 21 Apr 2011
That is true. I would apply it as follows:
x = linspace(-100,100,10000);
y = arrayfun(@(a) myfun(a,0.3),x);
I suppose logical indexing will speed things up.
Clemens
on 21 Apr 2011
Since I just read about producing infinite values. I'd do it:
x=0; step = 0.1;
while true
y = myfun(x,x1);
% so something with x,y
x = x+step;
end
Matt Fig
on 20 Apr 2011
Yet another approach:
x1 = .5; % x1 in image
amp = 3; % amplitude
n = 3; % number of periods.
x = [reshape(bsxfun(@plus,(0:n-1),[ 0 0 x1].'),1,3*n) n];
y = [0 repmat([amp 0 0],1,floor(length(x)/3))];
plot(x,y)
EDIT
In response to Paulo's comment, here is a version which preserves the exact point values at the integers and x1, yet supplies many more points.
x1 = .5; % x1 in image
amp = 3; % amplitude
n = 3; % number of periods.
r = 0.1; % max distance between points.
x = [0 (0:r:x1-eps) x1 x1+r-mod(x1,r):r:1-2*eps];
y = [repmat(max([0 -(amp/x1)*(x(2:end))+amp],0),1,n) 0];
x = [reshape(bsxfun(@plus,0:n-1,x.'),1,n*length(x)) n];
plot(x,y)
2 Comments
Paulo Silva
on 20 Apr 2011
That does produce a nice graph but it's just a few points, not enough to be used for input on a function, for example the simulink models.
Matt Fig
on 20 Apr 2011
Good point, Paulo!
li
on 21 Apr 2011
0 votes
5 Comments
Matt Fig
on 21 Apr 2011
What do you mean? Are you saying you want an infinite number of function values stored on your computer? That is going to be tough to do! If you mean that you need to be able to specify an arbitrary ending point, look at my edited answer. You can plug in for n the ending point.
li
on 21 Apr 2011
Paulo Silva
on 21 Apr 2011
Infinity is too much but in my code you can choose a bigger n and shift xxx values (xxx-floor(n/2))
li
on 21 Apr 2011
li
on 21 Apr 2011
li
on 21 Apr 2011
0 votes
2 Comments
li
on 21 Apr 2011
Paulo Silva
on 21 Apr 2011
amp=1; %amplitude of the wave
xp1=0.5; %this is your x1 value
r=0.1;n=4; %distance between points (r) and number of periods (n)
x0=0;y0=0;x1=xp1;y1=amp; %points to calculate m
m=(y1-y0)/(x1-x0); %declive of the wave
x=0:r:xp1; %x points of the wave where y value is not zero
xt=xp1:r:1; %x points of the wave where y value is zero
xx=[x xt]; %all the x points together
yy=[m*x 0*xt]; %the y value of the wave
yyy=repmat(yy,1,n); %n periods of the wave
xxx=[]; %make the time value for all the wave periods
for a=0:n-1 %the use of the for can be avoided somehow
xxx=[xxx a+xx]; %I used the for just to make it work for now
end
xxx=xxx-floor(n/2);
plot(xxx,yyy,'r') %plot it just to see if its working
Categories
Find more on Signal Operations 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!