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 个)
Paulo Silva
2011-4-20
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 个评论
Matt Fig
2011-4-20
This introduces some strange artifacts into the function. Try, for example,
x1 = .75;
n = 4;
amp = 6;
Paulo Silva
2011-4-20
Inded it does but that can be interpreted as Slew Rate
Matt Fig
2011-4-20
Okie-dokie! I had to look that one up, it has been so long...
Paulo Silva
2011-4-20
We still need to know if li wants a nice graph or values for something else, maybe li wants a signal generator
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 个评论
Matt Fig
2011-4-20
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
2011-4-21
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
2011-4-21
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
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 个评论
Paulo Silva
2011-4-20
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
2011-4-20
Good point, Paulo!
li
2011-4-21
0 个投票
5 个评论
Matt Fig
2011-4-21
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
2011-4-21
Paulo Silva
2011-4-21
Infinity is too much but in my code you can choose a bigger n and shift xxx values (xxx-floor(n/2))
li
2011-4-21
li
2011-4-21
li
2011-4-21
0 个投票
2 个评论
li
2011-4-21
Paulo Silva
2011-4-21
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
类别
在 帮助中心 和 File Exchange 中查找有关 Signal Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!