forked from rodrigoavalente/LibSimulation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lsim.cpp
155 lines (126 loc) · 3.15 KB
/
lsim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "lsim.h"
Lsim::Lsim()
{
}
void Lsim::addIO(Matrix in, Matrix out)
{
this->input = in;
this->output = out;
}
void Lsim::modelCoef(Matrix coef)
{
this->X = coef;
}
void Lsim::polyModel(int grau)
{
this->Model = "polinomial";
this->input.print();
this->A.ones(this->input.getRows(),1);
for(int i = 1; i <= grau; i++)
this->A = this->A|(this->input>i);
}
void Lsim::simPoly(int grau)
{
this->polyModel(grau);
this->b = this->A*this->X;
}
void Lsim::polyCoef(int grau)
{
this->polyModel(grau);
this->X = ((~this->A*this->A)^-1)*(~this->A)*this->output;
}
//Modelos ARX (Auto Recursive with eXogenous output)
void Lsim::arxModel(int ny, int nu)
{
int maxNuNy,minRowInOut;
this->Model = "ARX";
if(ny>nu)
maxNuNy = ny;
else
maxNuNy = nu;
if(this->output.getRows() > this->input.getRows())
minRowInOut = this->input.getRows();
else
minRowInOut = this->output.getRows();
for(int i = maxNuNy; i < minRowInOut; i++)
{
this->b.add(i-maxNuNy+1,1,this->output.getMat( i, 1));
for(int j = 0; j < nu+ny; j++)
{
if(j<ny)
this->A.add(i-maxNuNy+1,j+1,-this->output.getMat( i-ny+j,1));
else
this->A.add(i-maxNuNy+1,j+1,this->input.getMat( i-nu-ny+j, 1));
}
}
}
void Lsim::simArx(int ny, int nu)
{
this->arxModel(ny,nu);
this->b = this->A*this->X;
}
void Lsim::arxCoef(int ny, int nu)
{
this->arxModel(ny,nu);
this->X = ((~this->A*this->A)^-1)*(~this->A)*this->b;
}
void Lsim::eqdifModel(float h)
{
Matrix dy, d2y;
dy = diff(this->output,h);
d2y = diff(dy,h);
for(int i = 0; i < d2y.getRows(); i++)
{
this->A.add(i+1,1,-this->output.getMat( i, 1));
this->A.add(i+1,2,-dy.getMat( i, 1));
this->A.add(i+1,3,this->input.getMat( i, 1));
}
this->b = d2y;
}
void Lsim::eqdifCoef(float h)
{
this->eqdifModel(h);
this->X = ((~this->A*this->A)^-1)*(~this->A)*this->b;
}
Matrix Lsim::getInput()
{
return this->input;
}
Matrix Lsim::getOutput()
{
return this->output;
}
void addIO(const char *namefile, Lsim Sis)
{
ifstream myfile(namefile);
string data;
try
{
if (!myfile.is_open())
throw "Nao foi possivel abrir o arquivo";
else
{
int posBar;
string input = "", output = "", time = "";
while(!myfile.eof())
{
getline( myfile, data);
posBar = data.find("|");
input = input + data.substr( 0, posBar) + ";";
data.erase( 0, posBar+1);
posBar = data.find("|");
output = output + data.substr(0, posBar) + ";";
data.erase( 0, posBar+1);
time = time + data + ";";
}
myfile.close();
Sis.input = input;
Sis.output = output;
Sis.PeriodicTime = time;
}
}
catch(const char* msg)
{
cerr<<msg<<endl;
}
}