MATLAB Answers

0

simple random time series

Asked by zain hassan on 28 Oct 2019
Latest activity Commented on by Akira Agata
on 29 Oct 2019
Good day everyone,
excuse me but i am very new to matlab..
I am generating a time series, to perform a fatigue test using the rain flow counting method.
so untill now i was able to generate a spesific number of random samples in a spesefied range..
the thing i need to do is to generate a random number of samples, in a random range between ymin and ymax
clear; clc
NI = 6;
N = [ 1000 , 500 , 2000 , 300 , 700 , 1000 ]; % No of samples
y_min = [ 10 , 30 , 10 , 20 , 0 , 60 ]; *(this needs to be random between 10 and 60)
y_max = [ 15 , 35 , 20 , 22 , 3 , 75 ]; *(this needs to be random between 15 and 75)
T = 0;
Y = 0;
for I = 1 : length(N)
t = 1 : 1 : N(I);
t = T(end) + t;
y = y_min(I) + (y_max(I) - y_min(I))*rand( size(t) );
T = [ T , t ];
Y = [ Y , y ];
end
figure(1); cla; hold on; grid on; box on;
plot( T , Y , 'k' )
i would appreciate any help.

  0 Comments

Sign in to comment.

5 Answers

Answer by Daniel M
on 28 Oct 2019
 Accepted Answer

You can follow this example:
lb = 5; % lower bound, ymin
ub = 10; % upper bound
N = 1000; % num samples
vals = rand(1,N).*(ub-lb) + lb;

  0 Comments

Sign in to comment.


Akira Agata
Answer by Akira Agata
on 28 Oct 2019

How about the following?
N = [1000 , 500 , 2000 , 300 , 700 , 1000]; % No of samples
NI = length(N);
y_min = 10 + (60-10)*rand(1,NI); % Random between 10 and 60
y_max = zeros(size(y_min));
for kk = 1:NI % Create y_max which is random num between max(y_min(kk), 15) and 75
if y_min(kk) < 15
y_max(kk) = 15 + (75-15)*rand;
else
y_max(kk) = y_min(kk) + (75-y_min(kk))*rand;
end
end
Y = zeros(1,sum(N));
T = 1:sum(N);
Ncs = [0, cumsum(N)];
for kk = 1:NI
Y(Ncs(kk)+1:Ncs(kk+1)) = y_min(kk) + (y_max(kk)-y_min(kk))*rand(1,N(kk));
end
figure
plot(T,Y,'k')
grid on
box on

  2 Comments

zain hassan on 29 Oct 2019
the thing is that i also need to have a random N not only Ymin and Ymax.
Akira Agata
on 29 Oct 2019
To generate random integer number, please try randi function.

Sign in to comment.


Answer by zain hassan on 29 Oct 2019

thank you very much for your answer,
the thing i manages to do is:
but i can not make the number of samples random between for examples 500 and 2000.
but it never function.
Error in skript_Generator_003 (line 16)
y = y_min(I) + (y_max(I) - y_min(I))*rand( size(t) );
_________________________________________________________________
clear; clc
NI = 7;
N = [ 1000 , 500 , 2000 , 300 , 700 , 1000 ]; % No of samples
y_min = rand([5,60]);
y_max = rand([10,75]);
T = 0;
Y = 0;
for I = 1 : length(N)
t = 1 : 1 : N(I);
t = T(end) + t;
y = y_min(I) + (y_max(I) - y_min(I))*rand( size(t) );
T = [ T , t ];
Y = [ Y , y ];
end
figure(1); cla; hold on; grid on; box on;
plot( T , Y , 'k' )

  0 Comments

Sign in to comment.


Answer by zain hassan on 29 Oct 2019

thanks for your answer i am checking it now.

  0 Comments

Sign in to comment.


Answer by zain hassan on 29 Oct 2019

the thing is that i also need to have a random N not only Ymin and Ymax.

  0 Comments

Sign in to comment.