check the value if less than 200 or not

1 view (last 30 days)
Moataz alakkad
Moataz alakkad on 24 Jul 2022
Edited: Rupesh on 27 May 2024
hello;
I have been ceated code as below:
clc,
clear;
N=1000;
d11=3415;
d21=341;
d31=34135;
c=350000;
p11=31;
p21=45;
p31=15;
alf=0.1;
bet=0.5;
for N=1:N
I1=rand(N,1)
v1=c/d11*I1;
V11=rand(N,1).*v1;
a11=rand(N,1);
x11=V11/a11(N);
I2=rand(N,1);
v2=c/d21*I2;
V21=rand(N,1).*v2;
a21=rand(N,1);
x21=V21/a21(N);
I3=rand(N,1);
v3=c/d31*I3;
V31=rand(N,1).*v3;
a31=rand(N,1);
x31=V31/a31(N);
F=((alf+bet.*I1).*p11.*x11)+((alf+bet.*I2)*p21.*x21)+((alf+bet.*I3).*p31.*x31);
end
Input = [I1, x11,a11, I2, x21, a21, I3, x31, a31];
Output = F;
result=[Output Input];
best_output= max(Output);
[best_output,idx] = max(Output);
best_input = Input(idx,:);
best_reuslt= [best_output,best_input];
this the code that I have been created I need to limit x11 x21 and x31 to be less than or equal to 200 . if x11 more than 200 change the value of I1. and same thige for x21 and x31
now, I want to check if x21 less than or equal to 200 , than I2 is true and if x21 more than 200, then update I2.
that is mean I need the value of I2 that make x21 equal or less than 200
  3 Comments
dpb
dpb on 24 Jul 2022
Edited: dpb on 24 Jul 2022
Also NB you've used N in two different contexts -- as the constant and also as the looping variable in the for loop.
Walter Roberson
Walter Roberson on 24 Jul 2022
Note that you are overwriting all of the vectors every time through the "for N" loop, so the final result is whatever was assigned in the last iteration.
The reasons why you might want to do that are:
  • timing tests
  • you need to waste time such as to give graphics time to render
  • each rand() call "uses up" pseudorandom numbers and advancing in the random number stream ignoring some results is important for some reason
  • or it is simply a bug in your code

Sign in to comment.

Answers (1)

Rupesh
Rupesh on 27 May 2024
Edited: Rupesh on 27 May 2024
Hi Moataz,
I understand that you need to ensure x11, x21, and x31 do not exceed 200 by adjusting their respective I1, I2, and I3 values. The main issue is finding a way to dynamically adjust these I values such that the corresponding x values remain within the specified limit. To resolve this, you can consider introducing a loop for each I calculation that continues to adjust the I value until the corresponding x value is less than or equal to 200. This approach ensures that the x values are kept within bounds without manually tweaking the I values. Below are some modifications in your loop logic which will ensure that you achieve the desired outcome.
for N = 1:N
% Adjusting I1 for x11
conditionMet = false;
while ~conditionMet
I1 = rand(N,1);
v1 = c/d11.*I1;
V11 = rand(N,1).*v1;
a11 = rand(N,1);
x11 = V11./a11(N);
if max(x11) <= 200
conditionMet = true;
end
end
% Adjusting I2 for x21
conditionMet = false;
while ~conditionMet
I2 = rand(N,1);
v2 = c/d21.*I2;
V21 = rand(N,1).*v2;
a21 = rand(N,1);
x21 = V21./a21(N);
if max(x21) <= 200
conditionMet = true;
end
end
% Adjusting I3 for x31
conditionMet = false;
while ~conditionMet
I3 = rand(N,1);
v3 = c/d31.*I3;
V31 = rand(N,1).*v3;
a31 = rand(N,1);
x31 = V31./a31(N);
if max(x31) <= 200
conditionMet = true;
end
end
F = ((alf+bet.*I1).*p11.*x11) + ((alf+bet.*I2).*p21.*x21) + ((alf+bet.*I3).*p31.*x31);
end
This code dynamically adjusts I1, I2, and I3 to ensure that x11, x21, and x31 are within the specified limit. By using a while loop for each I value, the code recalculates the dependent variables until the condition (x less than or equal to 200) is met.
Hope this helps!
Thanks

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!