In: Computer Science
Create a java application that can solve the following differential equation and boundary condition with the Runge Kutta method and the secant method.
x' = f(t,x) = x + 0.09x2 + cos(10*t) differential equation
x(0) + x(1) - 3.0 = 0 boundary condition
Starting with the initial guesses 0.7 and 1.0 for the (unknown) initial value, x(0), obtain an approximation to x(0) {for the final solution, x(t)} such that the boundary condition is satisfied to within a tolerance of 10-4 . Use a fixed stepsize of 0.025 (i.e., take 40 steps each time you integrate the differential equation from t=0 to t=1). Write your program so that the output shows the values of x(0), x(1), and x(0)+x(1)-3 (the error in satisfying the boundary condition) at the end of each iteration of the secant method. After the last iteration of the secant method, re-integrate from t=0 to t=1 and print out the solution for x(t) over the range [0,1]. Your solution for x(t) should resemble the solution plotted below.
You approximation to x(0), when you finish, should be roughly 0.7378743818.
Here I am providing the java code. Hope it helps. Please like my answer for my work. If any doubts please comment. I will help in clearing it.
* This problem uses Euclid's method and the fourth
* order Runge-Kutta method to compute at x=1
* for the D.E. dy/dx = x + 0.09 x 2 + cos(10 t)
* with initial value x = 0 to x = 1 and t = 0 to t = 1
public class RungeKutta
{
// The number of steps to use in the interval
public static final int STEPS = 100;
// The derivative dy/dx at a given value of x and y.
public static double deriv(double x, double y)
{
return x + Math.sqrt(0.09*2)+ cos(10t);
}
// The `main' method does the actual computations
public static void main(String[] argv)
{
// `h' is the size of each step.
double h = 1.0 / STEPS;
double k1, k2, k3, k4;
double x, y;
int i;
// Computation by Euclid's method
// Initialize y
y = 0;
for (i=0; i<STEPS; i++)
{
// Step through, updating x and incrementing y
x = i * h;
y += h * deriv(x, y);
}
// Print out the result that we get.
System.out.println("Using the Euler method "
+ "The value at x=1 is:");
System.out.println(y);
// Computation by 4th order Runge-Kutta
// Initialize y
y = 0;
for (i=0; i<STEPS; i++)
{
// Step through, updating x
x = i * h;
// Computing all of the trial values
k1 = h * deriv(x, y);
k2 = h * deriv(x + h/2, y + k1/2);
k3 = h * deriv(x + h/2, y + k2/2);
k4 = h * deriv(x + h, y + k3);
// Incrementing y
y += k1/6 + k2/3+ k3/3 + k4/6;
}
// Print out the result that we get.
System.out.println();
System.out.println("Using 4th order Runge-Kutta "
+ "The value at x=1 is:");
System.out.println(y);
// Computation by closed form solution
// Print out the result that we get.
System.out.println();
System.out.println("The value really is:");
y = (Math.exp(0.5) - Math.exp(-0.5)) / 2;
System.out.println(y);
}
}
Thank you. Please like.