library(ggplot2) library(deSolve) library(gridExtra) library(cowplot) #signal <- data.frame(times = times, import = rep(0, length(times))) #Text=read.csv("input.csv") #head(signal) #signal$import <- ifelse((trunc(signal$times) %% 2 == 0), 0, 1) #signal[8:12,] #head(signal) #df <- read.csv("dataWEEK2.csv",col.names=c("DateTime", "DB", "RH","DP","P","WIND","WIND-DIR","SUN","RAIN","START")) df <- read.csv("dataWEEK2.csv",col.names=c("DateTime", "DB")) df=df[complete.cases(df), ] df$DateTime=as.Date(df$DateTime) df$DB=(df$DB/10) input <- approxfun(df$DB, rule = 2) model <- function(t, y, parms) { with(as.list(c(parms, y)), { Text <- input(t) # <---- here dy1 <- ifelse(y[2] == 0, -((qv * pa * Ca * (y[1] - Text)) / (V * pa * Ca)), -((qv * pa * Ca * (y[1] - Text)) / (V * pa * Ca))+((q2 * pa * Ca * (60-y[1])) / (V * pa * Ca))) dy2 = 0 # dy2 = 1 # dy2 <- y[2] list(c(dy1,dy2),Text = Text,Heating=(y[2]*((q2 * pa * Ca * (60-y[1])) / (V * pa * Ca))*Ca*V*pa)/1000) }) } parms = c(qv = 0.5, pa = 1.204, Ca = 1006, V = 300, q2=2) out <- ode(y = c(Tr=18), times = seq(0,nrow(df),1), func = model, parms) out=as.data.frame(out) head(out,n=50) #parms = c(qv = 54, pa = 1.204, Ca = 1006, V = 3000) #model <- function(time, Tr,Text, parms) { #df <- read.csv("dataWEEK.csv",col.names=c("DateTime", "DB", "RH","DP","P","WIND","WIND-DIR","SUN","RAIN","START")) #df=df[complete.cases(df), ] #df$DateTime=as.Date(df$DateTime) #df$DB=(df$DB/10) # #Text=parms[5] #dTr <- -(parms[1] * parms[2] * parms[3] * (Tr - Text)) / (parms[4] * parms[2] * parms[3]) #dText = df$DB #list(dTr,Text) #}# #solution <- ode(y = 20, times =as.numeric(rownames(df)), func = model, parms = parms) #print(solution) #solution=as.data.frame(solution) names(out) <- c("time", "Tr","Text") head(out) ##head(solution) # # Sample data for plot1 # Create ggplot objects plot1 <- ggplot(out, aes(x=time, y=Tr)) + geom_line(color="red")+ geom_line(data=out, aes(x=time,y=Text),color="blue") # Open a PDF device to save the plots pdf("tx.pdf", width = 40, height = 20) # Arrange and print the plots using gridExtra grid.arrange(plot1,nrow = 1) # Or arrange and print the plots using cowplot # plot_grid(plot1, plot2, nrow = 1) # Close the PDF device to save the plots to the PDF file dev.off() #gg_plot <- ggplot(data = out, aes(x = time, y = Total)) + # geom_line() + # geom_line(aes(x = time, y = Text), color = "blue")+ # geom_line(aes(x=time,y=Sig),color="red")+ # labs(x = "Time Steps", y = "Room Temperature (Tr)") + # ggtitle("Room Temperature Over Time") ## #ggsave(filename = "room_temperature_plot.pdf", plot = gg_plot)