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: 3826939 • 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 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].

I have used this code so far but I feel there is something missing, I would need help on improving it so I can fulfill the conditions stated above.

import java.lang.*;

public class TwoPointValue
{

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;

}

}

Explanation / Answer

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