MATLAB: How to do “Cosine wave approximation“ for random plot data

cosine wave approximation

I want to do "Cosine wave approximation“ for randam plot data.
Is there a way to forcefully approximate the following data?
Also please tell me about how to do it.
x = [0.087266463 0.261799388 0.436332313 0.610865238 0.785398163 0.959931089 1.134464014 1.308996939 1.483529864];
y = [22 22 16 13 13 9 3 0 3];

Best Answer

easy peasy. Though I have no clue what it means to "forcef\ully approximate".
x0 = [mean(y),max(y) - min(y)/2,0,3];
ft = fittype('a + b*cos((x-c)*d)')
ft =
General model:
ft(a,b,c,d,x) = a + b*cos((x-c)*d)
mdl = fit(x',y',ft,'startpoint',x0)
mdl =
General model:
mdl(x) = a + b*cos((x-c)*d)
Coefficients (with 95% confidence bounds):
a = 11.96 (7.416, 16.51)
b = 10.43 (4.97, 15.88)
c = -0.02594 (-0.8867, 0.8348)
d = 2.079 (0.141, 4.017)
plot(x,y,'o'),hold on,plot(mdl)
untitled.jpg