H
H
hudrogen2015-11-05 13:41:57
Programming
hudrogen, 2015-11-05 13:41:57

How to approximate a sinusoid in three parameters?

Hello! There is a set X from 0 to n with a certain step 360/n and a set Y corresponding to those X
Y = a + b*sin(X + c) . There is no coefficient before X, because it is known that the period is always equal to 2pi
.
Analysis of the same question on another resource:
www.cyberforum.ru/algorithms/thread1553818.html

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mrrl, 2015-11-05
@hudrogen

If X goes with a uniform step, then LSM is not needed.
First reduce the problem to Y=a+d*sin(X)+f*cos(X).
It's pretty obvious that a=sum(Y)/n (because sum(sin(X))=sum(cos(X))=0).
Next, multiply both sides of the original formula by sin(X):
sum(Y*sin(x))=a*sum(sin(X))+d*sum(sin(X)^2)+f*sum(sin (X)*cos(X))
Conditions sum(sin(X))=sum(sin(X)*cos(X))=0, sum(sin(X)^2)=n/2 (if n > 2). Hence d=sum(Y*sin(X))*2/n. Similarly, f=sum(Y*cos(X))*2/n.

S
sivabur, 2015-11-05
@sivabur

lab.polygonal.de/?p=205
is it?

A
Andrey Druzhaev, 2015-11-05
@borodaturan

For such things, python is very convenient with its mathematical libraries numpy and scipy.
Here is the code that solves your problem of finding unknown coefficients:

import numpy as np
from scipy.optimize import curve_fit

test_a = 1.7
test_b = -5.689
test_c = 1.2456


def test_sin(x, a, b, c):
    return a + b * np.sin(x + c)

if __name__ == '__main__':
    x = np.array(range(10))   
    y = test_sin(x, test_a, test_b, test_c)

    params, cov = curve_fit(test_sin, x, y)
    print(params)

Link to the description of the curve_fit function .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question