Numerical Solution of a Two-Point Boundary-Value Problem Caution: Do NOT wait un
ID: 3717231 • Letter: N
Question
Numerical Solution of a Two-Point Boundary-Value Problem Caution: Do NOT wait until the last minute!) Construct a computer program that uses both the secant method and the Runge ut a method that you developed in assignment #3 to obtain a numerical solution to the two-point boundary value problem: x'-(t,x) = x + 0.09 x 2 + cos(100 differential equation 0+1)-30-dary condition 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, xt 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 tot-1). Write your program so that the output shows the values ofx(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 t1 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 1.28 o.tExplanation / Answer
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;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.