Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fitting a Sine Curve to Two Points

  1. Jun 29, 2004 #1
    I'm a web developer working on a bit of javascript code for a personal project. I took through Calc II and linear algebra in college, but haven't looked at any of it a couple of years, so I'm a little rusty on the details. Anyway, what I'm trying to work out is a way to scale/transform an equation giving the position of an element as a function of time, of the form f(x) = sin(ax+b)+c , so that it has a given f(x) and f'(x) for two given values of x, and f''(x) at one of those values. If I remember right, I'm going to need to set up a system of equations including the function and both derivatives and solve it to calculate the necessary values of a, b, and c, but I'm a bit rusty on the details. If anyone would be so kind as to offer some clarification and save me a few more days of trial and error, I'd greatly appreciate it. Thanks!

  2. jcsd
  3. Jun 29, 2004 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    First off if you may want to use a more general form of the sin equation.

    f(x)= A sin(bx) +c

    It is not clear to me that you need a phase factor (your b) simply because it represents a shift on the coordinate axis.
    However, you may well need an Amplitude (my A) This will allow your resultant function to have an amplitude other then 1 (in what ever units you choose)

    If this:

    [tex] f(x) = A \sin(Bx) + C [/tex]

    [tex] f(x_1) = h; \ f'(x_1)=k [/tex]
    [tex] f(x_2) = a; \ f'(x_2)=b; \ f''(x_2)=n [/tex]

    is the problem you are trying to solve, then you may have some troubles. There is no guarantee that you will be able to find a solution to fit all of those conditions.

    Perhaps you can give us a bit more detail about what you are trying to do.
    Last edited: Jun 29, 2004
  4. Jun 29, 2004 #3


    User Avatar
    Science Advisor

    You have 5 "pieces of information" so your function prototype will have to have 5 "unknown numbers".

    Neither f(x)= sin(ax+b)+ c nor f(x)= A sin(Bx)+ C have enough "flexibility" to fit all 5 requirements.
  5. Jun 29, 2004 #4

    Sorry for being unclear. First of all, yes, a scaling factor for amplitude would be necessary, and I have no idea why that didn't occur to me earlier. I think it was mosly because in the simpler code I wrote originally, I used the position function to generate a percentage between 0 and 1 and multiplied that by the change in position, and added it to the initial position -- which is the same thing you're suggesting here, just in a more convoluted, less broadly useful way.

    Anyway, to summarize what I'm trying to do.. sinusoidals are great for smooth animation -- if you use a sinusoidal as a function that determines the position of an element, with the initial position at the trough and the final position at the next peak (or vice-versa) you get a beautifully smooth soft-start/stop. But what happens if, during a long animation of this sort, the user triggers an event, like clicking a button, etc (the setup I'm creating is part of an elaborate menu system) which changes the position the animation is moving to and/or the time it will take to arrive? At that point you need to smoothly transition to a new curve describing this new animation. (In this case, I'm ignoring what happens if a reverse of motion is needed -- once I work out slowing the animation to a stop, tacking on a new sinecurve for the reverse motion is relatively simple).

    For a transition between two curves to be smooth, I remember from calc that f1(x1)=f2(x1), f1'(x1)=f2'(x1), and f1''(x1)=f2''(x1) , where x1 is the point at which the two functions transition. That's the first condition. Then, at x2, I need to specify that f2(x2) = the new position target, f2'(x2) = 0, because that should be the peak or trough of the sinewave, but f2''(x2) doesn't concern me because, so long as it reaches the position with a velocity of zero, I don't care what it was accelerating at.

    so maybe a better general form would be p(t) = a * sin(bx+c) + d.

    Is this impossible?

    edit: I worked out a system of 5 equations with 4 unknowns that I think I should be able to solve.. someone care to sanity-check me here?

    Pi = a*sin(b*Ti+c)+d
    Vi = a*b*cos(b+Ti+c)
    Ai = -a*b^2*sin(b+Ti+c)
    Pf = a*sin(b*Tf+c)+d
    0 = a*b*cos(b*Tf+c)+d

    Where Pi = known initial position, Vi = known initial velocity, Ai = known initial acceleration, Ti = known initial time, Pf = known final position, and Tf = known final time. The unknowns are a, b, c, and d. Note that the velocity and acceleration functions are, of course, the 1st and 2nd derivative of the position function.

    I've currently worked the two position functions around to get

    sin(b*Tf+c) - sin(b*Ti+c) = (Pf-Pi)/a

    by setting both equal to d, and thus equal to eachother, and doing some simple arithmetic, but I forget how to use the arcsin to get rid of the sines on the left side of the equation.. again, any help appreciated.
    Last edited: Jun 29, 2004
  6. Jul 2, 2004 #5
    I'm not sure how bump ettiquette works on this forum, but I'm going to bump once since I'm totally lost and could use some help.

    In response to HallsOfIvy, the function


    provides 5 variables to alter the sinewave into any possible configuration that doesn't involve rotating it, so far as I know. The system of equations I've worked out has 5 equations for 5 unknowns, so theoretically, it should be solvable. I'm just very rusty on algebra w/trig functions.. is there a simple way to solve this system (ie, some relatively-easily-available software or something.. I have a couple of contacts who do engineering work with Matlab/Mathematica, is this the sort of thing they could write a simple script to solve?)

    I also realized that I need to note that I'm only looking for solutions on the interval [0, 2Pi].
    Last edited: Jul 2, 2004
  7. Jul 3, 2004 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    What you are doing is called splining, it is not clear to me that you need to use sin functions to achieve the desired results. Polynomial splines are pretty common and will do what you need. If you match position, velocity and acceleration, your transitions will be smooth no matter what functions you use. Use the simplest possible to achieve the desired results.

    Also in your last general formal, P(t)=a*sin(bx+c)+d, There are only 4 parameters to be determined. x is the independent variable, not a parameter.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook