/* PRIVOD1
Программа решения систем дифференциальных уравнений
методом Рунге-Кутта 4-го порядка.
ВХОДНЫЕ ДАННЫЕ:
N-число уравнений; h-шаг интегрирования; t-начальное
время; y[i]-массив начальных значений переменных
(i=0,1,2,...,N); функция вычисления правых частей
дифференциальных уравнений-f;
ВЫХОДНЫЕ ДАННЫЕ:
Результат после одного шага решения в массиве начальных 
значений переменных y[i];текущее время-t.
ВНУТРЕННИЕ ПЕРЕМЕННЫЕ:
py[i]-массив значений правых частей; 
y0[i],y1[i],y2[i]-масивы для хранения промежуточных
значений переменных; t0-время в начале шага;
i,N-целочисленные переменные. 
	При использовании данной программы в качестве 
вызываемой функции необходимо описать одномерные 
массивы y,y0,y1,y2,py размером N (все описания даются
с учётом нулевых значений индексов). Объявить типы
переменных t,h,h1,y,i,N. */

#include <fstream>
#include <iostream>		//библиотечные функции
#include <iomanip>
#include <math.h>

	using namespace std;

 void f(double t,double y[],double py[]);//прототип функции

 int main()
{
	
	const int N=13;			//число дифференциальных ур-ий	
	double y[N]={0};		//начальные условия

	double y0[N];
	double y1[N];
	double y2[N];
	double py[N];
	double t0=0,t=0,h,h1;
	
		h=0.001;			//шаг интегрирования

	int v=20;				//ширина поля печати
	int w=9;				//число знаков 


//---------функции управления выводом данных-----
	ofstream fout("1.txt");
	cout.precision(w);	
	cout.setf(ios::left,ios::adjustfield);
	fout<<setiosflags(ios::fixed);


//----------вывод на экран-----------------
cout<<"Programma C++ METOD RUNGE-KUTTA "<<endl<<endl;

	cout<<setw(v)<<"t"<<setw(v)<<"y11"<<setw(v)<<"y12"<<endl;
	cout<<setw(v)<<t<<setw(v)<<y[11]<<setw(v)<<y[12]<<endl;

//----------вывод в текстовый файл----------
	fout<<setw(v)<<t<<setw(v)<<y[11]<<setw(v)<<y[12]<<endl;
	

	for (int k=0;k<20;k++)		//количество точек на экране
	{
		for (int m=0;m<5;m++)	//число внутренних шагов интегрирования
		{
				t0=t;					//t=tn
			for (int i=0;i<N;i++)		//загрузка y0,y2
			{
				y0[i]=y[i]; y2[i]=y[i];
			};

	/*------1-й цикл-------------*/
				h1=h/2;					
				f(t,y,py);
			for (int i=0;i<N;i++)
			{
				y1[i]=py[i]*h1;		//y1=h/2*f(tn,yn)=K1/2			
				y[i]=y0[i]+y1[i];	//y=yn+K1/2	
				y2[i]=y2[i]+y1[i]/3;//y2=yn+K1/6
			};

	/*------2-й цикл--------------*/
				t=t0+h1;			//t=tn+h/2			
				f(t,y,py);
			for (int i=0;i<N;i++)
			{
				y1[i]=py[i]*h1;		//y1=h/2*f(tn+h/2,yn+K1/2)=K2/2
				y[i]=y0[i]+y1[i];	//y=yn+K2/2
				y2[i]=y2[i]+y1[i]*2/3;//y2=yn+K1/6+K2/3
			};

	/*-------3-й цикл---------------*/
				f(t,y,py);				
			for (int i=0;i<N;i++)
			{
				y1[i]=py[i]*h;		//h*f(tn+h/2,yn+K2/2)=K3
				y[i]=y0[i]+y1[i];	//y=yn+K3
				y2[i]=y2[i]+y1[i]/3;//y2=yn+K1/6+K3/3
			};

	/*-------4-й цикл----------------*/
				t=t0+h;				//t=tn+h					
				f(t,y,py);
			for (int i=0;i<N;i++)
			{
				y1[i]=py[i]*h1;		//y1=h/2*f(tn+h,yn+K3)=K4/2
				y[i]=y2[i]+y1[i]/3;	//y=yn+K1/6+K2/3+K3/3+K4/6
			};

//----------вывод в текстовый файл----------
	fout<<setw(v)<<t<<setw(v)<<y[11]<<setw(v)<<y[12]<<endl;

		};

//----------вывод на экран-----------------
		cout<<setw(v)<<t<<setw(v)<<y[11]<<setw(v)<<y[12]<<endl;



	};

	fout.close();
	return(0);
}

 void f(double t,double y[],double py[])	//Функция вычисления 
											//правых частей ДУ
{
	 double Uv=10, Ky=10, Ty=0.003, R=0.0317, L=0.000337, J=0.242, C=1.127;
	 double Kp=1/R, Tp=L/R, Kd=1/J, Koc=0.0478, Mn=76.4, Kot=C/10.5, Ti=0.002;
	 double Kpt=(Tp-Ti)/(Ti*Ky*Kp*Kot), Kv=1/Koc, Ki=(Kpt*Ky*Kp)/(1+Kpt*Ky*Kp*Kot);
	 double Tv=0.002, Kpc=1/(Tv*Ki*Kd*Koc*C);
	 double U0=0,U1=0,U2=0,U3=0,U4=0,U5=0,U6=0,U7=0,U8=0,U9=0,U10=0,U11=0,U12=0;	//коэффициенты

	U5=Uv-Koc*y[12]-y[7];		//уравнения связи
	U6=y[5];
	U7=U5-y[6];
	U8=Kpc*U5-Kot*y[11]-y[9];
	U9=U8-y[8];
	U10=Kpt*U8;
	U11=y[10]-C*y[12];
	U12=C*y[11]-Mn;
	py[0]=0;					//система ДУ
	py[1]=0;
	py[2]=0;
	py[3]=0;
	py[4]=0;
	py[5]=(U5-y[5])/Ty;
	py[6]=(U6-y[6])/Ti;
	py[7]=Kpc*Ki*C*Kd*Koc*U7;
	py[8]=(U8-y[8])/Ty;
	py[9]=(Kpt*Ky*Kp*Kot*U9-y[9])/Tp;
	py[10]=(Ky*U10-y[10])/Ty;
	py[11]=(Kp*U11-y[11])/Tp;
	py[12]=Kd*U12;

}
 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}