如何循环重新计算R中的公式

时间:2015-03-31 01:25:01

标签: r

## my target is to get the final Tb and Theta_T##
=================================================================
Tmax = c(35.0,30.0,28.0,25.0,25.5,26.0)
Tmin = c(15.4,15.8,13.2,9.8,9.5,9.4)
X = c(0.233,0.233,0.220,0.065,0.138,0.083)
D = c(12,25,46,41,45,62)
Ts = 40
To = 30
Tbo = 0


##Theta_g
if (Tbo<Tmin){ 
  Theta_g = (Tmax+Tmin)/2
} else if (Tbo<Tmax & Tbo>Tmin){
  Theta_g = (Tmax+Tmin)/2+(Tbo-Tmin)^2/2*(Tmax-Tmin)
} else 
Theta_g = Tbo

##Theta_a
if (To>Tmin & Tmax<Ts){
  Theta_a = (Ts-Tbo)*(Tmax-To)^2/(2*(Ts-To)*(Tmax-Tmin))
} else if (To>Tmin & Tmax>=Ts){
  Theta_a = (Tmax*(Tmax-2*Tbo)-To*(Ts-Tbo)+Ts*Tbo)/(2*(Ts-To))
} else if (Tmin>=To & Tmin<Ts & Tmax<Ts){
  Theta_a = (Ts-Tbo)*(Tmax+Tmin-2*To)/(2*(Tmax-Tmin))
} else if (Tmin>=To & Tmin<Ts & Tmax>Ts){
  Theta_a = (Tmax+Tmin)/2-Tbo-(To-Tbo)*(Ts-Tmin)^2/(2*(Ts-To)*(Tmax-Tmin))
} else 
Theta_a = (Tmax+Tmin)/2-Tbo

## Theta_s
Theta_s = (Theta_g-Theta_a)*D

## Linear formular: Theta_s/X=Theta_T+Tb(D/X)
y = Theta_s/X
x = D/X

##Least square linear regress simulation
Tb = sum((x-mean(x))*(y-mean(y)))/sum((x-mean(x))^2)
Theta_T = mean(y)-Tb*mean(x)

=============================================================================== 
## the condition to run loop is: when |Tb-Tbo|>=0.01,let Tbo=Tb, and then go back,  
##   apply the new Tbo to get the new Tb,until |Tb-Tbo|<0.01##

0 个答案:

没有答案