In: Computer Science
Design and construct a computer program in one of the approved languages (C, C++, C#, Java, Pascal, Python, etc.) 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. .DO NOT USE a[1] or a[2] = 1/2. 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) = 2 x2 cos(4 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 .
The answer at the end of the integration is about 1.978940602164785990777661.
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.
Write down (in your output file or in a text file) any conclusions that you can make from these experiments (e.g., what happens as h is decreased?).
import java.io.*;
//import java.Math;
class differential
{
double dydx(double t, double x)
{
return (2*x*2*Math.cos(4*t));
}
// Finds value of y for a given x using step size h
// and initial value y0 at x0.
double rungeKutta(double x0, double y, double x, double h)
{
differential d1 = new differential();
// Count number of iterations using step size or
// step height h
int n = (int)((x - x0) / h);
//System.out.println(n);
double k1, k2, k3, k4, k5;
for (int i = 1; i <= n; i++)
{
// Apply Runge Kutta Formulas to find
// next value of y
k1 = h * (d1.dydx(x0, y));
k2 = h * (d1.dydx(x0 + 0.5 * h, y + 0.5 * k1));
k3 = h * (d1.dydx(x0 + 0.5 * h, y + 0.5 * k2));
k4 = h * (d1.dydx(x0 + h, y + k3));
// Update next value of y
y = y + (1.0 / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4);
//System.out.println(y);
// Update next value of x
x0 = x0 + h;
}
return y;
}
}
class Main{
public static void main(String args[])
{
differential d2 = new differential();
double t = 0.0, y = 1, t1 = 2;
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.1));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.05));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.025 ));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.0125 ));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.00625 ));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.003125 ));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.0015625 ));
double sum=0;
sum+=d2.rungeKutta(t, y, t1, 0.1)+d2.rungeKutta(t, y, t1,
0.05)+d2.rungeKutta(t, y, t1, 0.025 )+d2.rungeKutta(t, y, t1,
0.0125 )+d2.rungeKutta(t, y, t1, 0.00625 )+d2.rungeKutta(t, y, t1,
0.003125 )+d2.rungeKutta(t, y, t1, 0.0015625 );
//System.out.println(sum);
}
}
As the value of h is decrease the value of x(t) will increase therefore they have inversally relationship.
If you found this answer helpful please give a thumbs up.