How to do a loop to re-calculate the formula in R?

前端 未结 1 1397
广开言路
广开言路 2021-01-27 13:36

My question is how to use loop(while) 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         


        
相关标签:
1条回答
  • 2021-01-27 13:47

    as put in comments :

    EDIT

    First run your code:

      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
    
      ## calculate Theta_g by Tbo ##
    
      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
    
    ##calculate Theta_a by Tbo ##
    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)
    

    Then add the while loop to your code:

     while ((Tb-Tbo)>=0.01) { 
    
     Tbo=Tb
    
      ## calculate Theta_g by Tbo ##
    
      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
    
    ##calculate Theta_a by Tbo ##
    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)
    
       }
    
    0 讨论(0)
提交回复
热议问题