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

5 views (last 30 days)
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?

Accepted Answer

Sean de Wolski
Sean de Wolski on 9 Jan 2014
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:
  1 Comment
Shiladitya
Shiladitya on 9 Jan 2014
Edited: Shiladitya on 9 Jan 2014
Thank you for your answer. Even if I make it as a function I will have to write v= (same lengthy equation) and second thing is that my code is prematurely going out of loop. How can I solve it?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!