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

If you choose to write code, your program should output the values of x, y, f(x,

ID: 3864941 • Letter: I

Question

If you choose to write code, your program should output the values of x, y, f(x, y) and so on at each stage Mathematical software (Matlab. Maple. Mathematica, or other mathematical software) if you choose to use mathematical software, your program should output the values of x, y, f(x, y) and so on at each stage Your solution should allow you to solve problems of this type: Example. Given the differential equation dy/dx = x^2 - 0.5xy and initial condition y(1.5) = 2.2, approximate the value of y(2.5) using step size h = 0.03 Your solution must be able to carry out Euler's Method, Improved Euler's Method, and Runge-Kutta Your solution should display all the points (x, y) found along the way, not just the final point. Your solution should also display other values found while carrying out each method: a. Euler's Method: display the slope f(x, y) at each stage b. Improved Euler's: display the values of k1, k2 at each stage c. Runge-Kutta: display the values of k1, k2, k3, k4 at each stage d. You can display other values as well, if you wish (for example, the intermediate y-value in the Improved Euler method that we refer to as z). You should be able to relatively easily change the initial condition, step size, and target value. You should be able to relatively easily change the differential equation.

Explanation / Answer

/*Copy paste code in a file name NumericalMethods.java and run the program. Step wise output would be visible*/

public class NumericalMethods{

  public static void main(String []args){
    double x_initial=1.5;   
    double y_initial=2.2;
    double stepSize=0.05;
    double x_final=2.5;

double y_e = eulerMethod(x_initial,y_initial,stepSize,x_final,1);
double y_ie = improvedEulerMethod(x_initial,y_initial,stepSize,x_final,1);
double y_rg = rungaKuttaMethod(x_initial,y_initial,stepSize,x_final,1);
System.out.println("Final value of y(2.5) from Euler Method ="+ y_e);
System.out.println("Final value of y(2.5) from Improved Euler Method ="+ y_ie);
System.out.println("Final value of y(2.5) from Runga Kutta Method ="+ y_rg);
}

// returns the value for given differential equation for input value of x and y
public static double get_f_x_y(double x,double y){
return x*x + 0.5*x*y; // change the differential equation here
}

// returns final value after doing calculations from euler's method
public static double eulerMethod(double x, double y, double h, double xn,int i){
double fxy_present=get_f_x_y(x,y);
double xnext=x+h;
double ynext=y+(h*fxy_present);
System.out.println("Euler's Method x, y at "+ i+" step are "+xnext+","+ynext);
if (xnext>=xn) return ynext;
return eulerMethod(xnext,ynext,h,xn,++i);
}

// returns final value after doing calculations from improved euler's method
public static double improvedEulerMethod(double x, double y, double h, double xn,int i){
double k1=get_f_x_y(x,y);
double x2=x+h;
double y2=y+(h*k1);
double k2=get_f_x_y(x2,y2);
double xnext=x2;
double ynext= y+h*(k1+k2)/2;
System.out.println("Impoved Euler's Method k1, k2 at "+ i+" step are "+k1+","+k2);
if (xnext>=xn) return ynext;
return improvedEulerMethod(xnext,ynext,h,xn,++i);
}

// returns final value after doing calculations from runga kutta method
public static double rungaKuttaMethod(double x, double y, double h, double xn,int i){
double K1 = h * get_f_x_y(x, y) ;
double K2 = h * get_f_x_y(x + h/2, y + K1 /2) ;
double K3 = h *get_f_x_y(x + h/2, y + K2 /2) ;
double K4 = h *get_f_x_y(x + h, y + K3 ) ;
double ynext = y + ( K1 + 2*K2 + 2*K3 + K4 )/6;
System.out.println("Runga Kutta Method k1,k2,k3,k4 at "+ i+" step are "+K1+","+K2+","+K3+","+K4);
if (x+h>=xn) return ynext;
return rungaKuttaMethod(x+h,ynext,h,xn,++i);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote