//#include "pch.h"
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double df(double x, double y)            //function for defining dy/dx
{
	double a = (x * x) / (y - 1);                //========Enter the Differential Equation to be solved========================================
	return a;

}

int main()
{
	char ch, ch1;
	cout << "This is the Runge Kutta Method calculator." << endl;
	cout << "Enter the differential equation to be solved in line 8 of the code" << endl;
	cout << "have you entered the right equation? (Enter Y or N)" << endl;
	cin >> ch;
	ch1 = ch;
	while (ch1 == 'y' || ch1 == 'Y') {


		int n;
		double h,x0, y0, x, y, k1, k2,k3, k4, z2, z3, z4;            
		cout.precision(5);						//sets number of decimal places to be displayed.
		cout.setf(ios::fixed);					//displays floating point numbers in standard notation
		cout << "\nEnter the initial values of x and y respectively:\n";        
		cin >> x0 >> y0;
		cout << "\nFor what value of x do you want to find the value of y\n";
		cin >> x;
		cout << "\nEnter the step size h:\n";            
		cin >> h;							//set size to 0.05 for percision
		cout << "x0" << setw(16) << "y0" << setw(19) << "K1" << setw(16) << "k2" << setw(19) << "K3" << setw(16) << "k4" << setw(16)<< "y(0+1)\n";
		cout << "---------------------------------------------------------------------------------------------------------\n";
		while (fabs(x - x0) > 0.0000001)
		{           //calculate next y   
			k1 = df(x0, y0);
			z2 = y0 + (h / 2)*k1;
			k2 = df(x0 + (h / 2), z2);
			z3 = y0 + (h / 2)*k2;
			k3 = df(x0 + (h / 2), z3);
			z4 = y0 + h * k3;
			k4 = df(x0 + h, z4);
			y = y0 + h * (k1 + 2 * k2 + 2 * k3 + k4) / 6;

			cout << x0 << setw(16) << y0 << setw(16) << k1 << setw(16) << k2 << setw(16) << k3 << setw(16) << k4 << setw(19)<<y<< endl;
			y0 = y;                    //pass this new y as y0 in the next iteration.
			x0 = x0 + h;                //calculate new x.
		}
		cout << x0 << setw(16) << y << endl;
		cout << "----------------------------------------------------------------------" << endl;
		cout << "y(" << x0 << ") = " << y << endl;
		break;
	}
	goto EndLable;
EndLable:
	cout << "----------------------------------------------------------------------" << endl;
	cout << "Program will end so that you can enter the differential equation to be solved" << endl;

	system("Pause");
	return 0;
}