MATLAB Answers


Regression Fit a SinWave to a dataset?

Asked by Clifford Shelton on 1 May 2012

Ok..I'm almost there. Yesterday I received some help but still have questions (I'm an ultra beginner).

I love baseball and have a data set with the amount of runs scored by the Pittsburgh Pirates for their last 501 games. The data set is sampled by 1. I completed a spectral analysis on that data set and a 3.5678 game cycle is identified in the data set. I would like to know exactly how to: 1.) generate the sin/cos wave with a period of 3.5678 games 2.) fit the newly created sin/cos wave to the Score data set.

Any code or other insight will be MOST appreciated.

  1 Comment


No products are associated with this question.

3 Answers

Answer by Wayne King
on 1 May 2012
 Accepted answer

Something is probably off in your spectral analysis in identifying the frequency. You have 501 points, which means the spacing between frequencies is 1/501. The closest you can actually get to a period of 3.5678 is 0.2794 cycles/game. So let me use that

 y = score(:); %make sure score is a column vector
 X = ones(501,3);
 n = 0:500;
 X(:,2) = cos(2*pi*0.2794*n)';
 X(:,3) = sin(2*pi*0.2794*n)';
 betahat = X\y;

Fit is

   yhat = betahat(1)+betahat(2)*cos(2*pi*0.2794*n)+betahat(3)*sin(2*pi*0.2794*n);

If you have the Statistics Toolbox

 [b,bint,r,rint,stats] = regress(y,X);

returns useful information in stats for example:

the R-square statistic, the F statistic and p value for the full model, and an estimate of the error variance.

If you have the Statistics Toolbox and R2012a, you can use

   mdl =,y)

which outputs a table. You can get the coefficients with:



how did you get the .2794 number?
Is there a way to calculate that?
Sorry if my questions seem retarded...I'm so new to this!

Also..for some reason..I cannot complete
%betahat = X/y;
without getting the error
"??? Error using ==> mldivide
Matrix dimensions must agree."

what am I doing wrong?

Answer by Wayne King
on 1 May 2012

Because when you do a spectral analysis, the frequency spacing in your case is 1/501. So you only get frequency estimates at 0, 1/501, 2/501,3/501, 4/501. So I was assuming that your spectral analysis really revealed a peak at the closest Nyquist frequency (a multiple of 1/501).

If you want, you can use 0.2803 which is 1/3.5678 for the frequency. That should not change the results much I would think.


He gave you the hint... it is a multiple of 1/501

140/501 = 0.2794...

He chose this because ABS(140/501 - 1/3.5678) < ABS(141/501 - 1/3.5678), which means 140/501 is closer to the frequency you are after.

Honestly, I'm very new to that "hint" didn't register with me.

I'm still having trouble with the error:
??? Error using ==> mldivide
Matrix dimensions must agree.

After looking at the values of my variables it seems that the X variable has a value of <501x3 double> while the y variable has a value of <501x1 double>. Could the two different matrix values be the problem?

When I copy and past your code it works. But when I try to use the data from my dataset i get the error.
Not sure why.

Answer by Wayne King
on 1 May 2012

You are not using what I gave you. You are using

 %betahat = X/y; 

Look at the example I gave you:

 y = score(:); %make sure score is a column vector
 X = ones(501,3);
 n = 0:500;
 X(:,2) = cos(2*pi*0.2794*n)';
 X(:,3) = sin(2*pi*0.2794*n)';
 betahat = X\y;

It's very important which slash you use.


How right you are!! Thanks!

Ok. I don't fully understand how you knew that the number needed to caclulatea cycle of 3.5678 games was .2794.
So my question would be what is the easiest way for me to caclulate what I would need to use for cycles identified by my spectral analysis as being:
31.19552 games
5.87064 games
8.00192 games
4.64491 games

Do I just use the same 1/501 calculation? That doesnt' seem tob e right. Please help! Thanks!!

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!