nonlinear least squares fit

1 回表示 (過去 30 日間)
Sebastian Schnelle
Sebastian Schnelle 2011 年 6 月 10 日
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.

回答 (2 件)

Ivan van der Kroon
Ivan van der Kroon 2011 年 6 月 10 日
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 2011 年 6 月 10 日
Thanks. I get different results but that might have to do with slightly different starting values. Thanks again.

カテゴリ

Help Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by