Here is the code for the 4-order Runge-Kutta method for a 1-order control system. It seems all right, but the answer is not correct. Where is the mistake. Tell me please. The answer should be x11 = -0,9 x22 = 0,4 (something like this)

 #include <stdio.h> #include <iostream> #include <math.h> using namespace std; double f1(double x1, double x2, double t)// первая функция { return(-52*x1-100*x2+exp(-t)); } double f2(double x1, double t)// вторая функция { return(x1+sin(t)); } int main() { setlocale(LC_ALL,"rus"); double h; //cout << " Введите h = ";//шаг //cin >> h; //int n; //n=10000; //cout << "Введите n = ";//число точек //cin >> n; double k1=0; double k2=0; double k3=0; double k4=0; double m1=0; double m2=0; double m3=0; double m4=0; double x1=0; double x2=0; double x11=0; double x22=0; double t=0; //cout << "enter x[0] = "; //cin >> x[0]; //cout << "enter y[0] = "; //cin >> y[0]; t = 0; // начальне значения x1 = 1; // начальне значения x2 = 0; // начальне значения h = 0.01; // шаг int i = 0; while(t<2.0) { k1 = h*f1(x1, x2, t); m1 = h*f2(x1, t); k2 = h*f1(x1 + k1/2., x2 + m1/2., t+h/2.); m2 = h*f2(x1 + k1/2., t+h/2.); k3 = h*f1(x1 + k2/2., x2 + m2/2., t+h/2.); m3 = h*f2(x1 + k2/2., t+h/2.); k4 = h*f1(x1 + k3, x2 + m3, t+h); m4 = h*f2(x1 + k3, t+h); x11 = x1 + (1. / 6.)*(k1 + 2. * k2 + 2. * k3 + k4); x22 = x2 + (1. / 6.)*(m1 + 2. * m2 + 2. * m3 + m4); i++; x1 = x1 + h; x2 = x2 + h; t = t + h; cout<<"t = "<< t << " x11 = "<< x11 << " x22 = " << x22 << endl; } system("pause"); return 0; } 

1 answer 1

You should still correct your original question, and not create a new one. Well, see for yourself what you are doing -

  x1 = x1 + h; x2 = x2 + h; 

Those. Is everything counted like a cat's tail and replaced with a simple linear function? ...

Here is the solution "according to you":

 #include <iostream> #include <iomanip> #include <cmath> using namespace std; double f1(double x1, double x2, double t) // первая функция { return -52 * x1 - 100 * x2 + exp(-t); } double f2(double x1, double t) // вторая функция { return x1 + sin(t); } int main() { //setlocale(LC_ALL,"rus"); double h = 0.01; // шаг double x1 = 1.0; double x2 = 0.0; double t = 0.0; cout << setw(10) << t << setw(12) << x1 << setw(12) << x2 << endl; while (t <= 2.0) { double k1 = h*f1(x1,x2,t); double m1 = h*f2(x1,t); double k2 = h*f1(x1+k1/2,x2+m1/2,t+h/2); double m2 = h*f2(x1+k1/2,t+h/2); double k3 = h*f1(x1+k2/2,x2+m2/2,t+h/2); double m3 = h*f2(x1+k2/2,t+h/2); double k4 = h*f1(x1+k3,x2+m3,t+h); double m4 = h*f2(x1+k2,t+h); x1 += (k1+2*k2+2*k3+k4)/6; x2 += (m1+2*m2+2*m3+m4)/6; t += h; cout << setw(10) << t << setw(12) << x1 << setw(12) << x2 << endl; } system("pause"); return 0; } 

http://ideone.com/2cerCs