How to do 'cos' curve fitting for data obtained.

18 views (last 30 days)
Hello everyone,
I have been trying to do curve fit for the data given below.
I want to implement 'cosine square' curve fitting for the data obtained and later find the peak. I have already written a program for it and obtained the graph below as attached. But I am unsure if I am correct in implementing it, so that I can find a peak.
Can someone please check my programme if it is correctly implemented and latter how can I find the peak?
The program and also the data is attached.
I request to please help in setting the cosine square curve fitting and also how can I find peak from it please.
Thanking you in advance.
Kind regards
x_axis = [linspace(0, 360, 360)'];
y_axis =
[3.7560368
3.8085489
3.7661638
3.7799058
3.8907118
3.6859937
3.8746176
3.8137977
3.8134153
4.0203352
4.1017566
4.0970173
3.9302003
4.1788678
4.2053556
4.1945562
4.2701845
4.2767544
4.4449506
4.2852941
4.3978033
4.4853888
4.6127644
4.7098279
4.8618488
5.1263094
5.3121061
5.2374048
5.4019165
5.3228474
5.7068501
5.8063822
5.6671882
6.0611329
6.3209352
6.3571095
6.3894024
6.3625441
6.7195029
6.9633155
7.1388698
7.1086278
7.3036475
7.7009292
7.5671926
7.8018169
7.8800983
7.9435253
8.2067919
8.0853844
8.4278364
8.5053186
8.7994070
8.8510847
8.8690329
9.1009340
9.0523500
9.2792454
9.1106558
9.3377943
9.3589869
9.5377388
9.5682468
9.6855421
9.9991894
10.101757
10.096369
10.156980
10.274252
10.360273
10.509270
10.480047
10.654547
10.673642
10.609531
10.812882
10.907791
10.918486
11.225864
11.290207
11.172333
11.488586
11.520347
11.551353
11.549279
11.526500
11.671359
11.850876
12.058664
12.165114
12.094005
12.211543
12.288712
12.303370
12.436886
12.596936
12.663608
12.708924
12.855083
12.925530
13.090031
13.018736
13.070112
13.107990
13.310437
13.422553
13.567563
13.744937
13.490406
13.864213
13.847771
13.847562
14.041366
14.147572
14.082581
13.991263
14.123412
14.369276
14.390491
14.520578
14.327042
14.693224
14.133552
14.292513
14.025004
14.026464
13.991936
13.935472
13.823612
13.694707
13.577332
13.184267
13.157513
12.778562
12.687349
12.503510
12.076763
11.935090
11.697372
11.518528
11.050751
10.501066
10.570576
10.255017
10.040149
9.6180134
9.4150209
9.1862717
9.1615105
8.8085833
8.4177094
8.1676283
7.9493303
7.8774567
7.4021716
7.2626643
7.0130467
6.8367281
6.5165462
6.2733707
6.0217953
5.8303905
5.7666621
5.5948973
5.3705506
5.0788374
4.8748956
4.9039559
4.7801609
4.5220037
4.4644861
4.3326731
4.1139574
4.0280752
4.0393724
3.9195518
3.8203096
3.9346380
3.9120667
3.9352870
3.7858152
3.7302327
3.8530660
3.8764021
4.0210533
3.8053162
3.7861629
4.0157118
3.8974786
4.0198369
3.8873518
4.0392332
4.1915207
4.2629776
4.1356020
4.2647386
4.4922714
4.5622220
4.4029360
4.5087366
4.6259847
4.6793890
5.0826612
4.8744206
5.1262283
5.2914929
5.4136310
5.5176930
5.6838617
5.7043476
6.1272130
6.2987809
6.3886957
6.7363043
6.4276047
6.7431059
6.8973627
7.0461044
7.4170375
7.5335674
7.6382785
7.5001392
7.8367167
7.8627295
8.1570034
8.2677975
8.3691025
8.3157787
8.6507921
8.5380859
8.7457590
8.8766565
8.9957819
9.4086370
9.2976923
9.1666203
9.5562086
9.4445915
9.5937271
9.5391989
9.7914810
9.7707987
10.014866
9.8694038
10.086926
10.070402
10.140689
10.358998
10.547773
10.397189
10.616878
10.850748
10.820518
10.890109
10.967093
10.936852
10.996153
11.312604
11.382068
11.275920
11.237150
11.464695
11.733998
11.555235
11.594063
11.750486
11.719097
11.809939
12.001703
12.123749
12.068143
12.305802
12.398463
12.620493
12.616426
12.758910
12.791411
12.814006
12.958403
13.033417
12.982018
13.097528
13.326486
13.099764
13.440501
13.762190
13.757254
13.685774
13.790647
13.996119
13.993882
14.195066
14.017554
13.993523
14.137665
14.068213
13.908880
14.126356
14.090123
13.816242
13.845986
13.771297
14.081943
13.869555
13.688763
13.756604
13.374838
13.495052
13.327528
13.196979
12.910977
12.892890
12.608303
12.453722
12.261135
11.895277
11.831989
11.474671
11.331120
11.078547
10.780775
10.553984
10.312628
10.234334
9.8746061
9.6202841
9.2517500
8.9077330
8.5553513
8.2447739
8.2016010
8.0945034
7.6051397
7.3414907
7.1926098
6.7445426
6.6355791
6.5867977
6.2250996
6.1827264
5.7160850
5.7975760
5.6851592
5.3907700
5.2925820
5.1095896
4.7592812
4.7868695
4.6418242
4.3699365
4.3055477
4.3884525
4.1613369
4.0429530
3.8451636
4.0363250
4.0363483
3.7999976
3.8972006
3.9059372]
  3 Comments
Alex Sha
Alex Sha on 24 Apr 2024
@Paramjit Yadav The original fitting function is not good for your data, try the function below:
Fitting result:
Sum Squared Error (SSE): 6.91129594176633
Root of Mean Square Error (RMSE): 0.1385570073388
Correlation Coef. (R): 0.999222888083962
R-Square: 0.998446380070854
Parameter Best Estimate
--------- -------------
a1 0.238948803230066
b1 9.42672548601341
c1 0.795056999679539
a2 1.20658677913231
b2 -14.2617939394349
c2 -1.95421762967456
a3 -4.79700609782976
b3 -28.3377665740981
c3 140.304593586862
d 9.03378373900727
Paramjit Yadav
Paramjit Yadav on 13 May 2024
Dear Alex
Thank you so much for repyling and help. I would like to know how did you implement the code to get all the values. Sum, root......
Can you please give me the code which you implemented.
Thanking you,
Kind regards
Paramjit

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 24 Apr 2024
Edited: Mathieu NOE on 25 Apr 2024
hello
you can do a cos/sin series approximation , using this simple code :
here I opted for 6 terms :
x_axis = [linspace(0, 360, 360)'];
y_axis = [3.7560368
3.8085489
3.7661638
3.7799058
3.8907118
3.6859937
3.8746176
3.8137977
3.8134153
4.0203352
4.1017566
4.0970173
3.9302003
4.1788678
4.2053556
4.1945562
4.2701845
4.2767544
4.4449506
4.2852941
4.3978033
4.4853888
4.6127644
4.7098279
4.8618488
5.1263094
5.3121061
5.2374048
5.4019165
5.3228474
5.7068501
5.8063822
5.6671882
6.0611329
6.3209352
6.3571095
6.3894024
6.3625441
6.7195029
6.9633155
7.1388698
7.1086278
7.3036475
7.7009292
7.5671926
7.8018169
7.8800983
7.9435253
8.2067919
8.0853844
8.4278364
8.5053186
8.7994070
8.8510847
8.8690329
9.1009340
9.0523500
9.2792454
9.1106558
9.3377943
9.3589869
9.5377388
9.5682468
9.6855421
9.9991894
10.101757
10.096369
10.156980
10.274252
10.360273
10.509270
10.480047
10.654547
10.673642
10.609531
10.812882
10.907791
10.918486
11.225864
11.290207
11.172333
11.488586
11.520347
11.551353
11.549279
11.526500
11.671359
11.850876
12.058664
12.165114
12.094005
12.211543
12.288712
12.303370
12.436886
12.596936
12.663608
12.708924
12.855083
12.925530
13.090031
13.018736
13.070112
13.107990
13.310437
13.422553
13.567563
13.744937
13.490406
13.864213
13.847771
13.847562
14.041366
14.147572
14.082581
13.991263
14.123412
14.369276
14.390491
14.520578
14.327042
14.693224
14.133552
14.292513
14.025004
14.026464
13.991936
13.935472
13.823612
13.694707
13.577332
13.184267
13.157513
12.778562
12.687349
12.503510
12.076763
11.935090
11.697372
11.518528
11.050751
10.501066
10.570576
10.255017
10.040149
9.6180134
9.4150209
9.1862717
9.1615105
8.8085833
8.4177094
8.1676283
7.9493303
7.8774567
7.4021716
7.2626643
7.0130467
6.8367281
6.5165462
6.2733707
6.0217953
5.8303905
5.7666621
5.5948973
5.3705506
5.0788374
4.8748956
4.9039559
4.7801609
4.5220037
4.4644861
4.3326731
4.1139574
4.0280752
4.0393724
3.9195518
3.8203096
3.9346380
3.9120667
3.9352870
3.7858152
3.7302327
3.8530660
3.8764021
4.0210533
3.8053162
3.7861629
4.0157118
3.8974786
4.0198369
3.8873518
4.0392332
4.1915207
4.2629776
4.1356020
4.2647386
4.4922714
4.5622220
4.4029360
4.5087366
4.6259847
4.6793890
5.0826612
4.8744206
5.1262283
5.2914929
5.4136310
5.5176930
5.6838617
5.7043476
6.1272130
6.2987809
6.3886957
6.7363043
6.4276047
6.7431059
6.8973627
7.0461044
7.4170375
7.5335674
7.6382785
7.5001392
7.8367167
7.8627295
8.1570034
8.2677975
8.3691025
8.3157787
8.6507921
8.5380859
8.7457590
8.8766565
8.9957819
9.4086370
9.2976923
9.1666203
9.5562086
9.4445915
9.5937271
9.5391989
9.7914810
9.7707987
10.014866
9.8694038
10.086926
10.070402
10.140689
10.358998
10.547773
10.397189
10.616878
10.850748
10.820518
10.890109
10.967093
10.936852
10.996153
11.312604
11.382068
11.275920
11.237150
11.464695
11.733998
11.555235
11.594063
11.750486
11.719097
11.809939
12.001703
12.123749
12.068143
12.305802
12.398463
12.620493
12.616426
12.758910
12.791411
12.814006
12.958403
13.033417
12.982018
13.097528
13.326486
13.099764
13.440501
13.762190
13.757254
13.685774
13.790647
13.996119
13.993882
14.195066
14.017554
13.993523
14.137665
14.068213
13.908880
14.126356
14.090123
13.816242
13.845986
13.771297
14.081943
13.869555
13.688763
13.756604
13.374838
13.495052
13.327528
13.196979
12.910977
12.892890
12.608303
12.453722
12.261135
11.895277
11.831989
11.474671
11.331120
11.078547
10.780775
10.553984
10.312628
10.234334
9.8746061
9.6202841
9.2517500
8.9077330
8.5553513
8.2447739
8.2016010
8.0945034
7.6051397
7.3414907
7.1926098
6.7445426
6.6355791
6.5867977
6.2250996
6.1827264
5.7160850
5.7975760
5.6851592
5.3907700
5.2925820
5.1095896
4.7592812
4.7868695
4.6418242
4.3699365
4.3055477
4.3884525
4.1613369
4.0429530
3.8451636
4.0363250
4.0363483
3.7999976
3.8972006
3.9059372];
%% harmonic series decomposition
orders = 1:6; % define up to wich order you want to proceed
% model fit : X = sum(A*cos(orders(k)*theta) + B*sin(orders(k)*theta)) + C
C = mean(y_axis);
% loop over the A,B cos and sin components
theta = x_axis*pi/180;
yfit = 0;
n = numel(theta);
for k = 1:numel(orders)
A = 2/n*trapz(y_axis.*cos(orders(k)*theta));
B = 2/n*trapz(y_axis.*sin(orders(k)*theta));
yfit = yfit + A*cos(orders(k)*theta) + B*sin(orders(k)*theta);
end
yfit = yfit + C; % add the constant term
% plot
figure(1),
plot(x_axis, y_axis, 'b',x_axis, yfit, 'r')
legend('data','series fit');
  7 Comments
Paramjit Yadav
Paramjit Yadav on 14 May 2024
Thank you so much for help. This is helpful.
But I wanted something where I can use "fitoptions and fittype"and then gain coefficients and error bars. I have attached two programs for example.
I kindly request if you can help me something with something like this. I will be very very thankful to you.
Thanking you,
Kind regards
Paramjit
Mathieu NOE
Mathieu NOE on 14 May 2024
hello again
unfortunately I don't have the Curve Fitting Tbx so I cannot do this

Sign in to comment.

More Answers (0)

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!