From 039469d1135a86dffc8f41ed00da64e0c653c1ee Mon Sep 17 00:00:00 2001 From: vladkuzhyr <77207255+XD-cods@users.noreply.github.com> Date: Sat, 28 Sep 2024 09:38:13 +0300 Subject: [PATCH] [task_01] Done task_01 (#13) * The assignment is complete. Create file main.cpp and readme.md, update gitignore * update folder name * Delete images and create formuls in task_01 readme.md * update readme md * update gitignore --------- Co-authored-by: Yana Sitkovets --- .gitignore | 1 + readme.md | 2 +- trunk/as0006413/task_01/doc/readme.md | 154 +++++++++++++++++++++ trunk/as0006413/task_01/src/CMakeLists.txt | 5 + trunk/as0006413/task_01/src/main.cpp | 92 ++++++++++++ 5 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 trunk/as0006413/task_01/doc/readme.md create mode 100644 trunk/as0006413/task_01/src/CMakeLists.txt create mode 100644 trunk/as0006413/task_01/src/main.cpp diff --git a/.gitignore b/.gitignore index 2cd63d8b..a2f18293 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ # Directories to ignore .vs/ +.vscode out/ build/ diff --git a/readme.md b/readme.md index 9f5231db..b5f2158e 100644 --- a/readme.md +++ b/readme.md @@ -62,7 +62,7 @@ |10|Игнаткевич Кирилл Сергеевич||||||||||| |11|Кашпир Дмитрий Русланович||||||||||| |12|Котковец Кирилл Викторович||||||||||| -|13|Кужир Владислав Витальевич||||||||||| +|13|Кужир Владислав Витальевич|[as0006313](./trunk/as0006413/)|:white_check_mark:||||||||| |14|Немирович Дмитрий Александрович||||||||||| |15|Попов Алексей Сергеевич||||||||||| |16|Рабченя Максим Юрьевич||||||||||| diff --git a/trunk/as0006413/task_01/doc/readme.md b/trunk/as0006413/task_01/doc/readme.md new file mode 100644 index 00000000..3f4a8801 --- /dev/null +++ b/trunk/as0006413/task_01/doc/readme.md @@ -0,0 +1,154 @@ + 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-64 + + Kuzhyr U. V. + + 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 τ – time; y ( τ ) – input temperature; u ( τ ) – input warm; Y 0 – room temperature; C , R C – 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 τ – time discrete moments ( 1 , 2 , 3 … n ); a , b , c , d – some constants. + +Task is to write program (С++), which simulates this object temperature. +Example of program output: + +``` + +Choose an option (0 - exit, 1 - linear model, 2 - nonlinear model): Input choice: 1 +Input A: 12 +Input B: 2 +Input current temperature: 36 +Input heat input: 2 +Input simulation time: 10 +TIME Linear Model +========================= +1 436 +2 5236 +3 62836 +4 754036 +5 9.04844e+06 +6 1.08581e+08 +7 1.30297e+09 +8 1.56357e+10 +9 1.87628e+11 +10 2.25154e+12 + +Choose an option (0 - exit, 1 - linear model, 2 - nonlinear model): Input choice: 2 +Input A: 3 +Input B: 15 +Input C: 20 +Input D: 25 +Input current temperature: 27 +Input heat input: 37 +Input simulation time: 20 +TIME Nonlinear Model +1 804.912 +2 -7796.35 +3 -9.7409e+06 +4 -9.40969e+08 +5 -1.42328e+15 +6 -1.32856e+19 +7 -3.03859e+31 +8 -2.64761e+39 +PS C:\Users\user\Desktop\TMAU-2024\build\trunk\as006417\task_01\src> ./lab_01.exe + +Choose an option (0 - exit, 1 - linear model, 2 - nonlinear model): Input choice: 1 +Input A: 1.1 +Input B: 0.5 +Input current temperature: 25 +Input heat input: 10 +Input simulation time: 5 +TIME Linear Model +========================= +1 32.5 +2 40.75 +3 49.825 +4 59.8075 +5 70.7883 + +Choose an option (0 - exit, 1 - linear model, 2 - nonlinear model): Input choice: 2 +Input A: 1.2 +Input B: 0.1 +Input C: 0.3 +Input D: 0.05 +Input current temperature: 30 +Input heat input: 15 +Input simulation time: 5 +TIME Nonlinear Model +========================= +1 40.5325 +2 -36.8285 +3 -203.95 +4 -375.841 +``` + +## Input erroe handling + +``` +Choose an option (0 - exit, 1 - linear model, 2 - nonlinear model): Input choice: p + +*** Incorrect input *** +Input choice: 5 +*** Invalid input *** +``` + +## Exit the program +``` +Choose an option (0 - exit, 1 - linear model, 2 - nonlinear model): Input choice: 0 +PS C:\Users\user\Desktop\TMAU-2024\build\trunk\as006417\task_01\src> ^C +``` + +The source code + +The source code is located in the src folder. + + CMakeLists.txt - this is a file that contains CMake commands to control the project build process. + main.cpp - source file, contains the main function that controls the program execution. diff --git a/trunk/as0006413/task_01/src/CMakeLists.txt b/trunk/as0006413/task_01/src/CMakeLists.txt new file mode 100644 index 00000000..c5b94f2c --- /dev/null +++ b/trunk/as0006413/task_01/src/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required (VERSION 3.0.0) + +project (lab_01) + +add_executable(${PROJECT_NAME} main.cpp) \ No newline at end of file diff --git a/trunk/as0006413/task_01/src/main.cpp b/trunk/as0006413/task_01/src/main.cpp new file mode 100644 index 00000000..bf8ddc23 --- /dev/null +++ b/trunk/as0006413/task_01/src/main.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +using namespace std; + +void run_linear_model(); +void run_nonlinear_model(); +bool get_choice(int &choice); + +void get_input(double &value, const string &prompt) { + cout << prompt << ": "; + while (!(cin >> value)) { + cin.clear(); + cin.ignore(numeric_limits::max(), '\n'); + cout << "Invalid input! Try again.\n" << prompt << ": "; + } +} + +void get_input(int &value, const string &prompt) { + cout << prompt << ": "; + while (!(cin >> value)) { + cin.clear(); + cin.ignore(numeric_limits::max(), '\n'); + cout << "Invalid input! Try again.\n" << prompt << ": "; + } +} + +void display_results(const map &temps, const string &model_type) { + cout << "\n--- " << model_type << " Results ---\n"; + for (const auto &entry : temps) { + cout << "Step " << entry.first << ": Temperature = " << entry.second << endl; + } +} + +void run_linear_model() { + double A, B, temp, heat; + int steps; + get_input(A, "Enter coefficient A"); + get_input(B, "Enter coefficient B"); + get_input(temp, "Enter initial temperature"); + get_input(heat, "Enter heat input"); + get_input(steps, "Enter number of steps"); + + map temps; + for (int i = 1; i <= steps; ++i) { + temp = A * temp + B * heat; + temps[i] = temp; + } + display_results(temps, "Linear Model"); +} + +void run_nonlinear_model() { + double A, B, C, D, temp, heat; + int steps; + get_input(A, "Enter coefficient A"); + get_input(B, "Enter coefficient B"); + get_input(C, "Enter coefficient C"); + get_input(D, "Enter coefficient D"); + get_input(temp, "Enter initial temperature"); + get_input(heat, "Enter heat input"); + get_input(steps, "Enter number of steps"); + + map temps; + double prev_temp = 0; + for (int i = 1; i <= steps; ++i) { + double new_temp = A * temp - B * pow(prev_temp, 2) + C * heat + D * sin(heat); + prev_temp = temp; + temp = new_temp; + temps[i] = temp; + } + display_results(temps, "Nonlinear Model"); +} + +bool get_choice(int &choice) { + get_input(choice, "Choose option (0 - Exit, 1 - Linear, 2 - Nonlinear)"); + return choice >= 0 && choice <= 2; +} + +int main() { + void (*models[])() = { []() { exit(0); }, run_linear_model, run_nonlinear_model }; + int choice; + + while (true) { + if (!get_choice(choice)) { + cout << "Invalid choice! Try again.\n"; + } else { + models[choice](); + } + } +}