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= y
i + h * f(x
i, y
i)".
It is "y
i+1= y
i + h * f(x
i, y
i)"
The next step value y
i+1 is calculated as a delta from the current step value y
i. 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.