Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[task_01] Add task_01 #7

Closed
wants to merge 9 commits into from
3 changes: 3 additions & 0 deletions trunk/as0006315/task_01/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
.vscode
CMakeFiles
105 changes: 105 additions & 0 deletions trunk/as0006315/task_01/doc/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<p align="center">Ministry of Education of the Republic of Belarus</p>
<p align="center">Educational Institution</p>
<p align="center">“Brest State Technical University”</p>
<p align="center">Department of Information and Intelligent Technologies</p>
<br><br><br><br><br><br><br>
<p align="center">Laboratory work №1</p>
<p align="center">On the discipline “Theory and methods of automatic control”</p>
<p align="center">Topic: “Temperature modeling of an object”</p>
<br><br><br><br><br>
<p align="right">Performed by:</p>
<p align="right">Student of the 3rd course</p>
<p align="right">Group AS-63</p>
<p align="right">Loginov G. O.</p>
<p align="right">Supervised by:</p>
<p align="right">Sitkovets J. S.</p>
<br><br><br><br><br>
<p align="center">Brest 2024</p>

---

## 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.

8 changes: 8 additions & 0 deletions trunk/as0006315/task_01/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.10)
project(MyProject)

# Добавляем исходные файлы
add_executable(${PROJECT_NAME} main.cpp models.cpp)

# Добавляем заголовочные файлы
include_directories(${SRC_DIR})
80 changes: 80 additions & 0 deletions trunk/as0006315/task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "models.h"
#include <iostream>
#include <limits>

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<typename T> 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<streamsize>::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";
}
}
}
}

44 changes: 44 additions & 0 deletions trunk/as0006315/task_01/src/models.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "models.h"

void ModelingObject::print(const std::map<int, double> &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");
}
48 changes: 48 additions & 0 deletions trunk/as0006315/task_01/src/models.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef MODELS_H
#define MODELS_H

#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>

class ModelingObject {
public:
virtual void calculate(const int& time) = 0;
void print(const std::map<int, double>& temps, const std::string& name);
};

class LinearModel : public ModelingObject {
private:
double A;
double B;
double current_temperature;
double warm;
std::map<int, double> 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<int, double> 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

Loading