Using Runge Kutta method to solve a system of ODEs

Hello everybody
I try to solve a system of ODEs with Visual Studio 2015, and VS shows me result of program but It seems like VS didn't calculate anything.

I think I have problem with 5 functions f1,f2,f3,f4,f5 or arrays, that's why VS can not implement my command. But I don't know where is the mistake. how to fix the program. Thank you in advance! I really appreciate your help.
Below are system of ODEs, result showed in VS and code of program
system of ODEs
f'''+ 3*f*f''-2*f'*f' + q = 0
q''+ 2.25*f*q'= 0
Innitial and boundary conditions: f(0)=f'(0)=1-q(0)=f'(infinity)=q(infinity)=0
Results after debugging:
"Step in the coordinate h=0.25
bb -nan<ind> cc -nan<ind> //-nan<ind> means no number
iter=1 dd= -nan<ind>
x=0 then u= 0
x=0 then y= -nan<ind>
x=0 then z= 7
x=0 then w= -nan<ind>
x=0 then p= -3
x=0.25 then u= -nan<ind>
x=0.25 then y= -nan<ind>
x=0.25 then z= -nan<ind>
x=0.25 then w= -nan<ind>
x=0.25 then p= -nan<ind>
.............................etc``

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
double f1(double x, double u, double y, double z, double w, double p)
{
return y;
}
//
double f2(double x, double u, double y, double z, double w, double p)
{
return z;
}
//
double f3(double x, double u, double y, double z, double w, double p)
{
return -3 * u*z + 2 * y*y - w;
}
//
double f4(double x, double u, double y, double z, double w, double p)
{
return p;
}
double f5(double x, double u, double y, double z, double w, double p)
{
return -2.25*u*p;
}    
//
int main(void){
const double EPS = 1e-10;
int i, n = 40;
double h;
    		//
h = 10.0 / n;
//
double *x;
x = new double[n + 1];
x[0] = 0.0;
//
for (i = 0; i<n + 1; i++)
 x[i] = i*h;
 void Outputofarray(double x[], int n);
   {
   printf("\n");
   for (i = 0; i<n + 1; i++)
   {
    cout << x[i] << endl;
   }
    		}
//
cout << "step in the coordinate h= " << h << endl;
    		//
double *u;
u = new double[n + 1];
u[0] = 0.0;
double *y;
y = new double[n + 1];
y[0] = 0.0;
double *z;
z = new double[n + 1];
z[0] = 7.0;
double *w;
w = new double[n + 1];
w[0] = 1.0;
double *p;
p = new double[n + 1];
p[0] = -3.0;
    		//
int iter;
    		//
  double ksi = 0.0, eta = 7.0, gamma = 1.0, epsilon = -3.0;
  double hy, hw, dd, dy, dw;
  double bb, cc, bby, ccy, bbw, ccw;
  double a1, a2, a3, a4;
  double b1, b2, b3, b4;
  double c1, c2, c3, c4;
  double d1, d2, d3, d4;
  double g1, g2, g3, g4;
  iter = 0;
      do
   {
    iter++;
    u[0] = 0.0;
    y[0] = ksi;
    z[0] = eta;
    w[0] = gamma;
    p[0] = epsilon;
//
    for (i = 0; i<n; i++)
    {
    a1 = f1(x[i], u[i], y[i], z[i], w[i], p[i])*h;
    b1 = f2(x[i], u[i], y[i], z[i], w[i], p[i])*h;
    c1 = f3(x[i], u[i], y[i], z[i], w[i], p[i])*h;
    d1 = f4(x[i], u[i], y[i], z[i], w[i], p[i])*h;
    g1 = f5(x[i], u[i], y[i], z[i], w[i], p[i])*h;
    				//
    a2 = f1(x[i] + 0.5*h, u[i] + 0.5*a1, y[i] + 0.5*b1, z[i] + 0.5*c1, w[i] + 0.5*d1, p[i] + 0.5*g1)*h;
    b2 = f2(x[i] + 0.5*h, u[i] + 0.5*a1, y[i] + 0.5*b1, z[i] + 0.5*c1, w[i] + 0.5*d1, p[i] + 0.5*g1)*h;
    c2 = f3(x[i] + 0.5*h, u[i] + 0.5*a1, y[i] + 0.5*b1, z[i] + 0.5*c1, w[i] + 0.5*d1, p[i] + 0.5*g1)*h;
    d2 = f4(x[i] + 0.5*h, u[i] + 0.5*a1, y[i] + 0.5*b1, z[i] + 0.5*c1, w[i] + 0.5*d1, p[i] + 0.5*g1)*h;
    g2 = f5(x[i] + 0.5*h, u[i] + 0.5*a1, y[i] + 0.5*b1, z[i] + 0.5*c1, w[i] + 0.5*d1, p[i] + 0.5*g1)*h;
//
    a3 = f1(x[i] + 0.5*h, u[i] + 0.5*a2, y[i] + 0.5*b2, z[i] + 0.5*c2, w[i] + 0.5*d2, p[i] + 0.5*g2)*h;
    b3 = f2(x[i] + 0.5*h, u[i] + 0.5*a2, y[i] + 0.5*b2, z[i] + 0.5*c2, w[i] + 0.5*d2, p[i] + 0.5*g2)*h;
    c3 = f3(x[i] + 0.5*h, u[i] + 0.5*a2, y[i] + 0.5*b2, z[i] + 0.5*c2, w[i] + 0.5*d2, p[i] + 0.5*g2)*h;
    d3 = f4(x[i] + 0.5*h, u[i] + 0.5*a2, y[i] + 0.5*b2, z[i] + 0.5*c2, w[i] + 0.5*d2, p[i] + 0.5*g2)*h;
    g3 = f5(x[i] + 0.5*h, u[i] + 0.5*a2, y[i] + 0.5*b2, z[i] + 0.5*c2, w[i] + 0.5*d2, p[i] + 0.5*g2)*h;
//

    a4 = f1(x[i] + h, u[i] + a3, y[i] + b3, z[i] + c3, w[i] + d3, p[i] + g3)*h;
    b4 = f2(x[i] + h, u[i] + a3, y[i] + b3, z[i] + c3, w[i] + d3, p[i] + g3)*h;
    c4 = f3(x[i] + h, u[i] + a3, y[i] + b3, z[i] + c3, w[i] + d3, p[i] + g3)*h;
    d4 = f4(x[i] + h, u[i] + a3, y[i] + b3, z[i] + c3, w[i] + d3, p[i] + g3)*h;
    g4 = f5(x[i] + h, u[i] + a3, y[i] + b3, z[i] + c3, w[i] + d3, p[i] + g3)*h;
    
    				//
    u[i + 1] = u[i] + (a1 + 2.0*a2 + 2.0*a3 + a4) / 6.0;
    y[i + 1] = y[i] + (b1 + 2.0*b2 + 2.0*b3 + b4) / 6.0;
    z[i + 1] = z[i] + (c1 + 2.0*c2 + 2.0*c3 + c4) / 6.0;
    w[i + 1] = w[i] + (d1 + 2.0*d2 + 2.0*d3 + d4) / 6.0;
    p[i + 1] = p[i] + (g1 + 2.0*g2 + 2.0*g3 + g4) / 6.0;
    			}
//
    bb = y[n] - 0.0;
    cc = w[n] - 0.0;
    cout << "bb " << bb << " cc " << cc << endl;
    

for (i = 0; i<n + 1; i++)
{
    			cout << "x[i]= " << x[i] << " then u[i]= " << u[i] << endl;
    			cout << "x[i]= " << x[i] << " then y[i]= " << y[i] << endl;
    			cout << "x[i]= " << x[i] << " then z[i]= " << z[i] << endl;
    			cout << "x[i]= " << x[i] << " then w[i]= " << w[i] << endl;
    			cout << "x[i]= " << x[i] << " then p[i]= " << p[i] << endl;
delete[] x;
delete[] u;
delete[] y;
delete[] z;
delete[] w;
delete[] p;
system("pause");
    	}
Last edited on
Your posted code does not compile. A copy-paste error?
The original code is 316 lines but because of content limit I can only post a half the code. The rest of code is quite similar. It is just iteration of the first part
This is how your code ends
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
		for (i = 0; i < n + 1; i++)
		{
			cout << "x[i]= " << x[i] << " then u[i]= " << u[i] << endl;
			cout << "x[i]= " << x[i] << " then y[i]= " << y[i] << endl;
			cout << "x[i]= " << x[i] << " then z[i]= " << z[i] << endl;
			cout << "x[i]= " << x[i] << " then w[i]= " << w[i] << endl;
			cout << "x[i]= " << x[i] << " then p[i]= " << p[i] << endl;
			delete[] x;
			delete[] u;
			delete[] y;
			delete[] z;
			delete[] w;
			delete[] p;
			system ("pause");
		}
so there you are trying to delete several times the same pointers, and also try to dereference them after being deleted.

Create a small example that reproduces your problem. Simplify the equations to solve.
Also, use meaningful names and comments.
http://www.eelis.net/iso-c++/testcase.xhtml
Topic archived. No new replies allowed.