Question

The variable y may take a value which is in a defined range:

3<y<5

The value of y should be determined introducing a constraint like

|x-y|=min

x is given and should scan a larger range like:

x:=-1000:1:1000

How do I find the exact y-value with a given x?

The results that I consider is like:

x     y
-1000 3
.     3
.     3
2.9   3
3     3
3.1   3.1
4     4
5     5
6     5
7     5
.     5
.     5
1000  5

Which means I want to allow a larger "error" but between 3 and 5 it should solve with a very smaller error so that I can resolve this area fine as possible.

What would be the best way to implement something like this in Matlab? Without "IF"-condition and if possible, symbolically. But also numerical alternatives would be interesting.

Was it helpful?

Solution

Based on your comment and example I think you are simply looking for this:

x = -10:0.1:10 %Suppose this is your x

y = max(min(x,5),3) %Force it between 3 and 5 by rounding up or down respectively
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top