ResearchPDF Available

Example code for the spectrum fitting of electrochemical impedance using Scilab

Authors:

Abstract

This is an example program-code for the fitting of impedance spectrum using the Scilab. The Scilab is open source software for numerical computation that can download and be used with free. The Scilab can be operate on Windows, MacOS, and Linux. Although this code is not a versatile one, you can modify the code according to your own aim. For example, you want to try fitting using a new impedance model function, it will be made easily by modification of the source code. By this mean, this is minimal code for impedance fitting.
27th July 2015
Example code for the spectrum fitting of electrochemical impedance
using Scilab
Kiyoshi Kobayashi
NIMS
This is an example code for the fitting of impedance spectrum using the Scilab. The Scilab
is open source software for numerical computation that can download and be used with
free. The Scilab can be operate on Windows, MacOS, and Linux.
http://www.scilab.org/
Although this is not a versatile program, you can modify the code according to your own
aim. For example, you want to try fitting using a new impedance model function, it will
be made easily by modification of the source code. By this mean, this is minimal code for
impedance fitting.
If you want to use this code, you should download the Scilab from the website
(http://www.scilab.org/) and install to your computer. I confirm to run this code by Scilab
version 5.5.2 (64 bit) on Microsoft Windows 8.1.
After installation of Scilab, Scipad is called from the “Menu” to select the “Scipad” on
the Scilab main window. Then, the code presented below is copied on the Scipad window.
The file is saved as ***.sce in a folder you want. “***” is a file name you want. Preferable
file extension is “sce”.
Next, the text data part is copied in text editor program including Scipad and then, the
data file is also saved as ***.txt in a folder you want. The data format in the impedance
data is “frequency”<tab>“Zreal”<tab>“Zimage” in one column. Never addition of header
and/or column labels in this file.
Select the Scipad window with program code to the top and click the “run” button
displayed above. Then, dialog window is open to select the impedance data file. You can
select the text file saved before. If the data are correctly read, the fitted results are
displayed by the two graph windows. These windows are initially overlapped and
therefore, you need to move one graph to the other part.
Then, another dialog window is open to save the parameter values and you enter a file
name to save. After clicking the “OK” button, the other dialog is open to save the
calculated impedance data using the fitted parameters. Those saved data can be open
using Text editor. If you want to read the data using excel etc, one space, “ “ is input to
the “other delimited character” when you open the file.
For the impedance fitting code, valuable information can be found at the “leastsolve”
section in the following website (but in Japanese).
http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&sqi=2&ved=
0CB0QFjAAahUKEwiLz-
y19_rGAhXENJQKHYVxCII&url=http%3A%2F%2Foptphys.sci.hokudai.ac.jp%2F~s
ekika%2Fwiki%2F%3FScilab%25B4%25CA%25B0%25D7%25A5%25EA%25A5%2
5D5%25A5%25A1%25A5%25EC%25A5%25F3%25A5%25B9&ei=I_G1VcuxG8Tp0
ASF46GQCA&usg=AFQjCNG63I3mAg2_2r0eciJ-o0ZffuIzNQ
Attention!
Convergency of nonlinear least squares is strongly depended on initial guess of the
parameters. If the initial guess is so far from the answer, the least square can’t be reached
to the minimum of squares or fallen into local minimum. User should check the fitted
parameters carefully.
With respect to the other functions of the Scilab, please learn those from the Scilab Help
etc by yourself. (https://help.scilab.org/doc/5.5.2/en_US/index.html)
I am also a beginner of Scilab and I can’t answer about details of the Scilab itself.
Program Code.
Copy from the “// Program Code” until “fprintfMat(dcfilename,[ff,zr_cal,zi_cal]);in page 6.
// Program Code
// This is the example code for electrochemical impedance fitting using SciLab.
// Coded by K. Kobayashi. 27th July 2015.
// Open the dialog to select the data file
filename = uigetfile( "*.txt" );
imp = fscanfMat(filename);
// Import the frequency, Zreal, and Zimage data
f = imp(:, 1);
zr = imp(:, 2);
zi = imp(:, 3);
// Declare the impedance model function.
// Here is the following one.
// Z = R1+(R2/CPE2)+(R3/CPE3)+(R4/CPE4)
// p(1)=R1, p(2)=R2, p(3)=T2, p(4)=a2, p(5)=R3, p(6)=T3, p(7)=a3, p(8)=R4, p(9)=T4, p(10)=a4
// Z_(R/CPE) == R/(1+R*T*(i*2*Pi*f)^a)
// i^2 = -1
// By Scilab, pi and i can be presented by %pi and %i, respectively.
function zc=zmodel(x,p)
zc = p(1);
zc = zc + p(2)./(1+p(2)*p(3)*(%i*2*%pi*x).^p(4));
zc = zc + p(5)./(1+p(5)*p(6)*(%i*2*%pi*x).^p(7));
zc = zc + p(8)./(1+p(8)*p(9)*(%i*2*%pi*x).^p(10));
endfunction
//initial guess of the fitting parameters
p0=[5, 5, 1.d-6, 0.7, 5, 1.d-5, 0.8, 5, 1.d-4, 0.9];
// Declare the residual function.
// f(p,m): Function given by the deviation (vector) at each data point with respect to the parameter, p.
// p0: Vector containing of initial guess of p.
// m: Number of the data points.
// See.
http://optphys.sci.hokudai.ac.jp/~sekika/wiki/?Scilab%B4%CA%B0%D7%A5%EA%A5%D5%A5%A1
%A5%EC%A5%F3%A5%B9
function e=err(p,m)
y2=zmodel(f,p);
e=[abs(zr-real(y2)); abs(zi-imag(y2))];
endfunction
// Least square calculation.
// Solving by the Lebenverg-Maquart method.
[p, v, info]=lsqrsolve(p0, err, size(f, 1)*2);
// If you want, the least squares can be done by below codes. But convergency is bad compared to the
above code.
// Initial guess of p should be closer to the answer.
//p0 = [5, 5, 1.d-5, 0.7, 6, 3.d-4, 0.8, 5, 5.d-4, 0.9]
//function e=err(p,m)
// y2=zmodel(f,p);
// e=abs(zr-real(y2)) + abs(zi-imag(y2));
//endfunction
// Least square calculation.
//[p, v, info]=lsqrsolve(p0, err, size(f, 1));
// Calculate the theoretical impedance using the fitted parameters.
// Expand the frequency range (10^10 Hz to 10^-4 Hz)
ff = logspace(10, -4, 160);
// Row vector is converted into column one.
ff = ff';
// Calculate the impedance using new frequency.
zcal=zmodel(ff,p);
zr_cal = real(zcal);
zi_cal=imag(zcal);
//Draw results
//Figure 1
figure(1); clf();
subplot(2,1,1);
plot2d(ff, zr_cal,style=10,logflag="ln", axesflag=1);
plot2d(f, zr, -1);
xlabel("log f/Hz", "fontsize",4);
ylabel("Zreal/ohm", "fontsize",4);
subplot(2,1,2);
datatipManagerMode('on');
curve_handles=datatipGetEntities();
plot2d(ff, zi_cal, style=10,logflag="ln", axesflag=1);
plot2d(f,zi, -1);
xlabel("log f/Hz", "fontsize",4);
ylabel("Zimage/ohm", "fontsize",4);
// Figure 2
scf(); clf();
xmin =0;
xmax = max(zr_cal);
ymin = 0;
ymax =xmax;
plot2d(zr_cal, -zi_cal, style=10, rect=[xmin,ymin,xmax,ymax],frameflag=4, axesflag=1);
plot2d(zr, -zi, style=-1, rect=[xmin,ymin,xmax,ymax],frameflag=4,axesflag=1);
xlabel("Zreal/ohm", "fontsize",4);
ylabel("-Zimage/ohm", "fontsize",4);
datatipManagerMode('on');
curve_handles=datatipGetEntities();
// Row vector of the fitted parameter is converted into column one for writing the text file.
pout=p';
// Save Parameter List
pfilename = uiputfile("*.txt");
fprintfMat(pfilename,pout);
// Save Measured Spectrum Data.
//dfilename = uiputfile("*.txt");
//fprintfMat(dfilename,[f,zr,zi]);
// Save Calculated Spectrum Data
dcfilename = uiputfile("*.txt");
fprintfMat(dcfilename,[ff,zr_cal,zi_cal]);
// End of Code.
The data part. Tab delimited columns. Copy from the “1.00008e+06 12.5552-1.14611
to the “0.10016 23.7202 0.0067143” in page 9.
1.00008e+06 12.5552 -1.14611
794391 12.7205 -1.25555
630984 12.9192 -1.35508
501234 13.1303 -1.43662
398109 13.3509 -1.50696
316266 13.5843 -1.5719
251203 13.828 -1.62994
199547 14.0781 -1.67879
158578 14.3278 -1.73168
125953 14.4534 -1.73694
100078 14.8863 -1.80144
79453.1 15.1397 -1.8418
63140.6 15.4065 -1.89148
50203.1 15.6887 -1.92774
39890.6 15.9705 -1.95855
31640.6 16.2584 -1.98284
25171.9 16.5432 -2.01684
20015.6 16.8372 -2.0416
15890.6 17.1371 -2.06593
12609.4 17.4294 -2.08872
10078.1 17.7265 -2.12272
8015.62 18.0351 -2.15784
6328.12 18.3739 -2.20717
5015.62 18.6912 -2.24686
3984.38 19.0276 -2.29104
3170.96 19.3833 -2.33116
2527.57 19.746 -2.3764
1976.1 20.1729 -2.39299
1577.52 20.5876 -2.39615
1265.62 20.9959 -2.35489
998.264 21.4251 -2.28404
796.875 21.8238 -2.1381
627.79 22.1995 -1.97321
505.515 22.4905 -1.79456
397.995 22.7668 -1.56225
315.505 22.9634 -1.35478
252.404 23.2009 -1.15732
198.623 23.2842 -1.03092
158.361 23.5085 -0.922323
125.558 23.3631 -0.786656
100.446 23.6074 -0.500175
79.0028 23.5216 -0.45731
63.3446 23.7939 -0.493679
50.2232 23.8994 -1.34575
38.4221 23.7188 0.147958
31.25 23.4651 -0.172981
24.9335 23.6827 -0.217813
19.8623 23.6944 -0.161098
15.625 23.7558 -0.168577
12.4008 23.7205 -0.112472
9.93114 23.7336 -0.0916443
7.94491 23.7262 -0.0828761
6.31739 23.7237 -0.0663251
5.00801 23.7338 -0.0589409
3.94571 23.732 -0.0449868
3.15869 23.7491 -0.0471556
2.50401 23.7503 -0.0326205
1.99808 23.7335 -0.0276533
1.58469 23.7501 -0.0242837
1.26689 23.7556 -0.0207347
0.999041 23.766 -0.0196381
0.792343 23.769 -0.0136754
0.633446 23.7673 -0.0092947
0.504032 23.7678 -0.0074287
0.400641 23.7633 -0.003761
0.316723 23.7593 -0.0017771
0.252016 23.7542 0.0005144
0.20032 23.7467 0.0039999
0.158898 23.7375 0.0043141
0.126008 23.7301 0.0051919
0.10016 23.7202 0.0067143

Supplementary resources (2)

Data
August 2015
Kiyoshi Kobayashi
Data
August 2015
Kiyoshi Kobayashi
ResearchGate has not been able to resolve any citations for this publication.
ResearchGate has not been able to resolve any references for this publication.