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?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
%% 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.
0 Commenti
Risposta accettata
Rik
il 16 Mag 2018
Modificato: Rik
il 17 Mag 2018
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 Commenti
Rik
il 19 Mag 2018
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)
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Historical Contests in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!