Linear Regression and Curve Fitting

조회 수: 3 (최근 30일)
Sam
Sam 2013년 12월 4일
댓글: Wayne King 2013년 12월 4일
I have a model and some data I'd like to fit to it: X_t = B1*cos(2*pi*omega*t) + B2*sin(2*pi*omega*t) + eta_t
What function would I use to conduct linear regression here, to find B1 and B2?

채택된 답변

Wayne King
Wayne King 2013년 12월 4일
편집: Wayne King 2013년 12월 4일
Fs = 1000;
t = 0:1/Fs:1-1/Fs;
y = 1.5*cos(2*pi*100*t)+0.5*sin(2*pi*100*t)+randn(size(t));
y = y(:);
X = ones(length(y),3);
X(:,2) = cos(2*pi*100*t)';
X(:,3) = sin(2*pi*100*t)';
beta = X\y;
beta(1) is the estimate of the constant term, beta(2) the estimate of B1 and beta(3) the estimate of B2.
If you set the random number generator to its default for reproducible results:
rng default
Fs = 1000;
t = 0:1/Fs:1-1/Fs;
y = 1.5*cos(2*pi*100*t)+0.5*sin(2*pi*100*t)+randn(size(t));
y = y(:);
X = ones(length(y),3);
X(:,2) = cos(2*pi*100*t)';
X(:,3) = sin(2*pi*100*t)';
beta = X\y;
The results are:
beta =
-0.0326
1.5284
0.4643
pretty good.
  댓글 수: 2
Sam
Sam 2013년 12월 4일
Thank-you Wayne. In my case, where I have the time series data in a .dat file, am I right in thinking that I would load that in as 'y' in place of the y = "1.5*cos(2*pi*100*t)+0.5*sin(2*pi*100*t)+randn(size(t));"?
Wayne King
Wayne King 2013년 12월 4일
Yes, that's correct. Make sure you flip it to a column vector if it isn't already. Obviously you have to change the frequencies in the design matrix to suit your problem.
If you are trying to estimate other frequencies you need to add two columns to the design matrix per frequency --- one for cosine and one for sine

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jos (10584)
Jos (10584) 2013년 12월 4일
You might be interested in the function REGRESS
X = [cos(2*pi*omega*t(:)) sin(2*pi*omega*t(:))]
B = regress(Y,X)
If you want to specify an offset B(3), add a column of ones to X
X(:,3) = 1

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by