Use C++ in Solving Ordinary Differential Equations using
a
Fourth-Order Runge-Kutta of Your Own Creation
Assignment:
Design and construct a computer program in C++ that will
illustrate the use of a fourth-order
explicit Runge-Kutta method of your own design. In other words, you
will first have to solve the Runge-Kutta equations of condition for
the coefficients
of a fourth-order Runge-Kutta method. See the
Mathematica notebook on solving the equations for 4th order RK
method.
That notebook can be found at rk4Solution.nb . PLEASE
DO NOT USE a[1] = 1/2 or a[2] =
1/2. In general,
you should pick a[1] and a[2] to be distinct values greater than
zero and less than one.
Then, you will use these coefficients in a computer program to
solve the ordinary differential equation below.
Be sure to follow the documentation and programming style policies
of the Computer Science Department.
The initial value problem to be solved is the following:
x'(t) = 3 x2 cos(5 t)
subject to the initial condition: x(0) = 1.0
Obtain a numerical solution to this problem over the range from t=0.0 to t=2.0
for seven different values of the stepsize,
h=0.1, 0.05 , 0.025 , 0.0125 , 0.00625 , 0.003125 , and 0.0015625 .
In other words, make seven runs with 20, 40, 80, 160, 320, 640,
and 1280 steps, respectively.
For each run, print out the value of h, then a table of t and x,
and then the error at t=2. You may use the following
very precise value for your "true answer" in order to compute the
error at t=2:
0.753913186469598763502963347.
Hint: It is often helpful to test your program on simple
differential equations (such as x' = 1 or x'=t or x'=x) as a part
of the debugging process.
Once you have worked these simple cases, then try working the
nonlinear differential equation given above for the assignment
(with a small stepsize).
Also, check your coefficients to make sure that they satisfy the
equations of condition and that you have assigned these correct
values to the
variables or constants in your program properly. For example, a
common error is to write something like: a2 =
1/2; when you meant to write
a2 = 1.0/2.0; so please be careful.
#include <iostream>
using namespace std;
double diffOfy(double x, double y) {
return ((x*x)+(y*y)); //function x^2 + y^2
}
double rk4thOrder(double x0, double y0, double x, double h)
{
int iteration = int((x - x0)/h); //calculate number of
iterations
double k1, k2, k3, k4;
double y = y0; //initially y is f(x0)
for(int i = 1; i<=iteration; i++) {
k1 = h*diffOfy(x0, y);
k2 = h*diffOfy((x0+h/2), (y+k1/2));
k3 = h*diffOfy((x0+h/2), (y+k2/2));
k4 = h*diffOfy((x0+h), (y+k3));
y += double((1.0/6.0)*(k1+2*k2+2*k3+k4)); //update y using del
y
x0 += h; //update x0 by h
}
return y; //f(x) value
}
int main() {
double x0, y0, x, h;
cout << "Enter x0 and f(x0): "; cin >> x0 >>
y0;
cout << "Enter x: "; cin >> x;
cout << "Enter h: "; cin >> h;
cout << "Answer of differential equation: " <<
rk4thOrder(x0, y0, x, h);
}
Get Answers For Free
Most questions answered within 1 hours.