Está en la página 1de 2

#include <iostream>

#include <cmath>
#include <iomanip>
using namespace std;
void rkutta(double, double, double, double, double);
double funcion(double, double);
int main()
{
double x0, xf, y0, n, j, h;

cout << "Este programa encuentra la solucion a la EDO y'= x - y" << endl;
cout << "Utilizando el metodo de Runge-Kutta" << endl;

cout << "Capture la condicion inicial de X0" << endl;
cin >> x0;

cout << "Capture la condicion inicial de Xf " << endl;
cin >> xf;

cout << "Capture el valor inicial de Y0" << endl;
cin >> y0;

cout << "Capture el numero de iteraciones deseadas" << endl;
cin >> n;

j = (xf - x0);
h = j / n;

rkutta(x0, xf, n, h, y0);
return 0;
}

void rkutta(double x0, double xf, double n, double h, double y0)

{
double i, k1, k2, k3, k4;

for (i = 1; i <= n; i++)
{
k1 = funcion(x0, y0);
k2 = funcion((x0 + (h / 2)), (y0 + ((h*k1) / 2)));
k3 = funcion((x0 + (h / 2)), (y0 + ((h*k2) / 2)));
k4 = funcion((x0 + h), (y0 + (h*k3)));

y0 = y0 + ((h / 6)*(k1 + (2 * k2) + (2 * k3) + k4));
x0 = x0 + h;
}

cout << "La funcion evaluada en Xf= " << xf << " es " << y0 << endl;
}


double funcion(double x, double y)
{
return (3 + exp(-x) - (0.5*y));
//return ((x*exp(x))+(y/x));
//return (4-x+(2*y));
//return (3+exp(-x)-(0.5*y));
}

También podría gustarte