r squared comparing sine waves

2 views (last 30 days)
C.G.
C.G. on 18 May 2022
I have a dataset to which I have asked matlab to fit a sine wave. I want to find the r^2 value to see how well the sine curve I have generated fits the data.
Is there a simple way of doing this?
x1 = time';
y = Qe_mean;
yu = max(y);
yl = min(y);
yr = (yu-yl);
yz = y-yu+(yr/2);
zx = time(yz .* circshift(yz,[0 1]) <= 0);
P = period;
ym = mean(Qe_mean);
fit = @(b,x) b(1).*(sin(2*pi*x./P + 2*pi/b(2))) + b(3);
fcn = @(b) sum((fit(b,x1) - y).^2);
s = fminsearch(fcn, [yr; -1; ym])
xp = linspace(min(x1),max(x1),P);
figure()
plot(xp,fit(s,xp), 'b')
hold on
plot(time,y,'k','Linewidth',1)

Answers (0)

Community Treasure Hunt

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

Start Hunting!