Fitting sine curve to data
이전 댓글 표시
Dear matlab community,
I'm tring to fit a few data points to the following equation (i.e.: "y = a+b*sin(x-c)")
my "x" and "y" values are:
x = [0,15,30,45,60,75,90,105,120,135,150,165,180,195,210,225,240,255,270,285,300,315,330,345,360];
y = [-0.267268221867135,-0.224927234804625,-0.189541103134544,-0.138342286610894,-0.0566423048346959,0.00234570638697967,0.0651873879993542,0.127045214064366,0.187359039666987,0.221423324938789,0.256601680672827,0.255278492431207,0.267669889966578,0.232962110292877,0.197299723252563,0.130461415017413,0.0624973744048143,-0.00389440045935707,-0.0720465871266569,-0.132628373295696,-0.194175941787212,-0.234779045975818,-0.254184352991033,-0.252575588397041,-0.245570829777601];
So far I tried to use the custom equation from the curve fitting tool. Yet, the curve is not properly adjusted. Yet, by using the "sum of sin" fitting tool, it works well. However with a diferent equations ("y = a*sin(b*x+1)").
I found some information posted by @Star Strinder showing a way to fit the data to an equation like a +b*sin(2*pi*x./c + d). The code works fine. Yet the equation is still diferent from the one I want.
Is it possible to rearrange the code to get the equation I want?
The code is below:
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
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

