RungeKutta class C++

I have an assigment in uni to write a program, that integrates multiple differential equations. d/dx=-ay, d/dy=ax. I have to do it using RungeKutta class and a virtual MyRungeKutta. Now I am a bit stuck and getting "MyRungeKutta was not declared in this scope error" Any ideas would be appreciated. Thanks

Main.cpp
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
  // 20 task
#include <iostream>
#include <iomanip>
#include <math.h>
#include "RungeKutta.h"

using namespace std;

int main()
{
    int a=1;
    int n=2;
    double t=0.0, h=0.1;

   cout << "***    Runges ir Kutos Metodas: Masyvai    ***" << endl;
   cout << "   dx[0]/dt = -x[1],  dx[1]/dt=0   " << endl;
   cout << "    t = 0: x[0]=1, x[1] = 0" << endl;
   cout << setw(3) << " t    " << setw(3) << "x[0]  " << setw(3) << "   cos(t)  " << setw(3) << " x[1]    " <<  setw(3) << "sin(t)   " << endl;
    cout << "--------------------------------------" << endl;

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

        MyRungeKutta X(n);
        X[0]=1;
        X[1]=0;

        cout << setw(3) << fixed << setprecision(1) << t << "  ";
        cout << setw(3) << fixed << setprecision(4) << X[0] << "   ";
        cout << setw(3) << fixed << setprecision(4) << cos(t) << "   ";
        cout << setw(3) << fixed << setprecision(4) << X[1] << "   ";
        cout << setw(3) << fixed << setprecision(4) << sin(t) << endl;

        t=t+h;

   }

   return 0;
}


RungeKutta.h
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
#ifndef RUNGEKUTTA_H_INCLUDED
#define RUNGEKUTTA_H_INCLUDED

#include <assert.h>

class RungeKutta
{
public:
    RungeKutta(int ln);
    ~RungeKutta();
    void step();
    virtual void fk(const double x, double *f)const;
    virtual void fkh(const double x, double *fh)const;
    double& operator[](int i){return x[i];}
protected:
    int n;          //number of equations
    double *x;      //pointer to function
};

RungeKutta::RungeKutta(int ln)
{
    n=ln;
    x = new double[n];
}

RungeKutta::~RungeKutta()
{
    delete []x;
}


void RungeKutta::step(){
    double f[]={0.0,0.0,0.0,0.0};

    fk(*x,f);
    x[2]=x[0]+0.1*f[0];
    x[3]=x[1]+0.1*f[1];
    fkh(*x,f);
    x[0]=x[0]+0.1/2*(f[0]+ f[2]);
    x[1]=x[1]+0.1/2*(f[1]+ f[3]);
}

#endif // RUNGEKUTTA_H_INCLUDED


RungeKutta.cpp
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
#include "RungeKutta.h"
#include <stdio.h>

class MyRungeKutta:public RungeKutta
{
public:
    MyRungeKutta(int ln):RungeKutta(ln){};
    void set(double la){a=la;}
    void fk(const double *x, double *f);
    void fkh(const double *x, double *f);
private:
    double a;
};


void MyRungeKutta::fk(const double *x, double *f)
{
   f[0] = -x[1]*a;
   f[1] = x[0]*a;

}

void MyRungeKutta::fkh(const double *x, double *f)
{
    f[2] = -x[3]*a;
    f[3] = x[2]*a;
}
Last edited on
What you currently have as RungeKutta.cpp should be split into MyRungeKutta.cpp and MyRungeKutta.h

Some of what is presently in RungeKutta.h should be in a new RungeKutta.cpp file.

The class definition goes into .h files.
The class method implementations go into .cpp files.

Put all three .cpp files in your project.
I created MyRungeKutta.cpp and MyRungeKutta.h, and also included it in main.cpp, but now i am getting more errors.
redefinition of 'RungeKutta::RungeKutta(int)' and also all other RugeKutta functions are redefined.
class MyRungeKutta does not have any field named "RungeKutta'
You don't #include one .cpp inside another.

Sorry, I only included .h files
Your virtual functions in the base class need a default implementation, unless you make them pure virtual functions.
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
/* myRungeKutta.h */
/* ================== */
#ifndef MyRungeKutta_h
#define MyRungeKutta_h
#include "RungeKutta.h"

class MyRungeKutta:public RungeKutta
{
public:
    MyRungeKutta(int ln):RungeKutta(ln){};
    void set(double la){a=la;}
    virtual void fk(const double *x, double *f)const;
    virtual void fkh(const double *x, double *f)const;
private:
    double a;
};
#endif
/* RungeKutta.h */
/* ================== */
#ifndef RungeKutta_h
#define RungeKutta_h
class RungeKutta
{
public:
    RungeKutta(int ln);
    ~RungeKutta();
    void step();
    virtual void fk(const double x, double *f)const { };
    virtual void fkh(const double x, double *fh)const { };
    double& operator[](int i){return x[i];}
protected:
    int n;          //number of equations
    double *x;      //pointer to function
};
#endif
/* main.cpp */
/* ================== */
#include <iostream>
#include <iomanip>
#include <math.h>
#include "myRungeKutta.h"

using namespace std;

int main()
{
    int a=1;
    int n=2;
    double t=0.0, h=0.1;

   cout << "***    Runges ir Kutos Metodas: Masyvai    ***" << endl;
   cout << "   dx[0]/dt = -x[1],  dx[1]/dt=0   " << endl;
   cout << "    t = 0: x[0]=1, x[1] = 0" << endl;
   cout << setw(3) << " t    " << setw(3) << "x[0]  " << setw(3) << "   cos(t)  " << setw(3) << " x[1]    " <<  setw(3) << "sin(t)   " << endl;
    cout << "--------------------------------------" << endl;

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

        MyRungeKutta X(n);
        X[0]=1;
        X[1]=0;

        cout << setw(3) << fixed << setprecision(1) << t << "  ";
        cout << setw(3) << fixed << setprecision(4) << X[0] << "   ";
        cout << setw(3) << fixed << setprecision(4) << cos(t) << "   ";
        cout << setw(3) << fixed << setprecision(4) << X[1] << "   ";
        cout << setw(3) << fixed << setprecision(4) << sin(t) << endl;

        t=t+h;

   }

   return 0;
}
/* myRungeKutta.cpp */
/* ================== */
#include "myRungeKutta.h"

void MyRungeKutta::fk(const double *x, double *f) const
{
   f[0] = -x[1]*a;
   f[1] = x[0]*a;

}

void MyRungeKutta::fkh(const double *x, double *f) const
{
    f[2] = -x[3]*a;
    f[3] = x[2]*a;
}
/* RungeKutta.cpp */
/* ================== */
#include "RungeKutta.h"

RungeKutta::RungeKutta(int ln)
{
    n=ln;
    x = new double[n];
}

RungeKutta::~RungeKutta()
{
    delete []x;
}


void RungeKutta::step(){
    double f[]={0.0,0.0,0.0,0.0};

    fk(*x,f);
    x[2]=x[0]+0.1*f[0];
    x[3]=x[1]+0.1*f[1];
    fkh(*x,f);
    x[0]=x[0]+0.1/2*(f[0]+ f[2]);
    x[1]=x[1]+0.1/2*(f[1]+ f[3]);
}

Thank you salem c. Now the program compiles. But it seems that it doesnt invoke fk and fkh fukctions, because X[0] and X[1] values stay constant.
So this is my code right now. main.cpp calls step() function from RungeKutta.cpp and step() should call fk() and fkh() from MyRungeKutta.cpp, but nothing happens. Program runs without errors, but x[0] and x[1] values do not change. I put cout<<"test"; in fk() function and it does not appear in program window. Any help would be appreciated. Thanks
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
//---------------------------------------------------------------------
/* MyRungeKutta.h */
/*=============*/
#ifndef MYRUNGEKUTTA_H_INCLUDED
#define MYRUNGEKUTTA_H_INCLUDED

#include "RungeKutta.h"
#include <stdio.h>

class MyRungeKutta:public RungeKutta
{
public:
    MyRungeKutta(int ln):RungeKutta(ln){};
    ~MyRungeKutta(){};
    void set(double la){a=la;}
    virtual void fk(const double *x, double *f)const;
    virtual void fkh(const double *x, double *f)const;
private:
    double a;
};
#endif // MYRUNGEKUTTA_H_INCLUDED

/*RungeKutta.h*/
/*===========*/

#ifndef RUNGEKUTTA_H_INCLUDED
#define RUNGEKUTTA_H_INCLUDED

#include <assert.h>

class RungeKutta
{
public:
    RungeKutta(int ln);
    ~RungeKutta();
    void step();
    virtual void fk(const double x, double *f) const {};
    virtual void fkh(const double x, double *fh) const {};
    double& operator[](int i){return x[i];}
protected:
    int n;          //number of equations
    double *x;      //pointer to function
    double *f;
};
#endif // RUNGEKUTTA_H_INCLUDED

/*MyRungeKutta.cpp*/
/*==============*/
#include "MyRungeKutta.h"
#include <iostream>
#include <stdio.h>
#include <iomanip>

using namespace std;
//---------------------------------------------------
void MyRungeKutta::fk(const double *x, double *f) const
{
    cout << "test";
    f[0] = -x[1];
    f[1] = x[0];
}
//-------------------------------------------------
void MyRungeKutta::fkh(const double *x, double *f) const
{
    f[2] = -x[3]*a;
    f[3] = x[2]*a;
}

/*RungeKutta.cpp*/
/*===============*/
#include "RungeKutta.h"
#include <iostream>
#include <iomanip>

using namespace std;

RungeKutta::RungeKutta(int ln)
{
    n=ln+2;
    x = new double[n];
    f= new double[4];
}
//--------------------------------
RungeKutta::~RungeKutta()
{
    delete []x;
    delete []f;
}
//----------------------------------
void RungeKutta::step(){

    fk(*x,f);
    x[2]=x[0]+0.1*f[0];
    x[3]=x[1]+0.1*f[1];
    //fkh(*x,f);
    x[0]=x[0]+0.1/2*(f[0]+ f[2]);
    x[1]=x[1]+0.1/2*(f[1]+ f[3]);
}

/*main.cpp*/
/*=============*/
// 20 Uzduotis
#include <iostream>
#include <iomanip>
#include <math.h>
//#include "RungeKutta.h"
#include "MyRungeKutta.h"

using namespace std;

int main()
{
    int a=1;
    int n=2;
    double t=0.0, h=0.1;

   cout << "***    Runges ir Kutos Metodas: Masyvai    ***" << endl;
   cout << "   dx[0]/dt = 1,  dx[1]/dt=0   " << endl;
   cout << "    t = 0: x[0]=1, x[1] = 0" << endl;
   cout << setw(3) << " t    " << setw(3) << "x[0]  " << setw(3) << "   cos(t)  " << setw(3) << " x[1]    " <<  setw(3) << "sin(t)   " << endl;
    cout << "--------------------------------------" << endl;

    MyRungeKutta X(n);
        X[0]=1;
        X[1]=0;
        X.set(a);

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

        cout << setw(3) << fixed << setprecision(1) << t << "  ";
        cout << setw(3) << fixed << setprecision(4) << X[0] << "   ";
        cout << setw(3) << fixed << setprecision(4) << cos(t) << "   ";
        cout << setw(3) << fixed << setprecision(4) << X[1] << "   ";
        cout << setw(3) << fixed << setprecision(4) << sin(t) << endl;
        X.step();
        t=t+h;

   }

   return 0;
}
Last edited on
You need to make the signatures match.
1
2
3
4
5
6
7
class MyRungeKutta
    virtual void fk(const double *x, double *f)const;
    virtual void fkh(const double *x, double *f)const;

class RungeKutta
    virtual void fk(const double x, double *f) const {};
    virtual void fkh(const double x, double *fh) const {};

The base class lacks pointer to x
Thank you very much. You were right, base class lacked pointer to x. Also call to fk() function had to be fk(x,f) instead of fk(*x, *f).
Topic archived. No new replies allowed.