MATLAB: How to find two parameters by non linear curve fitting with equation involving two variables.

lsqcurvefitnonlinear curve fittingoptimizationparameter finding

I have to find out the values of 'alpha' and 'Rb' in the following equation by fitting it into the experimentally observed data zc. zm and zn are independent variables for which I have the data. 1/zc= (1/zn)*((zn/(zn+zm))+(zm/(zn+zm))/((0.5*alpha*pz*besseli(0,alpha*pz)/besseli(1,alpha*pz))+Rb*((1/zn)+(1/zm))))
where pz= sqrt((1/zn)+(1/zm)) The equation is also shown in image.
I am using lsqcurvefit to determine alpha and Rb. My code is not working properly as it goes prematurely out of loop. The code looks like following:
A= xlsread('fz.xlsx');
B= xlsread('fzpc.xlsx');
f= A(:,1);
zm1= -(1000000*i)./(1*pi*f);
zm= zm1.*0.14;
zn1= A(:,2);
zn= zn1.*0.14;
zc1= B(:,2);
zc= zc1.*0.14;
a0=[20,50];
predicted= @(a,z) 1./((1./z(:,1)).*((z(:,1).*(1./(z(:,1)+z(:,2))))+((z(:,2).*(1./(z(:,1)+z(:,2))))./((0.5*a(1).*sqrt((1./z(:,1))+(1./z(:,2))).*((besseli(0,a(1).*((1./z(:,1))+(1./z(:,2)))))./(besseli(1,a(1).*sqrt((1./z(:,1))+(1./z(:,2)))))))+(2*a(2).*((1./z(:,1))+(1./z(:,2))))))));
[ahat,resnorm] = lsqcurvefit(predicted,a0,[zn,zm],zc);
Is there some other way as writing the whole equation in one go makes it very difficult to debug?

Best Answer

Write it as a function!
function v = predicted(a,z)
% stuff
end
Then call it using the syntax explained here for either anonymmous or nested functions: