4 minute read

Appendix D- CTSM-R code for dynamic tests

The code for dynamic test 1 is listing below:

## Set the working directory setwd(".") ## Use the ctsmr package library(ctsmr) library(readxl) ## Initialize the model model <- ctsm$new() ## Specify the system equations model$addSystem(dT ~ ( 1/(C*R1) * (Ti-T) + 1/(C*R2) * (Te-T) ) * dt + s11* dw) ## The inputs model$addInput("Te","Ti") ## The observation equation model$addObs( Q ~ 1/R1*(Ti-T)) model$setVariance(Q ~ s)

Advertisement

model$setParameter( T0 = c(init=20.2 ,lb=8 ,ub=40 )) model$setParameter( C = c(init=10000 ,lb=8000 ,ub=18000) ) model$setParameter( R1 = c(init=2 ,lb=0 ,ub=5 ) ) model$setParameter( R2 = c(init=2 ,lb=0 ,ub=5 ) ) model$setParameter( s11 = c(init=0.01 ,lb=0 ,ub=1 ) ) model$setParameter( s = c(init=0.01 ,lb=0 ,ub=1 ) )

X <- read_excel(file.choose(), sheet = "Sheet1", skip = 0) X <- subset (X , select = c("t" ,"Ti","Te","T","Q"))

print(X)

par(mfrow=c(3,1),mar=c(3,3.5,1,1),mgp=c(2,0.7,0)) # Setup plot plot(X$t,X$Te,type="l") plot(X$t,X$Ti,type="l") plot(X$t,X$Q,type="l") fit <- model$estimate(X)

## Print the parameters estimates and the Correlation Matrix of the estimates summary(fit,extended=TRUE)

## The parameter estimates in a data.frame Prm <- data.frame(xm=fit$xm) row.names(Prm) <- names(fit$xm)

78

## The true parameter values Prm$xmtrue <- c(20.2,12491,2.6,0.006,0.01,0) ## Approximately lower 95% confidence bound Prm$lower <- (fit$xm - 3*fit$sd) ## Approximately upper 95% confidence bound Prm$upper <- (fit$xm + 3*fit$sd) ## Print it all ##round(Prm,digits=2) format(Prm,digits=4)

## Calculate the one-step ahead predictions tmp <- predict(fit, newdata=X) ## Calculate the residuals and put them with the data in a data.frame X X$QHat <- tmp$output$pred$Q X$residuals <- X$Q - X$QHat ## Auto-correlation function and cumulated periodogram par(mfrow=c(1,2)) #Setup plot acf(X$residuals) cpgram(X$residuals)

## Plot of inputs, output, one-step ahead predicted output, and residuals par(mfrow=c(4,1),mar=c(3,3.5,1,1),mgp=c(2,0.7,0)) # Setup plot plot(X$t,X$Te,type="l") plot(X$t,X$Ti,type="l") plot(X$t,X$Q,type="l") lines(X$t,X$QHat,col=2) plot(X$t,X$residuals,type="l")

The code for dynamic test 2 is listing below: ## Set the working directory setwd(".") ## Use the ctsmr package library(ctsmr) library(readxl) ## Initialize the model model <- ctsm$new() ## Specify the system equations model$addSystem(dT ~ ( 1/(C*R1) * (Ti-T) + 1/(C*R2) * (Te-T) ) * dt + s11* dw) ## The inputs model$addInput("Te","Ti")

79

## The observation equation model$addObs( Q ~ 1/R1*(Ti-T)) model$setVariance(Q ~ s)

model$setParameter( T0 = c(init=27 ,lb=10 ,ub=35 )) model$setParameter( C = c(init=10000 ,lb=8000 ,ub=15000) ) model$setParameter( R1 = c(init=2 ,lb=0 ,ub=5 ) ) model$setParameter( R2 = c(init=2 ,lb=0 ,ub=5 ) ) model$setParameter( s11 = c(init=0.01 ,lb=0 ,ub=1 ) ) model$setParameter( s = c(init=0.01 ,lb=0 ,ub=1 ) )

X <- read_excel(file.choose(), sheet = "Sheet1", skip = 0) X <- subset (X , select = c("t" ,"Ti","Te","T","Q"))

print(X)

par(mfrow=c(3,1),mar=c(3,3.5,1,1),mgp=c(2,0.7,0)) # Setup plot plot(X$t,X$Te,type="l") plot(X$t,X$Ti,type="l") plot(X$t,X$Q,type="l") fit <- model$estimate(X)

## Print the parameters estimates and the Correlation Matrix of the estimates summary(fit,extended=TRUE)

## The parameter estimates in a data.frame Prm <- data.frame(xm=fit$xm) row.names(Prm) <- names(fit$xm) ## The true parameter values Prm$xmtrue <- c(27.4,12491,2.6,0.01,0.01,0) ## Approximately lower 99% confidence bound Prm$lower <- (fit$xm - 3*fit$sd) ## Approximately upper 99% confidence bound Prm$upper <- (fit$xm + 3*fit$sd) ## Print it all ##round(Prm,digits=2) format(Prm,digits=6)

## Calculate the one-step ahead predictions tmp <- predict(fit, newdata=X) ## Calculate the residuals and put them with the data in a data.frame x X$QHat <- tmp$output$pred$Q X$residuals <- X$Q - X$QHat

80

## Auto-correlation function and cumulated periodogram par(mfrow=c(1,2)) #Setup plot acf(X$residuals) cpgram(X$residuals)

## Plot of inputs, output, one-step ahead predicted output, and residuals par(mfrow=c(4,1),mar=c(3,3.5,1,1),mgp=c(2,0.7,0)) # Setup plot plot(X$t,X$Te,type="l") plot(X$t,X$Ti,type="l") plot(X$t,X$Q,type="l") lines(X$t,X$QHat,col=2) plot(X$t,X$residuals,type="l")

Test 3: # Set the working directory setwd(".") ## Use the ctsmr package library(ctsmr) library(readxl) ## Initialize the model model <- ctsm$new() ## Specify the system equations model$addSystem(dT ~ ( 1/(C*R1) * (Ti-T) + 1/(C*R2) * (Te-T) ) * dt + s11* dw) ## The inputs model$addInput("Te","Ti") ## The observation equation model$addObs( Q ~ 1/R1*(Ti-T)) model$setVariance(Q ~ s)

model$setParameter( T0 = c(init=27.2 ,lb=10 ,ub=40 )) model$setParameter( C = c(init=10000 ,lb=6000 ,ub=20000) ) model$setParameter( R1 = c(init=2 ,lb=0 ,ub=6 ) ) model$setParameter( R2 = c(init=0.1 ,lb=0 ,ub=5 ) ) model$setParameter( s11 = c(init=0.01 ,lb=0 ,ub=1 ) ) model$setParameter( s = c(init=0.01 ,lb=0 ,ub=1 ) )

X <- read_excel(file.choose(), sheet = "Sheet1", skip = 0) X <- subset (X , select = c("t" ,"Ti","Te","T","Q"))

print(X)

par(mfrow=c(3,1),mar=c(3,3.5,1,1),mgp=c(2,0.7,0)) # Setup plot

81

plot(X$t,X$Te,type="l") plot(X$t,X$Ti,type="l") plot(X$t,X$Q,type="l") fit <- model$estimate(X)

## Print the parameters estimates and the Correlation Matrix of the estimates summary(fit,extended=TRUE)

## The parameter estimates in a data.frame Prm <- data.frame(xm=fit$xm) row.names(Prm) <- names(fit$xm) ## The true parameter values Prm$xmtrue <- c(27.4,12491,2.6,0.006,0.01,0) ## Approximately lower 95% confidence bound Prm$lower <- (fit$xm - 3*fit$sd) ## Approximately upper 95% confidence bound Prm$upper <- (fit$xm + 3*fit$sd) ## Print it all ##round(Prm,digits=2) format(Prm,digits=4)

## Calculate the one-step ahead predictions tmp <- predict(fit, newdata=X) ## Calculate the residuals and put them with the data in a data.frame X X$QHat <- tmp$output$pred$Q X$residuals <- X$Q - X$QHat ## Auto-correlation function and cumulated periodogram par(mfrow=c(1,2)) #Setup plot acf(X$residuals) cpgram(X$residuals)

## Plot of inputs, output, one-step ahead predicted output, and residuals par(mfrow=c(4,1),mar=c(3,3.5,1,1),mgp=c(2,0.7,0)) # Setup plot plot(X$t,X$Te,type="l") plot(X$t,X$Ti,type="l") plot(X$t,X$Q,type="l") lines(X$t,X$QHat,col=2) plot(X$t,X$residuals,type="l")

82