periodic functions

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

回答(5 个)

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 个评论

This introduces some strange artifacts into the function. Try, for example,
x1 = .75;
n = 4;
amp = 6;
Inded it does but that can be interpreted as Slew Rate
Okie-dokie! I had to look that one up, it has been so long...
We still need to know if li wants a nice graph or values for something else, maybe li wants a signal generator

请先登录,再进行评论。

Clemens
Clemens 2011-4-20
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 个评论

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.
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.
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
Matt Fig 2011-4-20
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 个评论

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.
Good point, Paulo!

请先登录,再进行评论。

li
li 2011-4-21

0 个投票

o, thanks every one first. I think Paulo Silva's answer fit the most but there is another problem ha ha, I need the function to extend to infinity, which means it will repeat itself from -infinity to infinity, so how can I modify it? thanks

5 个评论

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
li 2011-4-21
http://www.flickr.com/photos/58977081@N02/5639550024/
here it is thanks
Infinity is too much but in my code you can choose a bigger n and shift xxx values (xxx-floor(n/2))
li
li 2011-4-21
ok and how should I do with the minus part? thanks
li
li 2011-4-21
haha your modified answer is cool. It looks like you know what I am doing!

请先登录,再进行评论。

li
li 2011-4-21

0 个投票

one last question, Paulo Silva how can I modify your code to get something like this?http://www.flickr.com/photos/58977081@N02/5639015965/ thanks

2 个评论

li
li 2011-4-21
Or Matt can help to asnwer this too thanks
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

请先登录,再进行评论。

提问:

li
2011-4-20

Community Treasure Hunt

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

Start Hunting!

Translated by