121 views (last 30 days)

I need to set maximum and minimum value of variables. For example,

x3= x1 + x2

where

maximum value of x1 = 3

minimum value of x1 = 0

maximum value of x2 = 2.5

minimum value of x2 = -1

I didn't understand that how i can implement it in MATLAB codes.

Omer Yasin Birey
on 18 Jan 2019

Edited: Omer Yasin Birey
on 18 Jan 2019

Hi Usman, you can use one of the codes below. However, I recommend the first one, because no loop is necessary here.

x1Max = 3;

x1Min = 0;

x2Max = 2.5;

x2Min = -1;

x1(x1>x1Max) = x1Max;

x1(x1<x1Min) = x1Min;

x2(x2>x2Max) = x2Max;

x2(x2<x2Min) = x2Min;

x3 = x1 + x2;

or

x1Max = 3;

x1Min = 0;

x2Max = 2.5;

x2Min = -1;

x3 = zeros(length(x1),1);

for i = 1:length(x1)%or length x2 since they must have the same dimensions.

if x1(i)>x1Max

x1(i) = x1Max;

elseif x1(i)<x1Min

x1(i) = x1Min;

end

if x2(i)>x2Max

x2(i) = x2Max;

elseif x2(i)<x2Min

x2(i) = x2Min;

end

x3(i) = x1(i)+x2(i);

end

Omer Yasin Birey
on 20 Jan 2019

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.