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!