How Do You Fit a Sine Wave to Data Points?

In summary, the conversation discusses finding the sine wave of best fit for a given set of coordinates, with the end goal of creating a software function to do so. The approach involves using a least squares fit algorithm to find parameters such as frequency, amplitude, and phase. It is also mentioned that a Fourier transform can be used for more complex functions. The process is explained to be covered in statistics textbooks and various websites.
  • #1
Simoyd
3
0
So my question is, how does this work (hopefully I'm allowed to do hyperlinks):
https://www.desmos.com/calculator/zlvrts7mul

Given a table of x and y coordinates, how do I find the sin wave of best fit. I need to get f (frequency), a (amplitude), and p (phase) for the function in this form f(x) = a ⋅ sin( x ⋅ f + p )

My end goal is to have a software function that can do this. I'm really having difficulty figuring out where to start :(. I'd like to also be able to do this for cos(), but I'm not sure if I can figure out cos based on how sin works.

I'd like to understand how it's done, but at the same time I'm a bit scared that this might be way over my head. Any help would be appreciated.
 
Mathematics news on Phys.org
  • #2
It depends a bit on your data, the typical approach would be a least squares fit where you find parameters a,f,p that minimize the sum of squared deviations (or squared [deviations divided by the uncertainty], if known for each entry). There are programs to do that automatically.
It doesn't matter if you fit a sine or a cosine or any other reasonable function, the algorithm is the same.

f in your formula is the frequency multiplied by 2 pi, by the way.
 
  • #3
oh yes, 2 pi I forgot.

I guess I know that there are programs that do this. I want to have my own program do this. I'm very confused with how least squares works. I've looked it up on google a bunch but just get confused. Is there maybe a resource that you would suggest?
 
  • #4
Every statistics textbook should cover it, and various websites should have good descriptions. The basic idea is not complicated, doing that efficiently for more complex functions can be challenging but with a sine wave it should be fine.
 
  • #5
And, of course, you can use some form of a Fourier transform, FFT, DFT,...
 
  • #6
For linear models, a typical statistics book would cover this process. For nonlinear functions with parameters like you have, nonlinear quadratic optimization algorithms have been developed. They use the gradient of the quadratic error function to find the changes of parameters a, f, and p that will lead it (usually step-by-step) to values a=a0, f=f0, p=p0, that give a minimum error. It is a studied in nonlinear programming.
 

Related to How Do You Fit a Sine Wave to Data Points?

1. What is regression of a sine wave?

Regression of a sine wave is a statistical process used to analyze and model the relationship between a dependent variable and one or more independent variables that follow a sinusoidal pattern. It is a type of regression analysis commonly used in fields such as physics, engineering, and finance.

2. How is regression of a sine wave different from linear regression?

The main difference between regression of a sine wave and linear regression is the type of relationship being modeled. Linear regression is used to model relationships that are linear, while regression of a sine wave is used to model relationships that are sinusoidal in nature.

3. What are the assumptions of regression of a sine wave?

The assumptions of regression of a sine wave are similar to those of linear regression and include linearity, independence of observations, normality of residuals, and equal variance of residuals. Additionally, regression of a sine wave assumes that the relationship between the variables follows a sinusoidal pattern.

4. How is regression of a sine wave used in real-world applications?

Regression of a sine wave has various applications in real-world scenarios, such as predicting stock market trends, analyzing seasonal patterns in sales data, and predicting the behavior of natural phenomena such as weather patterns. It is also commonly used in signal processing and image analysis.

5. What are some limitations of regression of a sine wave?

One limitation of regression of a sine wave is that it can only model relationships that follow a sinusoidal pattern. If the relationship between the variables is not sinusoidal, then regression of a sine wave will not accurately predict the values. Additionally, it assumes that the relationship between the variables is constant, which may not always be the case in real-world scenarios.

Similar threads

Replies
7
Views
2K
Replies
8
Views
960
Replies
6
Views
2K
  • General Math
2
Replies
61
Views
9K
Replies
3
Views
1K
  • General Math
Replies
1
Views
746
  • General Math
Replies
2
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
2K
Replies
2
Views
2K
  • Precalculus Mathematics Homework Help
Replies
3
Views
1K
Back
Top