# Using mathematica to find the equation of curves, by providing images, providing constraints, or both

By "collaborate", I mean to give some instructions or constraints.

Consider the following curves: (as examples, only to illustrate my question in a better way):

Curve (I): Assume it is f instead of f':

My constraint are:

1. y[-4]==0, y[5.1]==0, y[10]==0
2. y'[x]>0 for x in (-∞,0) and (7,∞), and y'[x]<0 for x in (0,7)
3. y''[x]>0 for x in (5,∞), and y''[x]<0 for x in [-∞,5]

curve (II):

My constraints: such and such

Curve (III): Yes it is easy without mathematica, since these are just parts of the unit circle, but say we do not know.

I do not have great knowledge in Mathematica, but I believe it can help for such task.

If I have all constraints, such as intercepts, first and second derivatives, do I need to provide the image of these curves? Will providing support in finding a better fit? Will my constraints be sufficient?

Note that, the final result can be piecewise functions, as in Curve III

Can it handle many constraints? Curve IV really has many constraints, increasing and decreasing, concave up and concave down.

What if I need my constraints to be exact, with no deviation?

EDIT:

As suggested in the comment, I tried to clean and change the colour of Curve IV. Is this the way?

Secondly, would it be sufficient to provide only that? How to ensure local max and local min, ensure concavity, ensure intercepts, etc. (Note: I can provide such information, but with providing, will it be exact or just estimate?)

Your help would be appreciated. THANKS!

• The images and constraints could help you find equations of curves but you're going to need to make some assumptions and clean up the images: 1) The curve part in the images should be easy to extract and other noise like axes and markers should not get in the way. I would recommend making the curve a different colour. 2) the images should come with metadata that tells you the min and max x and y coordinates of the curve. Commented Jun 14 at 22:33
• 3) There's no convenient way to generally model any arbitrary curves - you should settle on a family of representations, for example splines / polynomials then optimize the coefficients/control points towards your constraints or via some score function based on the extracted pixels from the image. Commented Jun 14 at 22:34
• @flinty (1) Thanks for your comment, it actually motivated me that this task is possible. (2) I did not actually understand what actually you mean by "clean up", I prepare one (Curve IV) to see if that is exactly what you mean. (3) if that is what you meant, how to ensure the local max and local min, and ensure concavity? Do we need to provide these constraints, or will it be detected automatically? (4) I am not expert in mathematica, so how can I begin writing the code for this task? See Curve IV, will this work? i.sstatic.net/kEBc2hPb.png Commented Jun 15 at 6:59
• You can at least extract the curve from the image when it's a different color from the axes like this. I then used the metadata from the plot (local min, zeros, domain of x ends at 0 on the left) to scale the data points. I didn't do any curve fitting because I feel it's a little dangerous to fit a function without any reasoning behind why that function should fit the data. Note that this isn't really automated either because I had to extract the metadata myself and the way I do the scaling/shifting is kind of bad.
– ydd
Commented Jun 17 at 2:42

I will show a simple method to generate a function based on constraints. Let us start with a polynomial form of low order:

n = 5;
y[x_] = Array[a[#] Power[x, #] &, n + 1, {0, n}] // Total;


The order can be adjusted based on the complexity of constraints. Next impose the constraints at given points and determine a possible combination of parameters using FindInstance. In the case of inequalities, the constraints are imposed at the edges of plot interval

xL = -12;
xR = 12;

s = FindInstance[
y[-4] == 0 && y[5.1] == 0 && y[10] == 0 &&
y'[xL] > 0 && y'[1] < 0 && y'[xR] > 0 &&
y''[xL] < 0 && y''[xR] > 0 &&
y[0] == 2,
Array[a, n + 1, {0, n}], Reals][[1]];


Finally, plot

Plot[y[x] /. s, {x, xL, xR}, PlotRange -> {All, {-12, 12}},
PlotTheme -> "Monochrome", AxesLabel -> {x, y}, AxesStyle -> 14]


It might be good to impose additional constraints in order to regularise the function, and to display the whole family of possibilities:

n = 5;
xL = -12;
xR = 12;
y[x_] = Array[a[#] Power[x, #] &, n + 1, {0, n}] // Total;
s = FindInstance[
y[-4] == 0 && y[5.1] == 0 && y[10] == 0 &&
y'[xL] > 0 && y'[1] < 0 && y'[xR] > 0 &&
y''[xL] < 0 && y''[xR] > 0 &&
y[0] == 2 && Integrate[y[x]^2, {x, xL, xR}] == 1000,
Array[a, n + 1, {0, n}], Reals, 20];
Plot[y[x] /. s, {x, xL, xR}, PlotRange -> {All, {-12, 12}},
AxesLabel -> {x, y}, AxesStyle -> 14, Evaluated -> True,
PlotStyle -> Thin]


• This is nice, but not really what I am looking for. As the title says "... equation of curves ...", but this does not provide the equation. Secondly, when I added some more constraints, sometimes I see the curves, but sometimes I saw a blank graph, only axes. In your answer, the family of curves is really good, but how to get the equation, for at least one member from this family? Commented Jun 21 at 21:31
• "when I added some more constraints, sometimes I see the curves, but sometimes I saw a blank graph" There could be i) contradictory constraints, ii) the number of constraints is larger then the number of free parameters. In the given example, the function is modelled as a 5th order polynomial. You might need to increase the order n. If it does not work, update your post to show a problematic case. Commented Jun 22 at 8:17
• @Hussain-Alqatari How to get the equation: just execute y[x] /. s. In the first example it will give you 1 function. In the second example it will give you a family of 20 functions. Commented Jun 22 at 12:45