# Finding a least squares fit for f(x)=A*cos(Bx)

#### leaf man

##### New Member
Hello,

I'm trying to derive the solutions for the estimates of A and B for the equation f(x)=A*cos(Bx) or ...cos(Bx+C) using least squares. I have found estimates for A and B using numerical iteration of the dataset I'm working with but I can't seem to derive a solution for A and B that matches my numerical estimates of these parameters. Any help would be great!

Thanks!

#### Dragan

##### Super Moderator
Hello,

I'm trying to derive the solutions for the estimates of A and B for the equation f(x)=A*cos(Bx) or ...cos(Bx+C) using least squares. I have found estimates for A and B using numerical iteration of the dataset I'm working with but I can't seem to derive a solution for A and B that matches my numerical estimates of these parameters. Any help would be great!

Thanks!

I don't think your going to be able to find (ordinary least squares) closed formed solutions to A and B because your model is non-linear in parameters i.e. cos(Bx).

#### vinux

##### Dark Knight
For the smaller size data(sample size < 6 or more) you may get multiple solutions for this eqn. But for Large data most proabably you will get unique solution.
Because
Here we wanted to estimate the closest Wave function. A will adjust the height and B will adjust wavelength.
I hope you have used the numerical stuff following way
Y= A Cos(BX)
E = SUM( [ Yi - A Cos(B*Xi)]^2 )
I am denoting partial differentiation of E by A by Da
Da(E) = SUM( 2* [ Yi - A Cos(B*Xi)] Cos(B*Xi) ) ---------------eqn(1)
Db(E) = SUM( 2* [ Yi - A Cos(B*Xi)] A*Sin(B*Xi)*Xi ) ---------------eqn(2)
Da(E) =0 => SUM( Yi * Cos(B*Xi) ) = A* SUM( Cos(B*Xi)] Cos(B*Xi) )
ie A = SUM( Yi * Cos(B*Xi) )/ SUM( Cos(B*Xi)] Cos(B*Xi) ) ----------------------eqn(3)
Db(E) =0 => SUM( 2* [ Yi - A Cos(B*Xi)] A*Sin(B*Xi)*Xi ) =0
Db(E) =0 => SUM( 2* [ Yi - A Cos(B*Xi)] Sin(B*Xi)*Xi ) =0 ----------------------eqn(4) ( It is not true Since A = 0)
(use the trigonametric relationship Sin2A = 2 SinA CosA and simplify)
Now start the iteration put B=0 and give A =Mean(Yi) ( I think this will be one of the best starting point)
Use this in eqn(4) and Solve B
I know it is bit tough. use the numeric stuff again. then continue the iteration till the estimates reaches the your accuracy level.
One thing.... In Your Solution A must lie between ( Min(Yi), Max(Yi) )
This is after a long time I am doing a math problem. So check the derivative expressions stuff .

Last edited:

#### Dragan

##### Super Moderator
For the smaller size data(sample size < 6 or more) you may get multiple solutions for this eqn.

Vinux: My interpretation is that he (or she) is asking for an analytical closed formed (OLS) solution to the problem. In short, you're not going to get one - Mkay.

#### leaf man

##### New Member
Thank you both Dragan and Vinux!

I was afraid that since "B" is where it is in the f(x)=AcosBx that it could not be solved using least-squares [i.e. looking for the equivalent of the estimates for A=SUM(x^2)SUM(y)-SUM(x)SUM(y)/N(SUM(x^2)-(SUM(x))^2 and B=... for the basic equation f(x)=A+Bx but just from the f(x)=AcosBx] and must be estimated numerically. Vinux, I've tried something similar to what you've suggested following the form of the derivation A and B for the OLS solution. Reading your you solution, I think I might have screwed up when I went through it so thank for the feedback and I will give your approach a try.

I've been convinced for a few days now that there is an analytical solution for f(x)=AcosBx using OLS but I'm beginning to agree that it's not possible. That should probably have been obvious to me from the outset given that I'm looking for cosBx as opposed to Bcosx. Out of curiosity, do either of you or anyone else have a sense of how the problem might be solved using something like logistic regression, etc.? (I have less experience with this and don't have a good sense of the utility of this type of approach in this case). Or will I be barking up a similar tree?

Thank you!