% Zfit(data)
Plots the impedance DATA which has to be a 3columns wise matrix [FREQ, RealZEXP, ImagZEXP]. Impedance, admittance, capacitance and modulus representations in the complex plane are then pushbutton obtainable.
%
The others usages are described in the help lines with examples too...
JeanLuc Dellis (2020). Zfit (https://www.mathworks.com/matlabcentral/fileexchange/19460zfit), MATLAB Central File Exchange. Retrieved .
1.3  Now the operators “s” and “p” may handle more than 2 elements as arguments. Warm thanks to Hazrul Shabri who offered the code in the comments. 

1.2.0.0  the way circuit is entered has been modified to ease the use 
Inspired by: fminsearchbnd, fminsearchcon
Create scripts with code, output, and formatted text in a single executable document.
Hi JeanLuc,
is there a method to estimate the standard errors of the fitted parameters as well as their correlation matrix?
Thanks in advance.
Hi Tanvir. Yes sure, read the help lines, there are several examples and one showing to fit with an userdefined impedances. Cheers. JL
Hi JeanLuc,
Can I fit EIS data with transmission line model ?
Thank you.
Hi JeanLuc Dellis,
Just wanted to say that this is fantastic! I will be putting this in a publication and citing it using the above "Cite As" section. Thanks for everything!
Kind Regards,
Jonathan
Hi Alexander. Yes there are severals methods but all need some work ... I didnt do them completly. One way is to use what Matlab did with the Jacobian matrix, see their own functions concerning NL fitting or mine (easyfit) in which I wrote (or copied) a subfunction called "uncertainties" which could help you. However easyfit is designed for real (not complex) data fit. Another way is to use "bootstrap" methods but I had no time to go far in this direction. One third way to check parameter sensibility is to write the "khi2" (Chisquared) numbers when you change one parameter from 10% for instance. Zfit returns the khi2. Have a good day. JeanLuc
Hi JeanLuc,
Are there any possibilities to calculate the confidence intervals for the fitted parameters of the EIS curve?
Thanks in advance!
Dear JeanLuc,
Thank you for the help; I made it work for all the cells with for loops and Zfit worked perfectly. I can also say ZFitGUI is great for finding initial parameters, to make Zfit work faster in the for loop. Thank you very much!
Br
Mads S. Larsen
Hi Larsen. Because your R values are small enough, milli and 1e4 Ohms, I wonder if the single point is a group of points of small amplitude. So you might check if the zbest is actually a single point or a vector with same length than your data C0. I recommand to use ZfitGUI which allows to check such details, set the model and find good parameters guess. Also, I agree with you : poor initial parameters may lead to never converge to the solution. Say me what after please.
Dear JeanLuc,
Thank you for the very nice EIS fit data. I have one issue, that I can't wrap my head around:
I have a simple circuit, 's(R1,p(R1,C1))', with initial param = [1e3,1e4,1e6]  Estimate I made from looking at the data.
However, when I enter: [pbest,zbest] = Zfit([C01(:,1),C01(:,2),C01(:,3)],'z',circuit,param); (C01 is all the data [FR,ZR,ZI])
I get that pbest = param and zbest is just a single point, which does not even fit into the raw data.
Is it because the guess is poor? I played around with it, but no matter what I did, pbest = param and zbest was a single point compromised of 45 data points equal to the same (i.e. fit did not work).
I hope you can help  thanks in advance :)
Hi Sheyi. Are you sure the data=[FR, ZR, ZI] forms a 3colunms matrix. In other words, are Zr, Fr and Zi colunm vectore ? Regards, JeanLuc
Hi Jeanluc,
Thanks for this amazing work.
as a beginner, I still cant use the programmable zfit.m
the tutorial is mainly on GUI.
how do insert the data in the whole code?
an example of what i did before.
i imported my data and assign it like this;
i defined my data like this
data=[FR, ZR, ZI];
then typed this
>> pbest= Zfit(data,'z','p(R1,C1)',[1e4, 1e9])
the error message
Undefined operator '*' for input arguments of type 'table'.
Error in Zfit>C (line 332)
z=1i*2*pi*f*p;
Error in Zfit>computecircuit (line 319)
z(:,k)=eval(func);% compute its impedance for all the frequencies
Error in Zfit (line 297)
zbest=computecircuit(pbest,circuitstring,freq);
thanks again for your selfless service
i defined my data like this
data=[FR, ZR, ZI];
then typed this
>> pbest= Zfit(data,'z','p(R1,C1)',[1e4, 1e9])
the error message
Undefined operator '*' for input arguments of type 'table'.
Error in Zfit>C (line 332)
z=1i*2*pi*f*p;
Error in Zfit>computecircuit (line 319)
z(:,k)=eval(func);% compute its impedance for all the frequencies
Error in Zfit (line 297)
zbest=computecircuit(pbest,circuitstring,freq);
hi Layiq. If you ask this question about a top level usage, it means you succeed to run the other sublevel examples. This function computes the impedance of an usercreated element named with one unique letter "U" given in the help lines as an example. This function is like those which compute the impedances of inductors "L", of capacitors "C", of CPE "E" ... The usercreated function must be saved in the working repertory so Zfit can call it.
Hi,
I dont understand what does this user file mean:
% where U is an user file saved apart:
% function z=U(p,f)
% z=1./(p(1)*j*2*pi*f).^p(2);
i am stuck here i am unable to define z as for Zfit to simulate or fit.
Thanks,
Hi Layiq,
Just read and try the examples given in the help lines of Zfit. Edit it in typing >> edit Zfit.
Regards
Hi sir,
i am new at matlab i know some basic operation in matlab like plotting and building function. i am unable to apply the Zfit.m function for fitting my EIS data. Can you please provide here how to operate this function. what is the first step for implementation. '
Thanks
Bonjour Marie. Sorry you get annoyed with the program. I just have reloaded it from this site to check it and all was ok. Please check if there are only and exactly 3 columns in data. If troubles again, feel free to send me the data and the file Zfit you used. Also check if your Matlab release is 2014 and above. To end, if all comes OK for you, note there are 4 examples in the help lines of Zfit, study them carefully to get the best of the program. regards. jeanLuc
Hi! First of all thank you for chearing your code, time and work!
Im not very good at MATLAB and Im struggeling to run your function, which Im sure is excellent.
I have put my data in three columbs Freq, Zrel and ZIm in a table I named "data"; and then I run Zfit(data).
First matlab complains about plotz, not knowing what it is and then it complains about *, I have no idea why.
Do you have any idea what Im doing wrong?
Best regards
Marie
Waooo, very well done Hazrul.
I am currently testing your code which is correct in my first trial. It will be a great improvement for the users who prefer s(R1,C1,E2) than s(s(R1,C1),E2). I noticed that there must be no space " " in the string but it is ok in my opinion. Also, I need more time to check if all the functionnalities are OK but I am optimist and thankfull to you. I will be back here to say what in the very next weeks. TY again Hazrul.
JeanLuc
Hi, thank you for the file. I think you can change some of the code to make the serial circuit able to take more than two series and parallel to take more than two branch; here is my rough code that i changed in mine and replace the s and p function. p/s i just learned to use matlab, hence the code might seen redundant and inefficient but it works. it can do s(R1,C1,E2) and p(R1,C1,E2)
function z=s(varargin) % more zs in series
temp_size_varargin=size(varargin{1},1);
temp_n=ones(temp_size_varargin,nargin);
temp_sum=ones(temp_size_varargin,1);
for iii=1:nargin
temp_n(:,iii)=varargin{iii};
end
for iii=1:temp_size_varargin
temp_sum(iii)=sum(temp_n(iii,:));
end
z=temp_sum;
end
function z=p(varargin) % more zs in parallel
temp_size_varargin=size(varargin{1},1);
temp_n=ones(temp_size_varargin,nargin);
temp_sum=ones(temp_size_varargin,1);
for iii=1:nargin
temp_n(:,iii)=varargin{iii};
end
for iii=1:temp_size_varargin
temp_sum(iii)=sum(1./temp_n(iii,:));
end
z=1./(temp_sum);
end
Bonjour Ishan. The model string s(s(R1,L1),p(R1,p(L1,C1))) is OK. My advice is that you would try the Graphical User Interface version ZfitGUI.m from https://drive.google.com/open?id=1XEgfGAps2KtUQglHkJsANEqBeLWyS7q or from https://fr.mathworks.com/matlabcentral/fileexchange/69811zfitgui. Regards. JLD
Hi,
Thank you so much for your code. I am trying to use it to fit some EIS data. The circuit I want to use is s(s(R1,L1),p(R1,p(L1,C1))) but for some reason it is not running with this circuit model. Any advice?
Furthermore, with a simpler circuit s(R1,p(R1,C1)) model I do get the code to run, however the curve does not follow exactly my trend in data. One limit to my raw data is that it is not a fully formed EIS semicircle, will this cause problems?
Thanks,
Ishan
Hi,
As a first step, have you written something like :
>>pbest= Zfit(data,'z','s(R1,L1)',[1e4, 1e6]);
where data is your 3colunms matrix ?
I tried here with a 10 000 lines random matrix and it works. Also why so many data points ?
Best regards, jeanLuc
Hi, I am new to your program and I am having trouble fitting entering my data. I have a three columns of ten thousand data points  Freq, Real and Imag. The error I get is  PLEASE, supply at least a 3columns wise data matrix: [FREQ,RealZEXP,ImagZEXP] at line 185 in the switch case construct. My circuit is a simple  s(R1,L1) type.
I would be glad if you can help me out at what part of the code I need to put my data in. It seems it is not being read.
Bonjour Giacomo,
Yes the model string looks like correct however, I suggest to begin by simpler models then add new elements step by step. So you will get initial values and may check the model validity. To end, I am enough skeptic about a so much complicated model (with 13 parameters) could give values which have physical true meanings. if still problems, you may send me data. Have a good day, JeanLuc
Good morning and thanks for your code.
I'm trying to model EIS with a complete impedance equivalent circuit 's(s(p(E1,R1),p(E1,s(R1,p(C1,R1)))),s(R1,p(C1,p(R1,s(R1,L1)))))'. It doesn't provide any values or fitting curve. I do not have any proper or precise values to input. Is the input model correct?
Thank you!! Now it works!!
Ciao Alice,
Just put 's(s(R1,p(R1,C1)),p(p(R1,C1),L1))' as model string because all the elements only need one parameter. The next step is to enter the initial values as they come in the model string and comma separated.
Hoping that is helping,
JeanLuc
Thank you for your amazing code!
I am trying to fit an equivalent circuit. I am using two arguments for s and p and everything the circuit seems correct 's(s(R0,p(R3,C3)),p(p(R4,C4),L4))'. However, there is the warning dialog "an error occured, the circuit string or pinit is bad defined, plese check them". Could the initial values be wrong?
Bonjour Bruno. Thanks you for your kind message.
I am very happy if some colleagues improve the code but I had no news from Jacopo since his 2016 coments. Yes indeed he was considering that "no bounds" would give better results. In my opinion, to get BETTER or not better results depends on the data and on the model (presence or not of small Z values, presence or not of CPE, ... which may have heavy influence on the fit process) . However it is expectable to get DIFFERENT results because to set bounds will mathematically change the f(p) target and the fit may converge to a different solution especially if the new target f(p) landscape is complex (has many local minima).
About the CPE, if the data is enough hum ... regular, in other words if the model is right, then I think it is not necessary to use bounds for the CPE exponent, it would be sufisient to furnish starting values in the [0 1] interval.
I didnt study these features ... Best Regards, JeanLuc
Thank you so much Jean Luc.
Very nice code. I enjoyed the GUI version but this one is perfect for anyone who is comfortable with matlab. I was wondering if you were able to see the suggestions Jacopo Remondina provided. Was he considering that the fit was better if no bound restrictions were given as input? I am getting good results by just restricting some obvious variables such as the n power (N_Cpe) between 0 and 1 in the CPE elements. I also compared some fit results (made with Zfit) to other software such as Zview and got a good agreement. Have you made this study or published some article with this type of comparation by any means?
Thank you once again for providing us this code.
Bonjour John,
* The Khi2 definition is given in the ZfitGUI manual : https://drive.google.com/open?id=1_zryTSEnynGd_7FsXxFLj9H7JgPqEWz. Dowload the manual, then open it with Word to read it properly (some formulae may disappear with other viewers; I will correct that soon).
* Fval from Zfit is the same : fval=Khi2, but only if the weigthing method is left as default : proportionnal. Otherwise, Fval is the value of the minimized function (which is the difference Ztheo versus Zexp) at the solution as stated in the fminsearch Matlab definition. Once again, some details which can help are given in the ZfitGUI manual.
Best regards,
JeanLuc
Hi Dr. Dellis,
Thank you kindly for uploading this file  it works wonderfully. I see you have commented earlier that the FVAL is a measure of goodness of fit. I have a question with regard to that:
Is there some mathematical relationship between FVAL and CHI^2? (I see that in ZfitGUI you calculate the CHI2  we would like to calculate the CHI2 in the Zfit file).
Thank you!
To Allison : no sorry, these kind of optimizers must start with initial guesses. You may find them by analysis of the targeted data using "ZfitGUI", his manual and his tutorial also share in the file exchange Mathworks site. Regards, JeanLuc
Is there a way to run this code and fit to circuit without inputting starting circuit parameters? The way I am trying to use this code  I may not know or have a good guess as to what to make the starting parameters.
Thanks!
Hi Allison. Please have a look on the help lines. The syntax is "[pbest,zbest,fval,exitflag,output]=Zfit(varargin)" where FVAL is a measure of the goodness of fit. Regards.
Hi!
I get a wrong circuit simulation for series combination: for example the following code to generate the ZdB plot of a series RC
%frequencies definition
X=logspace(2,6,1000);
X=transpose(X);
%circuit parameter definition, series combination of R & C
C1=1e6;
R1=50;
circuit_values=[C1,R1];
circuit_model='s(C1,R1)';
%Zfit eval
[fittedparam,FittedZ]=Zfit(X,'z',circuit_model,circuit_values);
%plotting the Zdb in semilog plot
figure;
semilogx(transpose(X),mag2db((abs(FittedZ(:,1)+FittedZ(:,2)))))
this code gives me
1) a fully imaginary impedance (which is not correct)
2) the module plotted has a notch, like series LC resonance (which doesn't make sense).
What am I missing?
thanks
Giuseppe
Hi,
Thanks a lot to JeanLuc Dellis for providing this software to all. It will be much more helpful to the research scholars like me.
Could any one please explain, how to install this software in labtap. I downloaded this Zfit software, but i could not install it in my laptab. It would be highly helpful for my research work, if i could use this EIS fit software.
Regards
Siva
Very useful and helpful
Bonjour Jacopo, I just have received today the notification of your coments.
To answer your first coment, I think it could come from the starting values which must close the best ones when using CPE. Another hint is to try the "fitNP" string (fit Not Proportionnal). I mean a fit could appear badly completed when estimated by eyes on the Z spectrum when it is good when checking the sum (error(i)/Z(i)) given by the output parameter fval.
About your second and interressing coment, I would like to get the data and the modified code... in order to check by myself.
Amicalement
update for my previous post:
I modify a bit more your code, turning the function "fminsearchbnd" to "fminsearch" (creating a new function "curfitNoBound") if no bounds are provided to Zfit and now the fit is much better (actualy better then in EIS Spectrum Analyzer), so I suggest to avoid parameter transformation if possible
First of all, thanks for this program, it's gonna really help me.
Second: I think that I managed to improve your function so now p() and s() can work with any number of parameters (maybe not 0, but a check is easy to implement);
Third I have a question for you: I tried to fit a complex circuit: s(R1,s(p(E2,R1),p(E2,R1))) with some data, but I'he got a fit that was worste then the input parameters (obtained via EIS software analyzer and entered in your program with 3 significant digit). Any idea why?
how can i download it
Really useful tool, thanks!
Hello,
Thank you very much for this function
Hello,
Thank you very much for this function.
Did the world a big favor.
I was just wondering if it possible to easily obtain some sort of fitting statistic for each fitted parameter or just the whole fit in general.
Thank you very much!
Thank You, J. Dellis. It worked and ZfitGUI is too good, Working great ;)
Dear Kamal.
1/ I tried your string and there was no error. I think the limit in the amount of elements is higher. So there is no problem to simulate data.
2/ Fitting experimental data with long strings will be an other task and you would beware of overparametrization (too many elements) leading to some parameter to have nonsensed values.
3/ I tried your string on some data and get the minimizer failed at the first try. As said in the help, I retried with the returned "pbest" as initial parameters and get the convergence. To end, I prompt you to use the "ZfitGUI" function which eases the data weigthing. Best regards.
Such a wonderful code.
I am trying for this string s(R1,p(C1,s(R1,p(C1,s(R1,p(R1,s(R1,C1))))))) , it doesn't work with more than 7 elements for the string ?
Thank you JeanLuc, I wasn't aware that nesting is possible.I tried it and works perfectly!
This makes the code even more beautiful ;)
Bonjour Walter.
As the operators "s" and "p" require exactly 2 arguments s(a,b) and p(c,d), the code you writen : s(p(R1,C1),p(R1,C1),p(R1,C1))
is not correct and would be : s(s(p(R1,C1),p(R1,C1)),p(R1,C1))
Hoping that will help,
Regards
I noticed that s(p(R1,C1),p(R1,C1),p(R1,C1)) is not accepted so i expanded the parser with a 'c' (from 'c'hain ;) at the 's' postion and added the c function accepting 3 pararmeters. Maybe the 's' function can be modified using overloading accepting more arguments?
Beautiful code btw!
Really thank you for such a wonderful work. it is very helpful for processing a large amount of EIS data.
Chen,
The operators s and p must have stricly 2 arguments. The operator s in the circuit string
's(R1,p(R1,E2),p(R1,E2),E2)'
has 4 arguments. Maybe you wanted :
's(s(R1,p(R1,E2)),s(p(R1,E2),E2))' ?
Great work!
When I am trying more complicated string like 's(R1,p(R1,E2),p(R1,E2),E2)', the programe seems not work?
Thank You !
For the reference, i suppose the author's name and the link to this page are sufficient.
Excellent software, thanks. What should I make the reference to if writing a Journal Paper?
A GUI is now available, please see ZfitGUI :
http://www.mathworks.com/matlabcentral/fileexchange/26386zfitguivararginfitsimpedancedata
but still for MATLAB users because it is mandatory to write the fitting function. However, if no fitting is needed, checking the Z, Y, C or the Modulus spectra is easy...
Working just grate. Simple for operation by MATLAB user, but a GUI is needed for others. Very fast, and really converge to the data (not as the commercial programs which come with EIS systems).