Generate values with constrains

1 view (last 30 days)
Hello,
I am new to matlab and I would like to generate several temperatures (V)s to cover a time duration (Tmax). Vmin <= V <= Vmax. The initial temperature in the room V(t=0)=V0=0; The temperature (V) can be found using thde function:
V(i)=V(i-1)+A*(t(i)-t(i-1));
"A " is picked randomly from A, but with no direct repetition. For example:
A1, A1, A0 , A2 not acceptable since A1 is followed by A1
A1,A2,A0,A1,A2,A2 not accepted since A2 is followed by A2
A1,A2,A0,A1.A0 accpeted
A1,A0,A1,A0,A1 accepted
The number of schedules =10;
Can someone help me please with writting the code. Here is my model.
%% This code is to generate set of temperatures between vmin and Vmax to cover a given time
%
%-------------------------------------------------------------------------%
clear variables;
close all;ddd
clc;
%------------------------------------------------------------------------%
%% Defining Units
%------------------------------------------------------------------------%
A=[-4 4/3 2];
v0=0; % Initial temperature
t0=0; % Initial time
Tmax=7; % Total time
Vmax=5; % Maximum temperature
Vmin=-1; % Minimum temperature
n=10; % Number of itterations (schedules)
n1=nan;
%-------------------------------------------------------------------------%
for i = 1:n
while t<=Tmax
V(i)=V(i-1)+(randi(A)*(rand-t(i-1)));
if V(i)>Vmax || V<Vmin
V(i)=A*(rand-t);
else
% Do Nothing
end
% To avoid selecting the same A
n2 = ind(A);
while n1 == n2
n2 = randi(A);
end
V(i)= [V, n2];
n1 = n2;
end
end
  2 Comments
Walter Roberson
Walter Roberson on 20 Apr 2020
Your random selection can be improved. Generate a random index with
NA - length(A);
i1 = randi(NA);
n1 = A(i1);
After that, you can get a different random entry without direct repetitions by using
i2 = randi(NA-1);
i2 = i2 + (i2 >= i1);
n2 = A(i2);
Hamzah Faraj
Hamzah Faraj on 20 Apr 2020
Hello Walter, Thanks for your tips. Can I ask you to add this to the code. As I said earlier I’m new to coding area and can’t get it immediately. Thanks again

Sign in to comment.

Accepted Answer

Hamzah Faraj
Hamzah Faraj on 22 Apr 2020
idxr = randi(length(a),1,10); %// create the first batch of numbers
idx = diff(idxr)==0; %// logical array, where 1s denote repetitions for first batch
while nnz(idx)~=0
idx = diff(idxr)==0; %// logical array, where 1s denote repetitions for
%// subsequent batches
rN = randi(length(a),1,nnz(idx)); %// new set of random integers to be placed
%// at the positions where repetitions have occured
idxr(find(idx)+1) = rN; %// place ramdom integers at their respective positions
end
modes=a(idxr);

More Answers (2)

Walter Roberson
Walter Roberson on 22 Apr 2020
A=[-4 4/3 2];
n = 10;
NA = length(A);
randA = zeros(1,n);
i1 = randi(NA);
randA(1) = A(i1);
for K = 2 : n
i2 = randi(NA-1);
i2 = i2 + (i2 >= i1);
randA(K) = A(i2);
i1 = i2;
end

Devineni Aslesha
Devineni Aslesha on 24 Apr 2020
To select a random value from A and to obtain different consecutive A's for every schedule can be done using randperm as well.
for i = 1:2:10
n1 = randperm(length(A));
flag = 0;
if i>1
if n1(1) == n2
n1(1) = n1(2);
flag = 1;
end
end
A1(i) = n1(1);
if flag == 1
n2 = n1(3);
else
n2 = n1(2);
end
A1(i+1) = n2;
end
Here, A1 is the desired output.
  1 Comment
Walter Roberson
Walter Roberson on 24 Apr 2020
You never use n1(4) or later so there is no point in using randperm(length(A)): you might as well use randperm(length(A),3)

Sign in to comment.

Categories

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