diff --git a/trunk/as0006315/task_01/.gitignore b/trunk/as0006315/task_01/.gitignore new file mode 100644 index 00000000..be5e9100 --- /dev/null +++ b/trunk/as0006315/task_01/.gitignore @@ -0,0 +1,3 @@ +build +.vscode +CMakeFiles diff --git a/trunk/as0006315/task_01/doc/readme.md b/trunk/as0006315/task_01/doc/readme.md new file mode 100644 index 00000000..0087944f --- /dev/null +++ b/trunk/as0006315/task_01/doc/readme.md @@ -0,0 +1,105 @@ +

Ministry of Education of the Republic of Belarus

+

Educational Institution

+

“Brest State Technical University”

+

Department of Information and Intelligent Technologies

+






+

Laboratory work №1

+

On the discipline “Theory and methods of automatic control”

+

Topic: “Temperature modeling of an object”

+




+

Performed by:

+

Student of the 3rd course

+

Group AS-63

+

Loginov G. O.

+

Supervised by:

+

Sitkovets J. S.

+




+

Brest 2024

+ +--- + +## Task: + +Let's get some object to be controlled. We want to control its temperature, which can be described by this differential equation: + +$$\Large\frac{dy(\tau)}{d\tau}=\frac{u(\tau)}{C}+\frac{Y_0-y(\tau)}{RC} $$ (1) + +where $\tau$ – time; $y(\tau)$ – input temperature; $u(\tau)$ – input warm; $Y_0$ – room temperature; $C,RC$ – some constants. + +After transformation we get these linear (2) and nonlinear (3) models: + +$$\Large y_{\tau+1}=ay_{\tau}+bu_{\tau}$$ (2) + +$$\Large y_{\tau+1}=ay_{\tau}-by_{\tau-1}^2+cu_{\tau}+d\sin(u_{\tau-1})$$ (3) + +where $\tau$ – time discrete moments ($1,2,3{\dots}n$); $a,b,c,d$ – some constants. + +Task is to write program (**С++**), which simulates this object temperature. + +## Example of program output: + +``` bash + Input choice (0 - exit, 1 - linear model, 2 - nonlinear model) +Input choice: 1 +Input A: 2 +Input B: 3.96 +Input current_temperature: 12 +Input warm: 40 +Input time: 5 +TIME LINEAR MODEL +========================= +1 182.4 +2 523.2 +3 1204.8 +4 2568 +5 5294.4 + + Input choice (0 - exit, 1 - linear model, 2 - nonlinear model) +Input choice: 2 +Input A: 8 +Input B: 3 +Input C: 0.1 +Input D: 2.015 +Input current_temperature: 1 +Input warm: 12 +Input time: 7 +TIME NONLINEAR MODEL +========================= +1 8.11881 +2 62.0693 +3 298.928 +4 -9166.23 +5 -341403 +6 -2.54791e+08 +7 -3.51707e+11 +``` + +### Input error handling has also been added: +``` bash + Input choice (0 - exit, 1 - linear model, 2 - nonlinear model) +Input choice: 5 + + ***Input value is not correct*** + +Input choice: р + + ***Input value is not correct*** +``` + +### And exit the program: + +``` bash + Input choice (0 - exit, 1 - linear model, 2 - nonlinear model) +Input choice: 0 +PS C:\Users\kseni\Documents\Универ\3курс\ТМАУ\TMAU-2024-my\TMAU-2024\tasks\task_01\build> +``` + +## The source code + +#### The source code is located in the [src folder](/trunk/as0006315/task_01/src). + +- [CMakeLists.txt](/trunk/as0006315/task_01/src/CMakeLists.txt) - this is a file that contains CMake commands to control the project build process. +- [models.h](/trunk/as0006315/task_01/src/models.h) - header file, contains functions declarations for linear and nonlinear models simulation. +- [models.cpp](/trunk/as0006315/task_01/src/models.cpp) - source file, contains functions definitions for linear and nonlinear models simulation. +- [main.cpp](/trunk/as0006315/task_01/src/main.cpp) - source file, contains the main function that controls the program execution. + diff --git a/trunk/as0006315/task_01/src/CMakeLists.txt b/trunk/as0006315/task_01/src/CMakeLists.txt new file mode 100644 index 00000000..37f6cd6e --- /dev/null +++ b/trunk/as0006315/task_01/src/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.10) +project(MyProject) + +# Добавляем исходные файлы +add_executable(${PROJECT_NAME} main.cpp models.cpp) + +# Добавляем заголовочные файлы +include_directories(${SRC_DIR}) \ No newline at end of file diff --git a/trunk/as0006315/task_01/src/main.cpp b/trunk/as0006315/task_01/src/main.cpp new file mode 100644 index 00000000..cac73e75 --- /dev/null +++ b/trunk/as0006315/task_01/src/main.cpp @@ -0,0 +1,80 @@ +#include "models.h" +#include +#include + +using namespace std; + +void get_liner_model(); +void get_nonlinear_model(); +bool choice_input(int &choice); + +int main() { + void (*point[])() = { []() { exit(0); }, get_liner_model, get_nonlinear_model}; + int choice = 0; + while (true) { + cout << "\n\tInput choice (0 - exit, 1 - linear model, 2 - nonlinear model)\n"; + if (!choice_input(choice)) { + cerr << "\n\a\t\t***Input value is not correct***\n\n"; + continue; + } + point[choice](); + } + + return 0; +} + +template bool input_value(T &value, const string &name) { + while (true) { + cout << "Input " << name << ": "; + if (cin >> value) { + return true; + } + else { + cin.clear(); + cin.ignore(numeric_limits::max(), '\n'); + cerr << "\n\a\t\t***Input value is not correct***\n\n"; + } + } +} + +void get_liner_model() { + double A, B, current_temperature, warm; + int time; + input_value(A, "A"); + input_value(B, "B"); + input_value(current_temperature, "current_temperature"); + input_value(warm, "warm"); + input_value(time, "time"); + LinearModel model(A, B, current_temperature, warm); + model.calculate(time); + model.print(); +} + +void get_nonlinear_model() { + double A, B, C, D, current_temperature, warm; + int time; + input_value(A, "A"); + input_value(B, "B"); + input_value(C, "C"); + input_value(D, "D"); + input_value(current_temperature, "current_temperature"); + input_value(warm, "warm"); + input_value(time, "time"); + NonLinearModel model(A, B, C, D, current_temperature, warm); + model.calculate(time); + model.print(); +} + +bool choice_input(int &choice) { + while (true) { + if (input_value(choice, "choice")) { + if (choice >= 0 && choice <= 2) { + return true; + } + else { + cerr << "\n\a\t\t***Input value is not correct***\n\n"; + } + } + } +} + diff --git a/trunk/as0006315/task_01/src/models.cpp b/trunk/as0006315/task_01/src/models.cpp new file mode 100644 index 00000000..8acbc94a --- /dev/null +++ b/trunk/as0006315/task_01/src/models.cpp @@ -0,0 +1,44 @@ +#include "models.h" + +void ModelingObject::print(const std::map &temps, const std::string &name) { + std::cout.setf(std::ios::left); + std::cout << std::setw(10) << "TIME"; + std::cout << std::setw(30) << name << std::endl; + std::cout << std::setfill('=') << std::setw(25) << "" << std::setfill(' ') << std::endl; + for (auto it = temps.begin(); it != temps.end(); ++it) { + std::cout << std::setw(10) << it->first; + std::cout << std::setw(30) << it->second << std::endl; + } +} + +LinearModel::LinearModel(double a, double b, double current_temperature, double warm) : A(a), B(b), current_temperature(current_temperature), warm(warm) {} + +void LinearModel::calculate(const int& time) { + double temp = current_temperature; + for (int i = 1; i <= time; ++i) { + temp = A * temp + B * warm; + temps[i] = temp; + } +} + +void LinearModel::print() { + ModelingObject::print(this->temps, "LINEAR MODEL"); +} + +NonLinearModel::NonLinearModel(double A, double B, double C, double D, double current_temperature, double warm) : A(A), B(B), C(C), D(D), current_temperature(current_temperature), warm(warm) {} + +void NonLinearModel::calculate(const int& time) { + double curr_temp = current_temperature; + double previous_temperature = 0.0; + double buffer = 0.0; + for (int i = 1; i <= time; ++i) { + buffer = A * curr_temp - B * pow(previous_temperature, 2) + C * warm + D * sin(warm); + previous_temperature = curr_temp; + curr_temp = buffer; + temps[i] = curr_temp; + } +} + +void NonLinearModel::print() { + ModelingObject::print(this->temps, "NONLINEAR MODEL"); +} diff --git a/trunk/as0006315/task_01/src/models.h b/trunk/as0006315/task_01/src/models.h new file mode 100644 index 00000000..d2dd422e --- /dev/null +++ b/trunk/as0006315/task_01/src/models.h @@ -0,0 +1,48 @@ +#ifndef MODELS_H +#define MODELS_H + +#include +#include +#include +#include +#include + +class ModelingObject { +public: + virtual void calculate(const int& time) = 0; + void print(const std::map& temps, const std::string& name); +}; + +class LinearModel : public ModelingObject { +private: + double A; + double B; + double current_temperature; + double warm; + std::map temps; + +public: + LinearModel(double a, double b, double current_temperature, double warm); + void calculate(const int& time); + void print(); +}; + +class NonLinearModel : public ModelingObject { +private: + double A; + double B; + double C; + double D; + double current_temperature; + double previous_temperature; + double warm; + std::map temps; + +public: + NonLinearModel(double A, double B, double C, double D, double current_temperature, double warm); + void calculate(const int& time); + void print(); +}; + +#endif // MODELS_H +