Fitting a Sine Curve to Two Points

In summary, Ryan is trying to solve an equation that gives the position of an element as a function of time, of the form f(x) = sin(ax+b)+c. He is a little rusty on the details, but if anyone would be so kind as to offer some clarification and save him a few more days of trial and error, he would greatly appreciate it.
  • #1
Alterscape
3
0
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!

-Ryan
 
Physics news on Phys.org
  • #2
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:
  • #3
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.
 
  • #4
Clarifications

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 each other, 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:
  • #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

P(t)=a*sin(bx+c)+d

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:
  • #6
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.
 

1. How do you determine the amplitude and period of a sine curve?

The amplitude of a sine curve is the distance from the middle line to the highest or lowest point on the curve. To determine the amplitude, measure this distance and divide it by 2. The period of a sine curve is the length of one full cycle, which is equal to 2π divided by the frequency. The frequency is the number of complete cycles in one unit of time.

2. What is the process for fitting a sine curve to two points?

To fit a sine curve to two points, you will need to first determine the amplitude and period of the curve using the two given points. Then, use the sine function with these values to create an equation for the curve. Finally, plot the points and the curve on a graph to ensure that they match up.

3. Can a sine curve be fitted to more than two points?

Yes, a sine curve can be fitted to any number of points. However, the more points that are used, the more accurate the curve will be. It is recommended to use at least 3 points to fit a sine curve.

4. What is the importance of fitting a sine curve to two points in scientific research?

Fitting a sine curve to two points allows scientists to analyze and predict patterns and trends in data. This can be useful in various fields of research such as physics, biology, and economics. By fitting a sine curve, scientists can make more accurate predictions and understand the behavior of a system or phenomenon.

5. Are there any limitations to fitting a sine curve to two points?

Yes, there are limitations to fitting a sine curve to two points. This method assumes that the data follows a perfect sine wave pattern, which is not always the case in real-world situations. Additionally, fitting a curve to only two points may not accurately represent the entire data set and can lead to errors in predictions. It is important to use multiple data points and consider other factors when fitting a sine curve.

Similar threads

Replies
2
Views
2K
Replies
3
Views
1K
  • Calculus
Replies
9
Views
2K
  • Calculus
Replies
3
Views
974
Replies
3
Views
2K
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
Replies
3
Views
304
Replies
4
Views
1K
Back
Top