//http://www.timestocome.com //linda cobb //winter 98-99 //solve coffee cooling problem using euler method //is it better to add cream first or later to cool //coffee quickly? //temperatures are in C #include #include #define TIMESTEP 0.001 void main (void){ //try to solve T-Ts = c(exp)^(-rt) double Ts=20; //room temperature double Told; //begining temperature of coffee double t = 0.0; //begining time double r; //estimated cooling constant double h; //time step double Tnew; //new temperature double C; //constant of integration (T-Ts) int j=0; //black coffee h = TIMESTEP; r = 0.027; C = 62.3; Told = 90.0; Tnew = 90.0; printf("\n Black Coffee \n"); while(Tnew >= 80.0){ //cool enough to drink at 75' if(t < TIMESTEP){ Tnew = Told; }else{ Tnew = Told - h*r*(Told-Ts); } t += TIMESTEP; Told = Tnew; j+=1; if(j >= 1000 ){ printf("\n minutes passed = %d, Temp = %.1f ",(int)t, Tnew); j = 0; } if(Tnew <= 80.0){ Tnew -= 5; printf("\n added cream at %.1f minutes, now the temperature is %.1f", t,Tnew); } } printf("\n+++++++++++++++++++++++++++++++++"); //coffee w/ cream r = 0.025; C = 48.8; Told = 90.0 - 5.0; //cream cools coffee by 5' Tnew = 90.0 - 5.0; t = 0.0; j = 0; printf("\n Coffee w/ cream\n"); while(Tnew >= 75){ if(t < TIMESTEP){ Tnew = Told; }else{ Tnew = Told - h*r*(Told-Ts); //previous value - timestep*dF/dt } t+= TIMESTEP; Told = Tnew; j+=1; if(j >= 1000){ printf("\n minutes passed = %.1f, Temp = %.1f ",t, Tnew); j=0; } } printf( "\n coffee w/ cream cool enough to drink at %.1f minutes, at a temperature of %.1f", t, Tnew); printf ("\n\n"); }