Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Construct a computer program that uses both the secant method and the Runge-Kutt

ID: 3827361 • Letter: C

Question

Construct a computer program that uses both the secant method and the Runge-Kutta method (that you developed in assignment #3) to obtain a numerical solution to the two-point boundary-value problem:

x' = f(t,x) = x + 0.09 x 2 + 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 step size 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. Also, your approximation to x(0) when you finish, should be roughly 0.7378743.

Please tell me a detailed explanation of what to do, or show me java code for it, I want to learn it but I just don't know how to do it.

PS. needs to use secant method and Runge-Kutta method

Explanation / Answer

Below is the solution to your problem:

Program in JAVA:

package java_project;
// An example of solving a boundary-value problem via
//the shooting method. The Runge-Kutta and secant
//methods are used for integration and root search.
import java.lang.*;
public class LinearDEq {
static final int n = 100, m = 5;
public static void main(String argv[]) {
double y1[] = new double [n+1];
double y2[] = new double [n+1];
double y[] = new double [2];
double h = 1.0/n;

// Find the 1st solution via Runge-Kutta method
y[1] = 1;
for (int i=0; i<n; ++i) {
double x = h*i;
y = rungeKutta(y, x, h);
y1[i+1] = y[0];
}

// Find the 2nd solution via Runge-Kutta method
y[0] = 0;
y[1] = 2;
for (int i=0; i<n; ++i) {
double x = h*i;
y = rungeKutta(y, x, h);
y2[i+1] = y[0];
}

// Superpose two solutions found
double a = (y2[n]-1)/(y2[n]-y1[n]);
double b = (1-y1[n])/(y2[n]-y1[n]);
for (int i=0; i<=n; ++i)
y1[i] = a*y1[i]+b*y2[i];

// Output the result in every m points
for (int i=0; i<=n; i+=m)
System.out.println(y1[i]);
}

// Method to complete one Runge-Kutta step.

public static double[] rungeKutta(double y[],
double t, double dt) {
int k = y.length;
double k1[] = new double[k];
double k2[] = new double[k];
double k3[] = new double[k];
double k4[] = new double[k];
k1 = g(y, t);
for (int i=0; i<k; ++i) k2[i] = y[i]+k1[i]/2;
k2 = g(k2, t+dt/2);
for (int i=0; i<k; ++i) k3[i] = y[i]+k2[i]/2;
k3 = g(k3, t+dt/2);
for (int i=0; i<k; ++i) k4[i] = y[i]+k3[i];
k4 = g(k4, t+dt);
for (int i=0; i<k; ++i)
k1[i] = y[i]+dt*(k1[i]+2*(k2[i]+k3[i])+k4[i])/6;
return k1;
}

// Method to provide the generalized velocity vector.

public static double[] g(double y[], double t) {
int k = y.length;
double v[] = new double[k];
v[0] = y[1];
v[1] = -Math.PI*Math.PI*(y[0]+1)/4;
return v;
}
}

Output:

0.0
0.2129983041900803
0.40080861312479454
0.5649636256018957
0.7069561449425062
0.828236289123789
0.9302091477944836
1.014232854662855
1.0816170448765035
1.1336216681704763
1.1714561297365957
1.196278731955946
1.2091963913316894
1.2112646061549446
1.2034876516272297
1.186818980344055
1.1621618072115387
1.1303698590174687
1.0922482700068197
1.0485546059162312
0.9999999999999996