fitting data to implicit function

I have an experimental data with first column an independent variable and the second column a dependent variable. I need to estimate 2 parameters k and b. My model is actually a system o equations. The code of the model is below:
function y=calc_bg1(x,z) for ii=1:length(z) syms b gbg k n p r rgbg1 rgbg2 rgbg3 rgbg4 positive b=x(1); k=x(2); n=z(ii); [gbg,p,r,rgbg1,rgbg2,rgbg3,rgbg4]=solve(4*r*gbg == (k)*rgbg1, 3*rgbg1*gbg == 2*(k)*b*rgbg2, 2*rgbg2*gbg == 3*(k)*(b^2)*rgbg3, 1*rgbg3*gbg == 4*(k)*(b^3)*rgbg4, r+rgbg1+rgbg2+rgbg3+rgbg4 == 7*166e-9, gbg+rgbg1+2*rgbg2+3*rgbg3+4*rgbg4 == n*166e-9, (.97*rgbg4+0.03*7*166e-9)*52806306*0.19 == p*1e-3 ,gbg,p,r,rgbg1,rgbg2,rgbg3,rgbg4); pp(ii)=double(p); clear gbg n p r rgbg1 rgbg2 rgbg3 rgbg4 end y=pp'; When I am trying to do the fit with lsqcurvefit lsqcurvefit(@calc_bg1,[.4 7e-6],test1(:,1)',test1(:,2)) Improper assignment with rectangular empty matrix.
Error in calc_bg1 (line 9) pp(ii)=double(p);
Error in lsqcurvefit/objective (line 261) F = feval(funfcn_x_xdata{3},x,XDATA,varargin{:});
Error in snls (line 329) newfvec = feval(funfcn{3},xcurr,varargin{:});
Error in lsqncommon (line 155) [xC,FVAL,LAMBDA,JACOB,EXITFLAG,OUTPUT,msgData]= ...
Error in lsqcurvefit (line 253) [xCurrent,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
Can anybody help?

3 commentaires

Karan Gill
Karan Gill le 10 Nov 2017
Please format your code. It's really hard to read.
Daniel Yakubovich
Daniel Yakubovich le 10 Nov 2017
is it better?
the code of the function is:
function y=calc_bg1(x,z)
for ii=1:length(z)
syms b gbg k n p r rgbg1 rgbg2 rgbg3 rgbg4 positive
b=x(1);
k=x(2);
n=z(ii);
[gbg,p,r,rgbg1,rgbg2,rgbg3,rgbg4]=solve(4*r*gbg == (k)*rgbg1, 3*rgbg1*gbg == 2*(k)*b*rgbg2, 2*rgbg2*gbg == 3*(k)*(b^2)*rgbg3, 1*rgbg3*gbg == 4*(k)*(b^3)*rgbg4, r+rgbg1+rgbg2+rgbg3+rgbg4 == 7.10838649776828*166.057788110262e-9, gbg+rgbg1+2*rgbg2+3*rgbg3+4*rgbg4 == n*166.057788110262e-9, (.97*rgbg4+0.03*7.10838649776828*166.057788110262e-9)*52806306.6954643*0.1960749100506533 == p*1e-3 ,gbg,p,r,rgbg1,rgbg2,rgbg3,rgbg4);
%pp(ii)=double(vpa(p))
pp(ii)=double(p);
clear gbg n p r rgbg1 rgbg2 rgbg3 rgbg4
end
y=pp';
and the fitting is done with: p=lsqcurvefit(@calc_bg1,[.4 7e-6],test1(:,1)',test1(:,2)) where test1 is a 2 column matrix

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by