#include <math.h>
#include <iostream.h>
#define M_E 2.71828f
// Return the value of the function for the given input
float Part1Function(float t, float x)
{
// Return the value from
return sin(pow(M_E, t)) * pow(x, 2);
}
// Run the runge-kutta on the function given the h provided
void Runge_KuttaPart1Function(float h)
{
float k1, k2, k3, start_t, end_t, start_x;
start_t = 0.0f;
start_x = 1.0f;
end_t = 3.0f;
// Print out the table headers
cout << "tn \t k1 \t k2 \t k3 \t xn \n";
// Print out the first line of the table with the initial values
cout << start_t << "\t\t\t\t\t" << start_x << "\n";
// Move to the first iteration
start_t += h;
// Iterate through each h value and output the results
for(start_t; start_t != end_t;start_t += h)
{
// Calculate k1, k2, k3
k1 = h * Part1Function(start_t, start_x);
k2 = h * Part1Function((start_t + (h/2.0f)), (start_x + (k1/2.0f)));
k3 = h * Part1Function((start_t + h), ((start_x - k1)+(2.0f * k2)));
// Calculate the next y
start_x = start_x + ((1.0f/6.0f) * k1) + ((2.0f/3.0f) * k2) + ((1.0f/6.0f) * k3);
// Now print out this line of calculations
cout << start_t << "\t" << k1 << "\t" << k2 << "\t" << k3 << "\t" << start_x << "\n";
}
}
// Run the test on the various values of h for the first assignment
void main()
{
// Run Runge_KuttaPart1Function for each h in part 1
Runge_KuttaPart1Function(1.0f);
}
Any idea why I don't get any output?
I have a very simple console application that looks like this.
When I the program I get a console window up, but no output. Any idea why?
Mark
Make sure you flush the ostream when you want the text to be displayed. Most streams buffer text instead of displaying right away. Also, step through your code with your debugger (or set breakpoints in correct places) to determine what is executed and what isn't.
Other than that:
1° You should use cmath and iostream instead of math.h and iostream.h (which are not standard C++ headers).
2° The probability of start_t and end_t, two floating point numbers, being equal to each other is extremely close to zero.
3° #define should not be used for constants in C++, that's what constants are for.
Other than that:
1° You should use cmath and iostream instead of math.h and iostream.h (which are not standard C++ headers).
2° The probability of start_t and end_t, two floating point numbers, being equal to each other is extremely close to zero.
3° #define should not be used for constants in C++, that's what constants are for.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement