-2

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: Error message snapshot

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.
    }

}
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
Erez Sabran
  • 13
  • 1
  • 7

1 Answers1

0

If you want to use multiple return you can use std::tuple

std::tuple<double,double,double,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 std::make_tuple(k1, k2, k3, k4);
}

And returned value can be captured

for (int i {0}; i<n; ++i) {
    double k1, k2, k3, k4;
    auto ret = rk4funct (tvector[i], yvector[i], d);
    k1 = std::get<0>(ret);
    k2 = std::get<1>(ret);
    k3 = std::get<2>(ret);
    k4 = std::get<3>(ret);

    yvector[i+1] = yvector[i] + (d/6)*(k1+2*k2+2*k3+k4);
    tvector[i+1] = tvector[i] + d; //same here.
}

Error fixed code

//Runge Kutta
#include <iostream>
#include<vector>
#include <tuple>
using namespace std;

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


std::tuple<double,double,double,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 std::make_tuple(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) {

        double k1, k2, k3, k4;
        auto ret = rk4funct (tvector[i], yvector[i], d);
        k1 = std::get<0>(ret);
        k2 = std::get<1>(ret);
        k3 = std::get<2>(ret);
        k4 = std::get<3>(ret);

        yvector[i+1] = yvector[i] + (d/6)*(k1+2*k2+2*k3+k4);
        tvector[i+1] = tvector[i] + d; //same here.
    }

}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
  • 1
    Why not just `std::tie(k1, k2, k3, k4) = rk4funct(tvector[i], yvector[i], d);`? – Henri Menke Aug 17 '17 at 00:05
  • How big is `yvector` (and `tvector`) inside the loop where `yvector[i]` and `yvector[i+1]` are used? – Bob__ Aug 17 '17 at 08:02
  • This provides a solution to a problem resulting from a very bad design decision. There is no reason to cut the usual RK4 step at exactly this point. In the cases where `k1,..,k4` are re-used for interpolation or to avoid repeated vector allocation, the general design of interfaces and data structures would look rather different. – Lutz Lehmann Aug 18 '17 at 05:22
  • definitely there may be problem is in logic ranga kutta above post just solves OP error message – Hariom Singh Aug 18 '17 at 05:24
  • 1
    With C++17 you can now also use `auto[k1, k2, k3, k4] = rkfunct(...)` – Mark A. Ropper Mar 20 '19 at 16:47