Stretch graph while staying within range

186 Views Asked by At

I need a mathematical formula to stretch a graph meanwhile it still within a maximum/minimum range. Important to not exceed the boundaries, which mean a range of -1 and 1. I don't want just simple "cut down" the part which overflow, the rescaled graph must be proportional.

This would be a programming related question, but I didn't get answer there (link), so I ask here and I will try to implement the answer in C#. Please if you have a solution don't be too complicated (I'm not a mathhead), so I can use it in coding.

I drew a picture to make it more understandable: EXAMPLE IMAGE

  • The red lines at the top and bottom are the limits (-1 and 1)
  • The gray dotted is the center
  • The yellow graph is the original one
  • The white graph is what I want to get

As you can see the white stretched. Note the peak at the middle where the new rescaled graph is not going over the top, but it still in range and proportionally a bit under the limit.

I tried to figure out the math long time ago, but I failed.. It's should be simple, but nah :) Of course, I can't just apply multiplication, as it will go over the top at the highest parts if I wish to stretch strongly.

I appreciate any help and idea!

1

There are 1 best solutions below

2
On

The two main functions you want to apply are:

  • Multiplication by a scalar, i.e. $f_{new}(x) = a \times f_{old}(x)$ - this will scale the graph larger proportionally; if the original function returned values in the range $[y_{min}, y_{max}]$ then the new function will return values in the range $[a y_{min}, a y_{max}]$. For example, if $a = 2$ and the original range was $[-0.5, 0.3]$ then the new range will be $[-1, 0.6]$.

  • Addition with a scalar, i.e. $f_{new}(x) = f_{old}(x) + b$ - this will move the graph up or down without changing its scale; the range will go from $[y_{min}, y_{max}]$ to $[y_{min} + b, y_{max} + b]$.

We can apply the addition followed by the multiplication, giving $f_{new}(x) = a (f_{old}(x) + b)$, which moves the range to $[a(y_{min} + b), a(y_{max} + b)]$. If your aim is for that to equal $[-1, 1]$, then you can solve for $a$ and $b$ in terms of $y_{min}$ and $y_{max}$.

If you just want to multiply by a scale but keep things inside that range, then notice that if $a > y_{max}$ or $a > -y_{min}$ then you're guaranteed to blow outside $[-1, 1]$ in one direction or the other. So the biggest scaling factor you can choose in that instance is $a = \max(y_{max}, -y_{min})$.

Importantly, both of these approaches are data dependent - the adjustment values are functions of the maximum and minimum points in your function. If you want a global adjustment, then you'd have to find the largest and smallest possible values the function could ever take, and use those as your $y_{max}$ and $y_{min}$. Without those, you're kind of screwed.