Mathematical Expression Produces Incorrect Result
I've implemented the Euler method (Euler method - Wikipedia, the free encyclopedia), but what I'm actually having a problem with is evaluating the exact solution.
The following is the mathematical expression I'm trying to compute:
(t^6 - 37)^(1/3).
Here is the C function:
Code:
double exact_solution( double t ){ //exact solution function
return pow(pow(t, 6) - 37, (1.0 / 3));
}
With an input of 3.0, the C function produces a result of: 8.783047
However a calculator produces: 8.845085422
What do you guys think?
EDIT: Okay. After testing just the expression, its not the problem. Here's the whole program.
Code:
#include <stdio.h>
#include <math.h>
double F( double t, double x ){ //slope function
return pow(t,5) * 2 / (x * x);
}
double exact_solution( double t ){ //exact solution function
return pow(pow(t, 6) - 37, (1.0 / 3));
}
int main(int argc, char *argv[]){
double t = 2; //independent var
double delta_t = .01; //step size
double x_n1 = 3; //x(0) = x_n1 ( initial value )
double x_n2;
double interval_size = 1;
double exact = exact_solution(t);
printf("step size -> %f\n\n", delta_t );
for(int i = 0;i < interval_size/delta_t; i++){
x_n2 = x_n1 + delta_t * F(t, x_n1);
x_n1 = x_n2;
exact = exact_solution(t);
t += delta_t;
printf("%f %f %f %f\n", t, x_n2, exact, fabs(exact - x_n2) / exact * 100);
}
return 0;
}