[Confused] Euler's Method C++

so this is the problem,

solve using Euler's Method:

A ball at 1200K is allowed to cool down in air at an ambient temperature of 300K. Assuming heat is lost only due to radiation, the differential equation for the temperature of the ball is given by:

dy/dt = -2.2067 x 10^-12 (y^4 - 81 x 10^8 ), y(0)=1200K

should i stick with LOOPING or should i go with if, else statement?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdio.h>

int main()
{
      printf("Euler's Method Application in Chemical Engineering\n\n");
      printf("Problem:\n\n A metal alloy at 1200K is allowed to cool down in room temp. at 300K.\n Assuming heat is only lost due to radiation,\n the differential equation for the alloy is given by:\n");
      printf("\n dy/dt = -2.2067 x 10^-12 (y^4 - 81 x 10^8 ), y(0)=1200K\n\n ");
      printf("Find the temp. at  t = 480 seconds, assume a step size (h) of 240 seconds.\n\n");
      printf("Solution:\n\n\n");
      printf("y= yi + f(xi,yi)h\n\n");
      
      float temp_initial;
      float time_initial=1200;
      float temp_final;
      float h;
      float true_value=647.57;
      
      for(;temp_final<=true_value;)
      {
                               temp_final= temp_initial + 240*(-2.2067*10exp(-12)*(temp_initial*temp_initial -81*10 exp(8));
                               printf(" %.3f", temp_final);
                                        
      }
      getch();
      }

1. Your step size of 240 is awful huge for Euler integration, which completely depends on small step sizes to maintain any reasonable approximation of a differential equation. If that was one of the requirements of the problem, then so be it, but for a problem like this I would think a 1 second step size would be ideal, certainly no larger than a 10 second step size.

2. You should be looping through your time slices until you've reached your target time. It looks like you've somehow pre-determined the final temp to be 647.57, but if I'm reading correctly that is what you're supposed to be solving for. Your loop should look more like:

1
2
3
4
5
6
7
8
9
10
int timeslice = 1;  // or 10, or 240 as your code uses.
float current_temp = 1200;
for(int t = 0; t < 480; t += timeslice){
  //For each time_slice, the current temp drops some amount based on diff. equation
  current_temp +=  timeslice * (pow(-2.2067,-12)*(current_temp*current_temp -pow(81, 8));

}

//Once you've reached t = 480, current_temp is the temperature of the ball at 480 seconds.


Your Euler step formula wasn't quite right either. It isn't "y= yi + h * f(xi, yi)".

It is "yi+1= yi + h * f(xi, yi)"

The next step value yi+1 is calculated as a delta from the current step value yi. The initial value only represents the starting point (i=0), but you're using the initial temp for each step's calculation.

Also, the exp() function is an exponential function, not an exponent function. You want to use pow() instead, as my sample code uses. Note though, that my sample code may be off by a factor of 10.
Topic archived. No new replies allowed.