File Exchange

image thumbnail


version (8.57 KB) by Jean-Luc Dellis
ZFIT is a function which can PLOT, SIMULATE and FIT impedance data


Updated 19 May 2010

View License

% Zfit(data)
Plots the impedance DATA which has to be a 3-columns 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...

Cite As

Jean-Luc Dellis (2019). Zfit (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (41)

As a first step, have you written something like :
>>pbest= Zfit(data,'z','s(R1,L1)',[1e4, 1e-6]);
where data is your 3-colunms matrix ?
I tried here with a 10 000 lines random matrix and it works. Also why so many data points ?
Best regards, jean-Luc

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 3-columns 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, Jean-Luc

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,

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, Jean-Luc

Bruno Melo

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 : 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,

John Aggas

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, Jean-Luc

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.


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.

Matthew Li

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


%circuit parameter definition, series combination of R & C


%Zfit eval


%plotting the Zdb in semilog plot


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?

siva kumar

Thanks a lot to Jean-Luc 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 lab-tap. 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.


Sobi Thomas

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.

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?

Mengfei Lyu

how can i download it

Really useful tool, thanks!

Thank you very much for this function

Matthew Li

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!

Kamal R.

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.

Kamal R.

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 Jean-Luc, 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,

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.



The operators s and p must have stricly 2 arguments. The operator s in the circuit string
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.

J Rey

Excellent software, thanks. What should I make the reference to if writing a Journal Paper?

A GUI is now available, please see ZfitGUI :

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...

Amit Kenny

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).


the way circuit is entered has been modified to ease the use

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: fminsearchbnd, fminsearchcon