How Can i transform condition into constraint equation?

2 views (last 30 days)
Hello everyone!
i'm asking if i can change a condition line command into equation in order to use it in my problem. The condition is related to trajectory of vehicle in segment line, so if it arrives at the last point of the segment , it will come back to the initial point and keep going back and forth until the time ends.
in my code i wrote the condition as follow :
qu(1) = 0; % qu the initial position of vehicule
speed = 50; % 50 is the speed of vehicule ( constant)
k = 1;
while k < length(qu)
xk = qu(k) + speed;
if xk > 10000 %10000 is the length of the segment
speed = -50;
elseif xk < 0
speed = 50;
else
k = k+1;
qu(k) = xk;
end
end
How can i transform this code into simple equation?
any help is appreciated!
  3 Comments
Walter Roberson
Walter Roberson on 11 Jan 2023
I'm pretty sure they are using fixed timestep, and that the speed variable is distance per time step.
Maria
Maria on 11 Jan 2023
@Torsten i want to add the condition of the length of segment, it means that : when new position = length of the segment , the new position should return at the previous position.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 10 Jan 2023
length(qu) starts as 1, k starts at 1, 1 < 1 is false, so the loop body is never entered. Therefore the code can be reduced to just the first two lines.
  26 Comments
Torsten
Torsten on 17 Jan 2023
Thus my question remains.
Is qu(1) a multiple of 20 ?
If no: How to deal with the case that the opposite wall is not exactly hit at a time instant t ?
Walter Roberson
Walter Roberson on 17 Jan 2023
Are you wanting the model to reject certain proposed values of qu(1), to say "Ah, this set of proposed parameters does not lead to a feasible model, try a different set of parameters" ?
If so, if qu(1) is one of the model parameters, then set a lower bound on it of 0, and set an upper bound of (L-NumberOfTimeSteps*v) . [If (L-NumberOfTimeSteps*v) would be negative, then that implies the equations are impossible to satisfy]
But I don't think that is what you want. You want the direction to reverse when you hit L or 0 and you want to keep going. That is not a constraint and cannot be handled by upper/lower bounds or by linear inequalities or by nonlinear equalities.

Sign in to comment.

Categories

Find more on Linear Programming and Mixed-Integer Linear 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!