Write a C++ program that can solve the following Boundary-Value Second Order Dif
ID: 3765212 • Letter: W
Question
Write a C++ program that can solve the following Boundary-Value Second Order Differential Equation: y" + 4y' + 4y = 0, with y(0) = 0 and y(1) = 3 Your program must use the Shooting Method, in combination with any one of the fourth order Runge-Kutta methods we reviewed in class. The program must prompt the user to provide a first guess of the slope of the solution at x = 0. You can select any step size you consider reasonable; proof of solution will be decided on how well the calculated results match the analytical solution at x = 1. As part of the presentation of results, you must prepare an Excel plot of the calculated values and the "exact" values. The analytical solution is: y = 3x e^-2x+2Explanation / Answer
#include <stdio.h>
#include <math.h>
#define SIZE 100
double X[SIZE];
double Y[3][SIZE], Z[3][SIZE];
double cc,er,xl,x0,h,ya,yb,yex;
int k,kl,l;
double F(double x, double y,double z)
{
return z;
}
// z'= -2sin(x)e^5-x
double G(double x,double y,double z)
{
double gg;
gg = -2sin(x)e^5-x;
if (l==2) gg += 5.0*x*x;
return gg;
}
double FX(double x)
{
return 8.0/x+x*x*x*(x+2.0);
}
void Boundary_Value(double x,double y,double z,double h,double *x1,double *y1, double *z1) {
double c1,c2,c3,c4,d1,d2,d3,d4,h2;
c1=F(x,y,z);
d1=G(x,y,z);
h2=h/2.0;
c2=F(x+h2,y+h2*c1,z+h2*d1);
d2=G(x+h2,y+h2*c1,z+h2*d1);
c3=F(x+h2,y+h2*c2,z+h2*d2);
d3=G(x+h2,y+h2*c2,z+h2*d2);
c4=F(x+h,y+h*c3,z+h*d3);
d4=G(x+h,y+h*c3,z+h*d3);
*x1=x+h;
*y1=y+h*(c1+2.0*c2+2.0*c3+c4)/6.0;
*z1=z+h*(d1+2.0*d2+2.0*d3+d4)/6.0;
}
void main()
{
x0=1.0;
xl=2.0;
kl=20;
h=(xl-x0)/kl;
ya=11.0;
yb=36.0;
X[0]=x0;
for (l=1; l<3; l++)
{
Y[l][0]=(l-1)*ya; Z[l][0]=1.0;
for (k=0; k<kl; k++)
Boundary_Value(X[k],Y[l][k],Z[l][k],h,&X[k+1],&Y[l][k+1],&Z[l][k+1]);
}
}
cc=(yb-Y[2][kl])/Y[1][kl];
printf(" ");
printf("X Y estimated Y true Absolute Error ");
for (k=0; k<=kl; k++)
{
Y[0][k]=Y[2][k] + cc*Y[1][k];
yex=FX(X[k]);
er=fabs(yex-Y[0][k]);
printf("%9.4f%12.7f %12.7f %9.2e ", X[k],Y[0][k],yex,er);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.