how to generate a traingular distrbution (sawtooth model) ?

I want to generate a random (n=100)sawtooth model with width (b=24)
1st (t=0)start at 0, 2nd at 1 .... nth at n=100
t is random variable with gaussian pdf (mu=1 and st.dev=3)
finally I want to add them like:
to form a shape of summation of 100 random sawtooth model
I try:
n=100;
a=0;b=130;c=61;
u=rand(n,1);
x=zeros(n,1);
for i=1:n
U=u(i);
if U<(c-a)/(b-a)
X=a+sqrt(U*(b-a)*(c-a));
else
X=b-sqrt((1-U)*(b-a)*(b-c));
end
x(i)=X;
end
figure
hist(x,100)
which produce this figure
its a good summation model , but not perfectly what I want .
how to generate this distribution?

 Accepted Answer

Guessing here, but if you want a Gaussian pdf with a mean of 1 and a standard deviation of 3, consider replacing the ‘u’ assignment with:
u = 1 + 3*randn(n,1);
The rand function produces uniformly-distributed random numbers on the interval [0,1].

6 Comments

what about the code to produce the summing of this shape:
This gets as close as I can:
t = 0:170;
sawtooth = @(t) rem(t,24); % Creates Sawtooth, ‘t’=Time
sawseries = sawtooth(t(1):t(72));
for k1 = 1:100
sawshift(k1,k1:k1+71) = sawseries; % Create & Sum Sawtooth Series
end
sawsum = sum(sawshift);
figure(1)
plot(t, sawsum)
figure(2)
mesh(sawshift)
grid on
You may want to tweak it, since the 24-sample period of the sawtooth corresponds (in code) to indices of (1,72) but corresponds to a time of (0,71). The ‘sawshift’ matrix stores the shifted versions of the sawtooth at each step, then sawsum sums them. The sum is in figure(1). The ‘sawshift’ matrix is in figure(2), and is just presented to demonstrate how the code works. It could probably be more efficient, but I coded it as I did so its function and structure are straightforward. I used the anonymous function ‘sawtooth’ to make it easier to tweak if you want to experiment with it.
Thanks for your time and effort.
but it seems that I didn't clarify enough.
I adjust the code to closed from what I want:
t = 0:120;
sawtooth = @(t) rem(t,24); % Creates Sawtooth, ‘t’=Time
sawseries = sawtooth(t(1):t(121));
figure
plot(t, sawseries)
for k1 = 1:100
sawshift(k1,k1:k1+120) = sawseries; % Create & Sum Sawtooth Series
end
hold on
plot(t, sawshift(2,1:121))
plot(t, sawshift(3,1:121))
plot(t, sawshift(4,1:121))
axis([0 125 0 25])
but I want K to be a 100 random variable with gaussian pdf and mu=1 st dev= 2
then plot all sawshift them like:
You completely changed your Question!
You can plot the full ‘sawshift’ matrix easily:
figure(1)
plot(t, sawshift)
The ‘k1’ variable is a discrete index, and cannot have an arbitrary continuous distribution.
What you want to do is simply not possible, at least as I understand it.
I will delete my Answer in a few minutes.
Thanks...
I adjust your last line.
plot(t, sawshift(:,1:121))
My pleasure.
Noted. I used my original code in the revised figure(1) plot. (It worked there.)

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics 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!