Clear Filters
Clear Filters

nonlinear least squares fit

3 views (last 30 days)
Sebastian Schnelle
Sebastian Schnelle on 10 Jun 2011
Hi everyone,
sorry, but I am trying to fit some data and don't get where I am going wrong. I have defined my function
function F = nonlinfun(x,t)
F = x(1)*t.^2 + x(2)*t + x(3) + x(4)*sin(2*pi*(t+x(5)));
and then I am trying to do the fit
clear;
read = importdata('c:/data.txt',',');
data = read(:,7);
n = length(data);
t = 1:n;
x0 = rand(5,1);
x0 = 300;
[x,resnorm] = lsqcurvefit(@nonlinfun,x0,t,data);
The data I am trying to fit can be found here:
(coumn 7) if anyone is interested. I saved it in a file 'data.txt' and reading that out works. When I try to run above sequence Matlab returns the following error:
Function value and YDATA sizes are incommensurate.
I got the simple Matlab examples to work but have tried unsuccessfully all day with the atmospheric data. I guess there is something simple I am overlooking?
Thanks for the help.

Answers (2)

Ivan van der Kroon
Ivan van der Kroon on 10 Jun 2011
t is a row-vector while data is a column-vector. Change it to
t=(1:n)';
Btw, I got
x= 0.0012
0.1046
-7.6955
-2.0487
-3.2998

Sebastian Schnelle
Sebastian Schnelle on 10 Jun 2011
Thanks. I get different results but that might have to do with slightly different starting values. Thanks again.

Products

Community Treasure Hunt

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

Start Hunting!