periodic functions

3 Ansichten (letzte 30 Tage)
li
li am 20 Apr. 2011
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

Antworten (5)

Paulo Silva
Paulo Silva am 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 Kommentare
Matt Fig
Matt Fig am 20 Apr. 2011
Okie-dokie! I had to look that one up, it has been so long...
Paulo Silva
Paulo Silva am 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

Melden Sie sich an, um zu kommentieren.


Clemens
Clemens am 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 Kommentare
Clemens
Clemens am 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
Clemens am 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

Melden Sie sich an, um zu kommentieren.


Matt Fig
Matt Fig am 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 Kommentare
Paulo Silva
Paulo Silva am 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
Matt Fig am 20 Apr. 2011
Good point, Paulo!

Melden Sie sich an, um zu kommentieren.


li
li am 21 Apr. 2011
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 Kommentare
li
li am 21 Apr. 2011
ok and how should I do with the minus part? thanks
li
li am 21 Apr. 2011
haha your modified answer is cool. It looks like you know what I am doing!

Melden Sie sich an, um zu kommentieren.


li
li am 21 Apr. 2011
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 Kommentare
li
li am 21 Apr. 2011
Or Matt can help to asnwer this too thanks
Paulo Silva
Paulo Silva am 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

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by