version 1.3 (8.63 KB) by
Jean-Luc Dellis

ZFIT is a function which can PLOT, SIMULATE and FIT impedance data

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

Jean-Luc Dellis (2021). Zfit (https://www.mathworks.com/matlabcentral/fileexchange/19460-zfit), MATLAB Central File Exchange. Retrieved .

Created with
R2019b

Compatible with any release

**Inspired by:**
fminsearchbnd, fminsearchcon

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

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Andreas StraßerSheyi AdediwuraHi Jean-Luc,

is there a method to estimate the standard errors of the fitted parameters as well as their correlation matrix?

Thanks in advance.

Jean-Luc DellisHi Tanvir. Yes sure, read the help lines, there are several examples and one showing to fit with an user-defined impedances. Cheers. JL

Md. Tanvir Alam ArmanHi Jean-Luc,

Can I fit EIS data with transmission line model ?

Thank you.

Jonathan ScharfHi Jean-Luc 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

Alexander PapstJean-Luc DellisHi 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" (Chi-squared) numbers when you change one parameter from 10% for instance. Zfit returns the khi2. Have a good day. Jean-Luc

Alexander PapstHi Jean-Luc,

Are there any possibilities to calculate the confidence intervals for the fitted parameters of the EIS curve?

Thanks in advance!

Mads Søndergaard LarsenDear Jean-Luc,

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

Jean-Luc DellisHi Larsen. Because your R values are small enough, milli and 1e-4 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.

Mads Søndergaard LarsenDear Jean-Luc,

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 = [1e-3,1e-4,1e-6] - 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 :)

Jean-Luc DellisHi Sheyi. Are you sure the data=[FR, ZR, ZI] forms a 3-colunms matrix. In other words, are Zr, Fr and Zi colunm vectore ? Regards, Jean-Luc

Sheyi AdediwuraHi Jean-luc,

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, 1e-9])

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

Sheyi AdediwuraSheyi Adediwurai defined my data like this

data=[FR, ZR, ZI];

then typed this

>> pbest= Zfit(data,'z','p(R1,C1)',[1e4, 1e-9])

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

Jean-Luc Dellishi Layiq. If you ask this question about a top level usage, it means you succeed to run the other sub-level examples. This function computes the impedance of an user-created 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 user-created function must be saved in the working repertory so Zfit can call it.

layiq ziaHi,

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,

Jean-Luc DellisHi Layiq,

Just read and try the examples given in the help lines of Zfit. Edit it in typing >> edit Zfit.

Regards

layiq ziaHi 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

Jean-Luc DellisBonjour 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. jean-Luc

Marie LHi! 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

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

Jean-Luc

Hazrul ShabriHi, 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

Jean-Luc DellisBonjour 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=1XEgfGAps2-KtUQglHkJsANEqBeLWyS7q or from https://fr.mathworks.com/matlabcentral/fileexchange/69811-zfitgui. Regards. JLD

Ishan KarnikHi,

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

Jean-Luc DellisHi,

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

Gaurav Ramesh DattaHi, 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.

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

giacomo maggioreGood 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?

Alice FrancioniThank you!! Now it works!!

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

Jean-Luc

Alice FrancioniThank 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?

Jean-Luc DellisBonjour 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 MeloThank 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.

Jean-Luc DellisBonjour John,

* The Khi2 definition is given in the ZfitGUI manual : https://drive.google.com/open?id=1_zryTSEnynGd_7FsXxFLj9H7J-gPqEWz. 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,

Jean-Luc

John AggasHi 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!

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

Allison KajiharaIs 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!

Jean-Luc DellisHi 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 LiGiuseppe MichettiHi!

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=1e-6;

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

siva kumarHi,

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.

Regards

Siva

Dong Pyo JangSobi ThomasVery useful and helpful

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

Jacopo Remondinaupdate 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

Jacopo RemondinaFirst 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 Lyuhow can i download it

Jae-Phil KimJuan MonteroReally useful tool, thanks!

anas aldowajiHello,

Thank you very much for this function

Matthew LiHello,

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

Jean-Luc DellisDear 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 ?

Walter de GierThank you Jean-Luc, I wasn't aware that nesting is possible.I tried it and works perfectly!

This makes the code even more beautiful ;)

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

Walter de GierI 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!

ChenReally thank you for such a wonderful work. it is very helpful for processing a large amount of EIS data.

BrunildaJean-Luc DellisChen,

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))' ?

ChenGreat work!

When I am trying more complicated string like 's(R1,p(R1,E2),p(R1,E2),E2)', the programe seems not work?

Jean-Luc DellisThank You !

For the reference, i suppose the author's name and the link to this page are sufficient.

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

Jean-Luc DellisA GUI is now available, please see ZfitGUI :

http://www.mathworks.com/matlabcentral/fileexchange/26386-zfitguivarargin-fits-impedance-data

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