Generate values with constrains
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
Walter Roberson
2020 年 4 月 20 日
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);
採用された回答
その他の回答 (2 件)
Walter Roberson
2020 年 4 月 22 日
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
0 件のコメント
Devineni Aslesha
2020 年 4 月 24 日
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 件のコメント
Walter Roberson
2020 年 4 月 24 日
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)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!