Hi, i have a problem regarding curve fitting. I have a set of data which is linear, but i want to fit a cos(k*l)^2 to this data and wants to find for which value of (k*l), for which the initial linear part of cosine curve fits my data?

조회 수: 3 (최근 30일)
%% Here is my code.
[data]=[0 0 0.050000000000000 1.108646244630E-01 0.100000000000000 2.217423074817E-01 0.150000000000000 3.325947375398E-01 0.200000000000000 4.434863433851E-01 0.250000000000000 5.543595496420E-01 0.300000000000000 6.652338361973E-01 0.350000000000000 7.761094191116E-01 0.400000000000000 8.869865144820E-01 0.450000000000000 9.978653384221E-01 0.500000000000000 1.108746107036E+00]';
x=data(:,1);
y=data(:,2);
k=0.3 %fixed
l=0.01 %variable, can have any value to fit the linear part
f=cos(k*l)^2 % function to fit
I will be highly grateful if anyone can help me on this problem. Thank you very much.

채택된 답변

Rik
Rik 2018년 5월 16일
편집: Rik 2018년 5월 17일
You can use the code below to choose a value for l that results in the least difference between the actual data and the output defined by f. (Also, you should avoid the use of the lowercase L as a variable name, because it looks very similar to a 1 or I (one, uppercase i))
In future, you should use the {}Code button to make sure your code in ready to copy and paste to Matlab.
data=[0 0;
0.05 1.108646244630E-01;
0.10 2.217423074817E-01;
0.15 3.325947375398E-01;
0.20 4.434863433851E-01;
0.25 5.543595496420E-01;
0.30 6.652338361973E-01;
0.35 7.761094191116E-01;
0.40 8.869865144820E-01;
0.45 9.978653384221E-01;
0.50 1.108746107036E+00];
% Objective function
k=0.3;%fixed
f=@(l,x) cos(k*l)^2.*x;% function to fit
x=data(:,1);
yx=data(:,2);
intial_b_vals=0.01;
% Ordinary Least Squares cost function
OLS = @(b) sum((f(b,x) - yx).^2);
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
% Use 'fminsearch' to minimise the 'OLS' function
fitted_b = fminsearch(OLS, intial_b_vals, opts);
For me, this results in l having an optimal value of 0.
  댓글 수: 8
Sumera Yamin
Sumera Yamin 2018년 5월 18일
Can we somehow put the limit that the argument of cosine function should be greater than 0 and less than pi/2 (0<k*l<pi/2) so that only only that portion of the curve which is of interest is considered.
Rik
Rik 2018년 5월 19일
Same idea as above:
OLS_con = @(b) sum((f(b,x) - yx).^2)+...
1/(b>0.1)-1+...
1/(0<k*b & k*b<pi/2)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by