Help with vector problem Runge Kutta

hello this is my first post so sorry if it looks all crazy with code. Im having trouble with a 4th order Runge Kutta method problem. I dont know why it just spits out the initial condition i set for the whole loop instead of doing iterations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <fstream>
#define R .1
#define C 1.0
#define h .05
#define tend 1.0
using namespace std;

int main()
{

ofstream myfile;
myfile.open ("example2.txt");

int a = tend/h;

vector<double> volt (a);
vector<double> k1 (a);
vector<double> k2 (a);
vector<double> k3 (a);
vector<double> k4 (a);
cout<<"enter an initial condition "<<endl;
cin>>volt[0];

cout<<"time,t" <<"\t"<< "voltage,v "<<endl;

for(int i = 0; i<=20 ; i ++)
{

k1[i] = (-1*volt[i])/(R*C);

k2[i] = (-1)*(volt[i] + .5*k1[i]*h)/(R*C);

k3[i] = (-1) * (volt[i] + .5*k2[i]*h)/(R*C);

k4[i] = (-1) * (volt[i] + h)/(R*C);

volt[i+1] = (volt[i] + ((1/6)*h*(k1[i] + 2*k2[i] + 2*k3[i] + k4[i])));

cout<<i*h<<"\t"<<volt[i]<<endl;

}
myfile.close();


    return 1;
}
The code is executed sequentially from top to bottom.
k1[i] = (-1*volt[i])/(R*C); at this point volt[i] has 0 as value.
i ask the user to give it the initial value of the vector. the problem is that it just prints that value that you input over and over. what order would you recommend to put the equations in? im stumped on this.
Sorry, you are right.
The problem is in line 39. 1/6 integer division returns an integer (0 in this case)
Use 1.0/6 to force doubles.

By the way for(int i = 0; i<=20 ; i ++) will access your array out of bounds.
They are of size 20, but you try to set the 21th and 22th element
O makes sense thank you!
Topic archived. No new replies allowed.