# Thread: function for fitting a curve to 3 points

1. ## function for fitting a curve to 3 points

Hi MHF community! Hi. I develop audio synthesis and sequencing application, and am currently exploring a new synthesis technique. As part of this, I'll be specifying three [x,y] co-ordinates. two of them will represent the 'base' of a waveform ie [a, b] and [c, b]. the third will represent the 'peak point' above the 'base' ie [d, e] where a<d<c and e>b. Now, I need a function with a user-specified parameter: 'skinniness' (s for short). When s is a low value, the function should draw a smooth curve from [a,b], with a peak at [d, e], then down back to [c,b]. The low s value means the curve should 'bow out' as much as possible. When s is an intermediate value, the curve should essentially look similar to a triangle ie a straight line from [a,b] to [d,e] and a straight line from [d,e] to [c,b]. When s is high, the curves between the points should be as 'sucked in' as possible; something that looks line an exponential from [a,b] to [d,e], and a reversed exponential from [d,e] back down to [c,b]. Assistance please to meet this objective?

2. Try this curve

$f(x,s)=\begin{cases} \frac{1}{\exp\left(\tan\left( \frac{\pi}{100}s-\frac{\pi}{2} \right) \right)-1}\left[ \exp\left(\tan\left[\left( \frac{\pi}{100}s-\frac{\pi}{2} \right) x]\right \right)-1 \right], \text{ if } s \ne 50 \\ x \text{ if } s =50\end{cases}$

This is valid for $x \in [0,1]$ and $s \in (0,100)$ and gives you half of the wave.

Now you can use the algebra of functions to reflect stretch and shift this any where.

To complete this wave you would graph

$g(x,s)= \begin{cases} f(x,s), \text{ if } 0 \le x \le 1 \\ f(-[x-2],s) \text{ if }, 1 < x \le 2\end{cases}$

Let me know if this works for you.

3. Thanks for the function. Is this saying that y=1 if x=1?

Because, if 0<x<2 (as you have) and 0<y<10 (say) then the points could be:

first point: [0,0]
second point specified by user (which must be peak of wave): [0.2, 3]
third point: [2,0]

or:

first point: [0,0]
second point specified by user (which must be peak of wave): [1.6, 8]
third point: [2,0]

etc

4. Originally Posted by doctornash
Thanks for the function. Is this saying that y=1 if x=1?

Because, if 0<x<2 (as you have) and 0<y<10 (say) then the points could be:

first point: [0,0]
second point specified by user (which must be peak of wave): [0.2, 3]
third point: [2,0]

or:

first point: [0,0]
second point specified by user (which must be peak of wave): [1.6, 8]
third point: [2,0]

etc
So to get your first one you would use the function

$g(x)=\begin{cases} 3f\left(\frac{x}{0.2},s \right) , \text{ if } 0 \le x \le 0.2 \\ 3f\left( -\frac{x-2}{2-.2},s\right), \text{ if } .2 \le x \le 2\end{cases}$

I can't upload any files except .pdf so I changed this animated .gif file to a pdf so I could upload it. If you want to see it you will need to change it back to a .gifwave.pdf

This is what that would look like as s ranges between 0 and 100 and going though the points you gave.

Here is the maple code used to generate the .gif file

Code:
with(plots):

z := piecewise(s <> 50, (exp(tan((1/100)*Pi*s-(1/2)*Pi)*x)-1)/(exp(tan((1/100)*Pi*s-(1/2)*Pi))-1), x);

j := animate(plot, [((Heaviside(x)-Heaviside(x-.2))*3)*(eval(z, x = x/(.2))), x = 0 .. 2], s = 1 .. 99);

k := animate(plot, [((Heaviside(x-.2)-Heaviside(x-2))*3)*(eval(z, x = -(x-2)/(2-.2))), x = 0 .. 2], s = 1 .. 99);

display(j,k);
Note that there is an artifact from the use of the Heaviside step function in the plot.

5. Brilliant! Meets the criteria perfectly

Just for my education, are there applications (perhaps Scilab?) where one can specify constraint criteria that a curve must fit, and the application then provides a few simulations with accompanying functions? I imagine it wouldn't be quite as straightforward as that...

6. Originally Posted by doctornash
Brilliant! Meets the criteria perfectly

Just for my education, are there applications (perhaps Scilab?) where one can specify constraint criteria that a curve must fit, and the application then provides a few simulations with accompanying functions? I imagine it wouldn't be quite as straightforward as that...
I don't know of any, but maybe someone else will. Also I am not familiar with Scilab.

7. Although TheEmptySet's function is useable, after working with it a bit I now realize that my specification was a tad incomplete. Is it possible for the curve to be completely continuous around the peak (like a sinewave is continuous)? EmptySet's function results in a discontinuity at the peak as a result of the splicing of two waveform halves. For example this function:

y = y1 + (x-x1)((-y1+y2/-x1+x2) + ((x-x2)((y1-y2/-x1+x2) + (-y2+y3/-x2+x3))/ (-x1+x3))

does the trick, but has the deficiency that it does not support s-parameter based curve variability. (Another advantage of the aforementioned function is that the start and end points need not be in the same plane ie they can have different y values).

I do not know what the protocol is on this forum if the requester realizes a deficiency in the specification after a solution satisfying the original criteria is posted, so apologies in advance, but the IDEAL situation would be if the curve does what EmptySet's curve does, with the additional requirements that:
a) The curve be smooth all the way (ie continuous through the peak point)
b) It be possible for the end points of the curve to have differing y values

Do I need to create a new post? Again, sorry!

8. Originally Posted by doctornash
Although TheEmptySet's function is useable, after working with it a bit I now realize that my specification was a tad incomplete. Is it possible for the curve to be completely continuous around the peak (like a sinewave is continuous)? EmptySet's function results in a discontinuity at the peak as a result of the splicing of two waveform halves.
I am confused. The function is continuous at the peak! I need to ask clarification then. I am guessing you mean differentiable? If so how differentiable? is $C^1$ enough?

If this is the cases you have contradicted what you asked for in the first post
When s is an intermediate value, the curve should essentially look similar to a triangle ie a straight line from [a,b] to [d,e]
While this is continuous it cannot be differentiable at the peak for all values of s.

Also for the 2nd question by using the algebra of function you can change the y coordinate of either side of the wave.

9. sorry yes, by continuous I meant not just no breaks, but no 'bends/kinks/sharp corners'. yes, this implies differentiable and 'continuously differentiable C1 class' would be enough I believe. As for the triangle analogy, if the sides and apex are a little 'curved' it is fine...I just meant 'sort of' approaching a triangle; if it deviates from a triangle appearance more or less, it is not a deal breaker...