sample code. Write a C-language program called warm_beer that applies the Euler
ID: 3850817 • Letter: S
Question
sample code.
Write a C-language program called warm_beer that applies the Euler algorithm (2) to solve Newton's equation (1) for the temperature of a can of chilled beer over a period of two hours-assume the constants given in part (d) above. Your code should display (on standard output) two columns of output: time (since start of experiment), and temperature (with a precision of 1 decimal place). NB: A sample C-language code is listed inside the front cover of this exam paper. //Sample C-language program to print a table of sine values //0 0.0 0.0000 //1 10.0 0.1736 //2 20.0 0.3420 //... .. .. //9 90.0 1.0000 #include #include #define PI 4.0*atan(1.0) int main() { int i: double th_d, th: for (i = 0: iExplanation / Answer
#include <stdio.h>
#include <math.h>
#define FMTValue " %7.3f"
typedef double (*derivative_f)(double, double);
void analyticInitial()
{
double t;
printf(" Time: ");
for (t = 0; t <= 120; t += 20) printf(" %7g", t);
printf(" analytic: ");
for (t = 0; t <= 120; t += 20)
printf(FMTValue, 20 + 80 * exp(-0.07 * t));
printf(" ");
}
double coolingFactor(double temp)
{
return -0.07 * (temp - 20);
}
void eulerMethod(derivative_f fval, double y, int stepval, int end_t)
{
int t = 0;
printf(" Step value %2d: ", (int)stepval);
do {
if (t % 10 == 0) printf(FMTValue, y);
y += stepval * fval(t, y);
} while ((t += stepval) <= end_t);
printf(" ");
}
int main()
{
analyticInitial();
eulerMethod(coolingFactor, 100, 1, 100);
return 0;
}
output:
Time: 0 20 40 60 80 100 120
analytic: 100.000 39.728 24.865 21.200 20.296 20.073 20.018
Step value 1: 100.000 110.850 114.700 111.550 101.400 84.250 60.100 28.950 -9.200 -54.350 -106.500
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.