lsqcurvefit initial point is always a local minimum, relative first-order optimality is always = 0
4 views (last 30 days)
Show older comments
I'm trying curve fit 8 datapoints to a cos wave using 2 variable parameters v:
v0 = [omega 3];
the range of datapoints I need to evaluate for this iteration is:
cur_data_wave_x = 0:p_locs(2)-p_locs(1);
in this case, the above is equal to 0:7
my function is:
F = @(v,cur_data_wave_x) amplitude*(offset + cos(v0(1)*(cur_data_wave_x) + v0(2)));
where amplitude, offset, and omega are constants. Im trying to find the frequency v(1) and phase shift v(2) of the datapoints compared to a theoretical curve F.
then I use lsqcurvefit:
[v,resnorm] = lsqcurvefit(F,v0,cur_data_wave_x,cur_data_wave_y);
where cur_data_wave_y is the raw data at cur_data_wave_x (shown in blue on graph below)
No matter how close to the correct values I select the initial values to be, no matter what the resnorm, no lsqcurvefit iterations occur, the initial point is always the local minimum, and the relative first-order optimality is always = 0.
Am I doing something dumb or using this function in a way it wasn't meant to be used?
In the figure below:
the blue curve is connecting the datapoints (cur_data_wave_x,cur_data_wave_y)
the red curve the function F with v(1) = omega and v(2) = 0
the green curve is the function F with the resulting v = v0, in this case v = v0 = [omega 3]
The goal would be for the green curve to almost overlap the blue curve, then I can use the v(1) and v(2) values for the next step of my code. I'm trying to iterate through several thousand of these waves, find the delta omega aka (omega-v(1)) and delta phi (phi - v(2)) of each. Each wave in the iterations will have a different v(1) and v(2) value.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275176/image.png)
0 Comments
Accepted Answer
J. Alex Lee
on 5 Mar 2020
I think you have a typo here:
F = @(v,cur_data_wave_x) amplitude*(offset + cos(v0(1)*(cur_data_wave_x) + v0(2)));
should rather be
F = @(v,cur_data_wave_x) amplitude*(offset + cos(v(1)*(cur_data_wave_x) + v(2)));
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!