mercredi 16 août 2017

Runge-Kutta method on c++

i'm trying to code a simple runge-kutta method

The function to be approximated and the runge-kutta method are separate definitions, which are called within the loop in the main function.

approximate solutions for y and t are pushed into a separate vectors.

I'm receiving the following errors and struggling to figure out why: enter image description here

The code:

//Runge Kutta
#include "C:\Users\Erez\Documents\Dev C++ Projects\std_lib_facilities.h"

double f (double t,double y){ 
   f = t + y;
return f;
}

double rk4funct (double t,double y,double d){
  double k1, k2, k3, k4;
  k1 = d * f(t, y);
  k2 = d * f(t+0.5*d, y+0.5*k1);
  k3 = d * f(t+0.5*d, y+0.5*k2);
  k4 = d * f(t+d, y+k3);
return k1, k2, k3, k4;
}

int main (){

  vector<double> yvector;
  vector<double> tvector;
  double y0 {0}; //initial y value
  double t0 {0}; //initial time value
  double d {0}; //step size
  int n {0}; //number of iterations

  cout << "Please input variables according to the following order: step size, y0, t0, n(# of iterations)\n";
  cin >> d >> y0 >> t0 >> n;
  yvector.push_back(y0);
  tvector.push_back(t0);


    for (int i {0}; i<n; ++i;) {
      rk4funct (tvector[i], yvector[i], d); 
      yvector[i+1] = yvector[i] + (d/6)*(k1+2k2+2k3+k4); 
      tvector[i+1] = tvector[i] + d; //same here.
    }

}

Aucun commentaire:

Enregistrer un commentaire