ResearchPDF Available

Minimal code for impedance fitting using the R

Authors:

Abstract

This is the minimal code for impedance fitting using R language. This code is solved for the data uploaded on my researchgate page.
2016/11/01
Minimal code for impedance fitting using the R
Kiyoshi Kobayashi (National Institute for Materials Science)
This is the minimal code for impedance fitting using R language. The R is a
free software environment for statistical computing and graphics. It compile and runs on
a wide variety of UNIX platforms, Windows, and MacOS [1].
This code is solved for the data uploaded on my researchgate page [2]. For
running of this code, it is necessary to install the expanded package “minpac.lm”. If you
use the RGui environment, the minpac.lm package would be installed by selecting
“Package” menu => “Install Package(s) => minpack.lm”, then click the “OK” button.
After running the RGui program, you open the “impedance_test01.R” script
file from the “File” menu => “Open Script”. Then, you select the “Edit” menu => “Run
all”, the least square calculation will start. The fitted results are displayed by the three
graphs (these are overlapped). The values of optimized parameters and its fractional
standard uncertainty are displayed on the R console window. The “pfit” and “perr”
represent the optimized parameter values and fractional standard uncertainty. The RSS
indicates the sum of the least squares.In the case of the Levenberg-Marquardt module in
minpack.lm, the bounded constraints of the parameters can be applied. In detail, please
see the script code and the official manual of the nls.lm [3].
Comparing to the Scilab code [2], the R program can be got the standard
uncertainty of the optimized parameters (perr in this code) and the variance-covariance
matrix (fcov in this code). These outputs may be convenient for users.
[1] https://www.r-project.org/
[2] DOI: 10.13140/RG.2.1.3540.2085, test imp data, Exmpl impedance fitting
[3] https://cran.r-project.org/web/packages/minpack.lm/minpack.lm.pdf
# Import minpac.lm for Levenberg-Marquardt algorithm
library(minpack.lm)
# Open dialog to select text data file
data <- read.table(file.choose())
# Separate the data into f, zr, and zi
f <- data[,1,1]
zr <- data[,2,1]
zi <- data[,3,1]
# Complex impedance data list
zc <- zr + 1i*zi
# Declare impedance model function
imp <- function(p,x){
z <- p[1]
z <- z + p[2]/(1+p[2]*p[3]*(1i*2*pi*x)^p[4])
z <- z + p[5]/(1+p[5]*p[6]*(1i*2*pi*x)^p[7])
z <- z + p[8]/(1+p[8]*p[9]*(1i*2*pi*x)^p[10])
return(z)
}
# Declare the residual function
rsd <- function(p,z,x) {
errz = imp(p,x)-z
err = c(Re(errz), Im(errz))
return (err)
}
# Input an initial guess of the fitting parameters
p0 <- c( 10, 5, 1.0e-7, 0.8, 5, 1.0e-6, 0.8, 4, 1.0e-5, 0.8)
# Lower limit of parameters
pl <- c( 1, 0.1, 1e-10, 0.4, 0.1, 1e-10, 0.4, 0.1, 1e-10, 0.4)
# Upper limit of parameters
pu <- c( 20, 50, 1e-2, 1, 50, 1e-2, 1, 50, 1e-1,1)
# start fitting
nls.out <- nls.lm(p = p0, lower = pl, upper = pu, fn = rsd, z = zc, x = f, control =
nls.lm.control(nprint = 1, maxfev = 1000, maxiter = 100, ftol = 0.000001))
summary(nls.out)
aa <- summary(nls.out)
pfit <- coef(nls.out)
perr <- aa[[8]][11:20]
fcov <- vcov(nls.out)
RSS <- sum(abs(imp(pfit,f)-zc)*abs(imp(pfit,f)-zc))
while (aa[[5]] != 1){
p0 <- pfit
nls.out <- nls.lm(p = p0, lower = pl, upper = pu, fn = rsd, z = zc, x = f, control =
nls.lm.control(nprint = 1, maxfev = 1000, maxiter = 100, ftol = 0.000001))
summary(nls.out)
aa <- summary(nls.out)
}
summary(nls.out)
aa <- summary(nls.out)
pfit <- coef(nls.out)
perr <- aa[[8]][11:20]
RSS <- sum(abs(imp(pfit,f)-zc)*abs(imp(pfit,f)-zc))
fcov <- vcov(nls.out)
zc_cal <- imp(pfit,f)
zr_cal <- Re(zc_cal)
zi_cal <- Im(zc_cal)
plot(f,zr,log="x", xlim=c(10^-2, 10^6),ylim=c(10, 25),ann=F)
par(new=T)
plot(f,zr_cal,log="x", xlim=c(10^-2, 10^6),ylim=c(10, 25), type="l")
dev.new()
plot(f,zi,log="x", xlim=c(10^-2, 10^6),ylim=c(-2.5, 0.5),ann=F)
par(new=T)
plot(f,zi_cal,log="x", xlim=c(10^-2, 10^6),ylim=c(-2.5, 0.5),type="l")
dev.new()
plot(zr,-zi,xlim=c(10,25),ylim=c(-2, 5),ann=F, asp = 1)
par(new=T)
plot(zr_cal,-zi_cal, xlim=c(10,25),ylim=c(-2, 5), type="l", asp = 1)
pfit
perr
RSS
fcov

Supplementary resources (4)

ResearchGate has not been able to resolve any citations for this publication.
ResearchGate has not been able to resolve any references for this publication.