# Design a nonlinear least squares function with multiple local minima

I need a nonlinear least squares function $$f: \mathbb{R} \rightarrow \mathbb{R}$$ which has multiple minima in order to plot it and test some convergence methods using it.

The function must be of the form $$f = \frac{1}{2} r^2$$ where $$r: \mathbb{R} \rightarrow \mathbb{R}$$

I do not want the smallest minimum to be where $$f(x) = 0$$, it must be a non zero residual problem.

$$f(x)$$ can not take negative values, but $$x$$ can be negative.

Thanks

Take e.g. $$r(x) = (x-a)^2(x-b)^2 + 1$$. It will have two minima (at $$x=a$$ and $$x=b$$), and will never be zero (thanks to $$+1$$ in the end), implying $$f$$ will never be zero (in particular $$f(x) \neq 0$$ for the minima). Thus, it satisfies your conditions.

Live on Desmos

If you need the function to have different values at different minima, or if you want more than two minima, you can try this approach. Let $$x = a_1, \dots, a_n$$ be our minima, and $$c_1, \dots, c_n \in (0, 1)$$ and $$d_1, \dots, d_n$$ some constants. Define

$$r_i(x) = 1 - (1-c_i)exp(-d_i(x-a_i)^2)$$

$$exp(-d_i(x-a_i)^2)$$ defines a "bump" with maximum at $$a_i$$, and $$d_i$$ controls it's flatness. $$r_i$$ is this same bump, reversed and scaled so that it takes value $$c_i$$ at $$x=a_i$$ and takes the value of $$1$$ when $$x$$ is far from $$a_i$$.

Now, take the product of the $$r_i$$:

$$r(x) = r_1(x) \cdot r_2(x) \cdots r_n(x)$$

Live on Desmos

• Thanks for this, that works but at the moment the two minima give me the same value for $f(x)$. I should have added that I would ideally like the minima to be different so $f(a) \neq f(b)$. Any idea? Thanks – M6126 Oct 11 '18 at 15:26
• @M6126 I've updated the post. – lisyarus Oct 11 '18 at 16:05
• That's amazing, thanks very much! – M6126 Oct 12 '18 at 8:20