MATLAB Answers

0

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?

Asked by Sumera Yamin on 16 May 2018
Latest activity Commented on by Rik Wisselink
on 19 May 2018

%% 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 Comments

Sign in to comment.

1 Answer

Answer by Rik Wisselink
on 16 May 2018
Edited by Rik Wisselink
on 17 May 2018
 Accepted Answer

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 Comments

You need to read the doc for anonymous functions. Those should cover most of your question here. Setting a limit on the max value of l will not help, because it will go to 0 (unless you set a negative value as the max). Setting a constraint is not possible with this function, but you can add a term that sets the result to infinity:

OLS_con = @(b) sum((f(b,x) - yx).^2)+...
    1/(b>0.1)-1;%set cost function to inf when (b>0.1) is false

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.

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)

Sign in to comment.