Curve fitting to a sinusoidal function

조회 수: 1,101 (최근 30일)
Dejan
Dejan 2014년 3월 14일
댓글: Star Strider 2023년 5월 2일
I have a series of data points that are governed by a sinusoidal function.
I want to fit, plot and generate a sinusoidal function to these data points.
I do not wish to fit an nth degree polynomial to this no matter how close it is to the sinusoidal function.
I understand that there is no standard tool in the toolbox that does this. I have looked at numerous and plenty of old threads and internet posts. None of the answers help me.
Please take into account that I am new to Matlab and can only curve fit very basic data points.
What I therefore need is an exact and step by step guide in how to fit a sine curve to data points.
Please assist.
  댓글 수: 1
Douglas Lim
Douglas Lim 2016년 2월 29일
Hi Star Strider, I have further question on this topic. May I know how to contact you for sending you the problem.

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

채택된 답변

Star Strider
Star Strider 2014년 3월 15일
편집: Star Strider 2014년 3월 15일
Here’s my suggested solution, using only core MATLAB functions:
yu = max(y);
yl = min(y);
yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);
zx = x(yz .* circshift(yz,[0 1]) <= 0); % Find zero-crossings
per = 2*mean(diff(zx)); % Estimate period
ym = mean(y); % Estimate offset
fit = @(b,x) b(1).*(sin(2*pi*x./b(2) + 2*pi/b(3))) + b(4); % Function to fit
fcn = @(b) sum((fit(b,x) - y).^2); % Least-Squares cost function
s = fminsearch(fcn, [yr; per; -1; ym]) % Minimise Least-Squares
xp = linspace(min(x),max(x));
figure(1)
plot(x,y,'b', xp,fit(s,xp), 'r')
grid
The elements of output parameter vector, s ( b in the function ) are:
s(1): sine wave amplitude (in units of y)
s(2): period (in units of x)
s(3): phase (phase is s(2)/(2*s(3)) in units of x)
s(4): offset (in units of y)
It provides a good fit.
  댓글 수: 41
Hector Camilo Clavijo Zarate
Hector Camilo Clavijo Zarate 2023년 5월 2일
@Star StriderWonderful! thank you for all your help
Star Strider
Star Strider 2023년 5월 2일
My pleasure!
A Vote would be appreciated!

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

추가 답변 (2개)

Chad Greene
Chad Greene 2018년 7월 21일
I turned this into a function called sinefit for climatological data with a periodicity of 1 year.

Jos (10584)
Jos (10584) 2014년 3월 14일
  댓글 수: 1
Dejan
Dejan 2014년 3월 15일
Im relatively new to Matlab and whilst the link provided I kinda get, its not an exact step by step guide on how to fit a Sine wave.
Here are my data points:
x = [ 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 ]
y = [ 16.5 14.32 11.58 10.017 9.629 10.2 12.16 15.08 16.97 16.75 14.331 11.508 10.013 9.617 10.22 12.15 15.304 17.38 16.853 ]
I need a Sine wave to fit that data and its governing equation

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

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by