lsqcurvefit question - ydata has two dimension (Function value and YDATA sizes are not equal)

1 次查看(过去 30 天)
Hi,
I am new to matlab and I want to do a curve fitting using lsqcurvefit (following a paper's action). But I got a error called: Function value and YDATA sizes are not equal. Where did i do mistake...? Thanks for help.
xdata is the timepoint while ydata is 2-D position changing with time. The two dimensions of y are not independent of each other, so I don't think they should be fitted separately. This model have three params.
xdata = 1:60;
ydata = [590.25,511.34;590.23,510.52;590.22,509.51;590.18,508.61;
590.13,507.77;590.16,506.05;590.20,504.68;590.15,503.54;
589.98,501.91;589.66,500.71;589.47,499.64;589.27,498.69;
589.00,497.27;588.65,495.75;588.24,494.53;588.00,493.50;
587.57,492.52;587.45,491.43;587.21,490.27;586.96,489.24;
586.63,488.08;586.44,487.03;586.23,485.81;585.94,484.29;
585.57,482.81;585.21,481.34;584.93,480.24;584.59,479.26;
584.30,478.44;583.86,476.75;583.33,475.30;582.87,474.14;
582.52,473.06;582.21,472.03;582.04,471.08;581.62,469.92;
581.29,468.39;580.85,466.46;580.66,464.52;580.43,463.14;
580.11,462.08;579.58,460.73;579.51,458.95;579.41,457.45;
579.49,455.14;579.26,452.99;578.85,451.19;578.71,450.34;
578.53,449.45;578.54,448.45;578.48,447.62;578.30,446.64;
578.20,445.49;577.43,442.85;577.27,440.24;576.85,438.83;
576.67,437.85;576.54,437.19;576.52,435.73;576.35,434.59];
Filter = [0,0.0191620208109086,0.0683763509212163,0.100379351034585,0.0974133620604203,0.0679132261017191,0.0279145555757322,...
-0.00924174097003220,-0.0361988204543976,-0.0510709858726009,-0.0554801938514580,-0.0525520022466199,-0.0454971586440819,...
-0.0368765372303619,-0.0283884375848139,-0.0209563283968831,-0.0149359405672504,-0.0103302318649573,-0.00696094418434395,...
-0.00458437240976897,-0.00295845614268432,-0.00187479263439797,-0.00116876316001411,-0.000717879195806221,...
-0.000435011977115633,-0.000260359690910572,-0.000154065078565133,-9.02143808828862e-05,-5.23151853625161e-05,...
-3.00650758394752e-05,-1.71336401217675e-05,-9.68796610836146e-06,-5.43788818548629e-06,-3.03138411065368e-06,...
-1.67897757316864e-06,-9.24285783937950e-07,-5.05912271159936e-07,-2.75416083714947e-07,-1.49167755170539e-07,...
-8.03986272699545e-08,-4.31338127221703e-08];
% Define cumulative Gaussian function
F = @(mtnd, stnd, t) normpdf(t, mtnd, stnd);
% Define curve-fitting function
fun = @(params, xdata) conv(Filter, F(params(1), params(2), xdata));
% Estimate parameters using least-square fit
params = lsqcurvefit(fun, [0, 1], xdata, ydata);

回答(1 个)

Torsten
Torsten 2023-2-12
Your function "fun" must return a matrix of size (60x2) like the ydata you provide. This is not the case - thus the error message.
  4 个评论
Walter Roberson
Walter Roberson 2023-2-12
They only return a matrix of one dimension (60x1)
No. You conv() a vector of length 41 (Filters) and xdata (length 60). The result is 41+60-1 = 100 .
If you wanted something the size of xdata to be returned you would need code similar to
fun = @(params, xdata) conv(F(params(1), params(2), xdata), Filter, 'same');
But of course this would still be a vector of length 60, not something that you could use with 60 x 2 Ydata.
for col = 1 : size(ydata,2)
params(:,col) = lsqcurvefit(fun, [0, 1], xdata, ydata(:,col).');
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get 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