How to parse different dimension ydata in lsqcurvefit using cell arrays?

2 次查看(过去 30 天)
Hi. I have compiled all my observed data (of different dimensions) in separate cells e.g.
y{1}=[];
for input_trace=1:h; % h= no of files from which ydata is imported where filenames are time(1) etc..
import=['time(' int2str(input_trace) ')'];
ydata=xlsread(import); %import of files
y(input_trace)={ydata(:,2)}; %
end
and so I have collection of ydata and also my functions output is arranged accordingly.
ini_param=[A,B,Z]; %initial parameters
options = optimset('DiffMinChange',[0.000001],'disp','iter','Algorithm','levenberg-marquardt','MaxIter',100000,'MaxFunEvals',[10000]);%simple max eval fun 100000
options = optimset(options, 'TolX', 1e-14);
[fitted_param,resnorm,fval,output]= lsqcurvefit( @(ini_param,x) functionname(ini_param,x),ini_param,x,y,lb,ub,options);
In this way my ydata is like
y =
Columns 1 through 6
[0.0156] [320x1 double] [-0.0212] [203x1 double] [199x1 double] [1.0034e-04]
Columns 7 through 11
[246x1 double] [0.0348] [242x1 double] [225x1 double] [340x1 double]
When I run the program it gives error
Error using lsqcurvefit (line 182)
LSQCURVEFIT requires the following inputs to be of data type double: 'YDATA'.
Error in functionname(line 204)
[fitted_param,resnorm,fval,output]= lsqcurvefit( @(ini_param,x)
functionname(ini_param,x),ini_param,x,y,lb,ub,options);
What am I doing wrong?

回答(1 个)

Walter Roberson
Walter Roberson 2017-2-9
lsqcurvefit does not accept cell arrays for x or y.
You may need to loop doing one y item at a time.
  2 个评论
Vipultomar
Vipultomar 2017-2-9
编辑:Vipultomar 2017-2-9
Okay. Thank you. I will give it a try. But where exactly should I use loop. Do I give one cell content (ydata) for one xvalue?
For each xdata values (x1,x2,x3...) I have a number of (y1,y2,y3.. etc) outputs, where each yi is again a vector (but may be of different dimension as I mentioned earlier). So now if I give y's using loop, does that mean it should be like:
[fitted_param,resnorm,fval,output]= lsqcurvefit( @(ini_param,xi) functionname(ini_param,xi),ini_param,xi,yi,l[],[],options);
So that at each x point I do lsqcurvefit. But how do I provide all of my corresponding ydata (for all cells) simultaneously so that it can optimize accordingly.
Walter Roberson
Walter Roberson 2017-2-9
Fitting requires that the size of the input (independent variable, each xi) matches the size of the output (each yi). You cannot, for example, use a 57 x 1 vector of x to calculate a 34 x 1 y.
It could make sense to use the same x with different y that are the same size as each other, and it could make sense to use different x with y of corresponding size. For example,
for K = 1 : length(y)
[fitted_param{K}, resnorm{K}, fval{K}, output{K}]= lsqcurvefit( @(ini_param,xi) functionname(ini_param, x{K}), ini_param, x{K} , y{K}, [], [], options);
end

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by