How to build function based on constraints

83 Views Asked by At

I'm trying to make a function that follows a set of constraints : $$f(0) = 0$$ $$f'(0.5) = 0$$ => minimum $$f(0.5) = -0.1$$ => not sure of value so should be easy to change $$f'(6.5) = 0$$ => maximum $$f(6.5) = 3$$ => not sure of value so should be easy to change $$f(1.5) = 0$$ $$\lim_{x\to\infty} f(x) = 0$$ $$\lim_{x\to-1} f(x) = \infty$$

The goal is to fit the blue line here: Goal image

1

There are 1 best solutions below

2
On BEST ANSWER

The shape of the function in the attached image can be approximated by the sum of a function of the form $$f_1(x)=\frac{a}{x+1}$$ and another function of the form $$f_2(x)=be^{c\left(x-6.5\right)^2+d}.$$ We can add these two functions together and include an adjustable vertical offset parameter to arrive at a function of the form $$f(x)=\frac{a}{x+1}+be^{c\left(x-6.5\right)^2+d}+h.$$ Simply put, $f$ is the sum of a function like $1/x$ and a Gaussian. This combination, with the fixed values of $1$ and $6.5$, should give us the vertical asymptote at $x=-1$ and the slow rise to a maximum at $x=6.5$. All that remains is to find values of $a$, $b$, $c$, $d$, and $h$ that satisfy the first six constraints you gave*.

After playing around in Desmos a bit, I found values of those five constants that came close to satisfying the constraints. I could only determine those to one or two significant digits, so for more precise answers, I used a Python function called scipy.optimize.fsolve**. Here's a Desmos page showing the result, and a picture of the final product:

Desmos screenshot showing an approximation of the function shown in the linked image in the question

And here's the full form of the function, just for completeness: $$f(x)=\frac{0.446176}{x+1}+2.00058e^{-0.0857155\left(x-6.5\right)^2+0.559650}-0.561971$$


*The final constraint is guaranteed to hold because of the denominator of $x+1$ in $f_1$, and enforcing the seventh constraint made the final product look a lot less like your picture while technically still adhering to the other constraints, so I decided not to enforce it.)

**I don't know what the best way is to share code here, but I can figure that out if you'd like to see what I did in more detail.