Hello, all. I\'ve been trying to program a solution to this two-point boundary v
ID: 2943115 • Letter: H
Question
Hello, all. I've been trying to program a solution to this two-point boundary value problem in C++.
The initial guesses for this problem are x(0) = 0.7 and x(1) = 1.0, and I have to get an approximation to x(0) within a tolerance of 10^(-4). I'm supposed to use both the secant method and the Runge-Kutta method to obtain this solution, and use a stepsize of 0.025. The secant method must show the values of, x(0), x(1), and x(0) + x(1) - 3 (the error) with each iteration. Finally, I have to re-integrate the problem from t=0 to t=1 and print out the solution over the range of [0,1].
Right, that's the problem. I initially hoped this problem would be as simple as jamming the secant method and Runge-Kutta method together, but evidently that isn't the case and my program isn't producing results anywhere near what's expected. I'd have expected x(0) and x(1) to become larger to meet the requirements of the boundary condition but the exact opposite has happened. Heck, I doubt I even know what I'm doing at this point and my eyes are starting to glaze over. The book I'm using isn't much help either and I'm just about ready to give up. Anyway, I'm including my code down below. If you can tell me what I'm doing wrong I'd greatly appreciate it. Thanks.
#include <iostream>
#include <cmath>
using namespace std;
// performs the differential equation
double f(double t2, double x2)
{
double xDerivative = x2 + 0.09*(x2*x2) + sin(10 * t2);
return xDerivative;
}
//performs third-order Runge-Kutta method
double RK3(double x)
{
double K1,K2,K3; //these are function evaluations for the third order method
double t = 0;
double h = 0.025;
int nSteps = 40;
// the following loop will execute once for each step
for (int i = 1; i <= nSteps; i++)
{
K1 = h*( f( t , x ));
K2 = h*( f( (t + (1/2.)*h), (x + (1/2.)*K1) ));
K3 = h*( f( (t + (3/4.)*h), (x + (3/4.)*K2) ));
//performs function evaluations
x = x + (1/9.)*(2.*K1 + 3.*K2 + 4.*K3);
//determines value of x
t = t + h;
//determines new value of t
}
return x;
// cout << "---------- " ;
}
// determines solution to boundary value problem
double g(double s)
{
return (s + RK3(s) - 3.0);
}
void twoPoint()
{
double t0 = 0.0;
double h0 = 0.025;
double x0 = 0.7; // first guess for x of n (also used as the n-1 value of x)
double x1 = 1.0; // second guess for x of n (also used as the nth value of x)
double x2, temp, fx0, fx1, solution;
// initializes values for n+1 of x, a temporary value,
// and the results of f(x of n). Also includes solution to boundary value problem
int n = 1; //counter
fx0 =
fx0 = x0 + 0.09*(x0 * x0) + sin(10 * t0*(x0));
// calculates the value of f(the n-1 value of x)
fx1 = x1 + 0.09*(x1 * x1) + sin(10 * t0*(x1));
// calculates the value of f(the n value of x)
cout.precision(4);
// sets precision to 10
//secant method
for (n = 1; n <= 40; n++)
{
if (fx0 > fx1)
{
temp = x1;
x1 = x0;
x0 = temp;
temp = fx1;
fx1 = fx0;
fx0 = temp;
// if f(the n-1 value of x) is greater than f(the n value of x), their
// values will be swapped
}
x2 = x1 - (g(x1)*(x1-x0))/(g(x1)-g(x0));
// calculates the n+1 value of x
solution = (x0 + x1 - 3.0); //calculates solution to the boundary condition
cout << "X(0): " << x0 << " X(1): " << x1 << " Solution: " << solution << ' ';
// reports values of x0, x1, and the solution
// if (solution < 0.0001)
// {
// cout << " Boundary condition satisfied. ";
// break;
// }
// ensures that the loop stops if the solution goes below 0.0001
// and meets requirement that boundary condition is satisfied to
// within a tolerance of 10^(-4)
x1 = x0; //sets the n value of x to be equal to the n-1 value of x
fx1 = fx0; //sets f(the n value of x) equal to f(the n-1 value of x)
x0 = x2;
fx0 = x0 + 0.09*(x0 * x0) + sin(10*t0*(x0));
}
cout<< " Test: " << g(x0);
cout << " Solution from t = 0 to t = 1: ";
while (t0 < 1)
{
cout << "Solution: " << g(t0) << ' ';
t0 = t0 + 0.025;
}
// char wait;
// cin >> wait; //waits for input from user before closing
}
int main()
{
twoPoint();
char wait;
cin >> wait;
return 0;
}
Explanation / Answer
breasts
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.