Random Timeseries with Max and Min Delta

1 visualizzazione (ultimi 30 giorni)
johnnyjohnson
johnnyjohnson il 18 Set 2016
Risposto: BhaTTa il 27 Ago 2024
Hi, i want to create a reproducable timeseries with e.g. 2000 values, which are containing numbers between "minval" and "maxval". the minimum and the maximum of two following values should be "mingrad" and "maxgrad.
Till now I only got the code for some randomized reproducable numbers. My current code:
clear all
val.n=1000;
val.min=0;
val.max=100;
grad.min=-5;
grad.max=5;
rng(0,'twister');
r=(val.max-val.min).*rand(val.n,1)+val.min;
How can i add the wanted gradient between two numbers? I would accept values short outside my range, if r(x)=0 that r(x+1)=-5.

Risposte (1)

BhaTTa
BhaTTa il 27 Ago 2024
To create a reproducible time series with specific constraints on the gradient between consecutive values, you can generate the series iteratively. You need to ensure that each new value falls within the specified gradient range relative to the previous value. Here's how you can modify your code to achieve this:
% Clear workspace
clear all;
% Define parameters
val.n = 2000; % Number of values
val.min = 0; % Minimum value
val.max = 100; % Maximum value
grad.min = -5; % Minimum gradient between consecutive values
grad.max = 5; % Maximum gradient between consecutive values
% Set random seed for reproducibility
rng(0, 'twister');
% Initialize the time series array
r = zeros(val.n, 1);
% Start with a random initial value within the specified range
r(1) = (val.max - val.min) * rand() + val.min;
% Generate the time series
for i = 2:val.n
% Calculate potential next value with the gradient constraints
next_min = max(val.min, r(i-1) + grad.min);
next_max = min(val.max, r(i-1) + grad.max);
% Ensure the next value is within the specified min and max limits
r(i) = (next_max - next_min) * rand() + next_min;
end
% Display the first few values of the time series
disp(r(1:10));

Categorie

Scopri di più su Time Series in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by