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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
| #include <QApplication>
#include<utility>
#include<math.h>
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QLineEdit>
#include<QString>
#include<QPushButton>
#include<QRadioButton>
#include<QLabel>
class Seeker_of_Roots{
float low_verge; //Нижняя граница
float high_verge; //Верхняя граница
short int iter_val; //Количество итераций
const static float eps; //Точность
pair<float,float> *koef_mass; //Массив коэффициентов
public:
Seeker_of_Roots():low_verge(0),high_verge(0),iter_val(0)
{
koef_mass=new pair<float,float>[8];
for(int i=0;i<8;i++)
koef_mass[i].first=koef_mass[i].second=0;
}
short Get_iter_val()const{return iter_val ;}
void Set_Verges(float a,float b){low_verge=a;high_verge=b;}
void Set_Koefs(pair<float,float> *mass)
{
for(int i=0;i<8;i++)
koef_mass[i]=mass[i];
}
private:
float funct(float); //Вычисление функции
bool sign_double_diff_funct(float); //Знак второй производной
float diff_funct(float); //Вычисление производной
public:
float Bisection(); //Метод бисекции
float Step(); //Шаговый метод
float Iter(); //Метод простых итераций
float Niuton(); //Метод касательных(Ньютона)
};
class MyWindow:public QDialog{
Q_OBJECT
public:
MyWindow(QWidget* prnt=0);
private:
Seeker_of_Roots Seek;
QLabel *Result_Window;
QRadioButton *Step;
QRadioButton *Bisection;
QRadioButton *Iter;
QRadioButton *Niuton;
QLineEdit **Lines;
QLineEdit **Verges;
private slots:
void calculate();
};
MyWindow::MyWindow(QWidget *prnt):QDialog(prnt)
{
QLabel *start=new QLabel("Выберите формулу:");
Step=new QRadioButton("Шаговый метод");
Bisection=new QRadioButton("Метод бисекции");
Iter=new QRadioButton("Метод простых итераций");
Niuton=new QRadioButton("Метод касательных(Ньютона)");
Step->setChecked(true);
QLabel* l=new QLabel("Задайте границы поиска:");
Verges=new QLineEdit*[2];
Verges[0]=new QLineEdit;
Verges[0]->setFixedWidth(50);
Verges[0]->setText("0");
Verges[1]=new QLineEdit;
Verges[1]->setFixedWidth(50);
Verges[1]->setText("0");
QHBoxLayout *Box0=new QHBoxLayout;
Box0->addWidget(Verges[0]);
Box0->addWidget(Verges[1]);
QVBoxLayout *Box=new QVBoxLayout;
Box->addWidget(l);
Box->addLayout(Box0);
Box->addWidget(start);
Box->addWidget(Step);
Box->addWidget(Bisection);
Box->addWidget(Iter);
Box->addWidget(Niuton);
Lines=new QLineEdit*[16];
QLabel **Labels=new QLabel*[15];
Lines[0]=new QLineEdit;
Lines[0]->setFixedWidth(50);
Lines[0]->setText("0");
Labels[0]=new QLabel("sin(");
Lines[1]=new QLineEdit;
Lines[1]->setFixedWidth(50);
Lines[1]->setText("0");
Labels[1]=new QLabel("*x)+");
Lines[2]=new QLineEdit;
Lines[2]->setFixedWidth(50);
Lines[2]->setText("0");
Labels[2]=new QLabel("cos(");
Lines[3]=new QLineEdit;
Lines[3]->setFixedWidth(50);
Lines[3]->setText("0");
Labels[3]=new QLabel("*x)+");
Lines[4]=new QLineEdit;
Lines[4]->setFixedWidth(50);
Lines[4]->setText("0");
Labels[4]=new QLabel("tg(");
Lines[5]=new QLineEdit;
Lines[5]->setFixedWidth(50);
Lines[5]->setText("0");
Labels[5]=new QLabel("*x)+");
Lines[6]=new QLineEdit;
Lines[6]->setFixedWidth(50);
Lines[6]->setText("0");
Labels[6]=new QLabel("ln(");
Lines[7]=new QLineEdit;
Lines[7]->setFixedWidth(50);
Lines[7]->setText("0");
Labels[7]=new QLabel("*x)+");
Lines[8]=new QLineEdit;
Lines[8]->setFixedWidth(50);
Lines[8]->setText("0");
Labels[8]=new QLabel("log10(");
Lines[9]=new QLineEdit;
Lines[9]->setFixedWidth(50);
Lines[9]->setText("0");
Labels[9]=new QLabel("*x)+");
Lines[10]=new QLineEdit;
Lines[10]->setFixedWidth(50);
Lines[10]->setText("0");
Labels[10]=new QLabel("x^2+");
Lines[11]=new QLineEdit;
Lines[11]->setFixedWidth(50);
Lines[11]->setText("0");
Labels[11]=new QLabel("x^3+");
Lines[12]=new QLineEdit;
Lines[12]->setFixedWidth(50);
Lines[12]->setText("0");
Labels[12]=new QLabel("x^4+");
Lines[13]=new QLineEdit;
Lines[13]->setFixedWidth(50);
Lines[13]->setText("0");
Labels[13]=new QLabel("x^5+");
Lines[14]=new QLineEdit;
Lines[14]->setFixedWidth(50);
Lines[14]->setText("0");
Labels[14]=new QLabel("x+");
Lines[15]=new QLineEdit;
Lines[15]->setFixedWidth(50);
Lines[15]->setText("0");
QHBoxLayout *Box2=new QHBoxLayout;
for(int i=0;i<8;i++)
{
Box2->addWidget(Lines[i]);
Box2->addWidget(Labels[i]);
}
QHBoxLayout *Box3=new QHBoxLayout;
for(int i=8;i<15;i++)
{
Box3->addWidget(Lines[i]);
Box3->addWidget(Labels[i]);
}
Box3->addWidget(Lines[15]);
Box->addLayout(Box2);
Box->addLayout(Box3);
QPushButton *Calculate=new QPushButton("Посчитать");
Calculate->setMaximumWidth(100);
connect(Calculate,SIGNAL(clicked()),this,SLOT(calculate()));
Box->addWidget(Calculate);
setLayout(Box);
}
void MyWindow::calculate()
{
Result_Window=new QLabel;
pair<float,float> *mass=new pair<float,float>[8];
for(int i=0;i<8;i++)
{
mass[i].first=Lines[i]->text().toDouble();
mass[i].second=Lines[i+1]->text().toDouble();
}
Seek.Set_Koefs(mass);
delete[] mass;
Seek.Set_Verges(Verges[0]->text().toInt(),Verges[1]->text().toInt());
float result(0);
int iter;
if(Step->isChecked())
result=Seek.Step();
else if(Bisection->isChecked())
result=Seek.Bisection();
else if(Iter->isChecked())
result=Seek.Iter();
else if(Niuton->isChecked())
result=Seek.Niuton();
iter=Seek.Get_iter_val();
QString str("Корень:");
str+=QString::number(result);
str+="\nКоличество итераций:";
str+=QString::number(iter);
str+="\n";
QPushButton *ok=new QPushButton("OK");
connect(ok,SIGNAL(clicked()),Result_Window,SLOT(close()));
QLabel *Label=new QLabel(str);
QVBoxLayout *box=new QVBoxLayout;
box->addWidget(Label);
box->addWidget(ok);
Result_Window->setLayout(box);
Result_Window->resize(200,100);
Result_Window->setWindowTitle("Результат");
Result_Window->show();
}
float Seeker_of_Roots::Bisection()
{
iter_val=0;
while((high_verge-low_verge)>eps)
{
iter_val++;
float half=(high_verge+low_verge)/2;
float val_a=funct(low_verge);
float val_half=funct(half);
if(val_a*val_half>0)
low_verge=half;
else if(val_a*val_half<0)
high_verge=half;
else
low_verge=low_verge=half;
}
return (high_verge+low_verge)/2;
}
float Seeker_of_Roots::Step()
{
float step(1.);
float prev(1.);
iter_val=0;
while((high_verge-low_verge)>eps)
{
float temp=low_verge;
int i;
prev=1;
iter_val++;
for(i=0;i<(high_verge-low_verge)/step;i++)
{
if(funct(temp)*funct(prev) < 0 && i!=0)
break;
else
prev=temp;
temp+=step;
}
low_verge=prev;
high_verge=temp;
step/=10;
}
return (high_verge+low_verge)/2;
}
float Seeker_of_Roots::Iter()
{
iter_val=0;
float lyambda=1/max(diff_funct(low_verge),diff_funct(high_verge));
float xi1,xi;
xi=low_verge;
while(true)
{
iter_val++;
xi1=xi-lyambda*(funct(xi));
if(fabs(xi-xi1)<eps)
break;
xi=xi1;
}
return (xi+xi1)/2;
}
float Seeker_of_Roots::Niuton()
{
iter_val=0;
float xi1,xi;
bool sign=(funct(low_verge)<0 ? false:true);
if(sign==sign_double_diff_funct(low_verge))
xi=low_verge;
else
xi=high_verge;
while(true)
{
iter_val++;
xi1=xi-funct(xi)/diff_funct(xi);
if(fabs(xi-xi1)<eps)
break;
xi=xi1;
}
return (xi+xi1)/2;
}
float Seeker_of_Roots::funct(float x)
{
return koef_mass[0].first*sin(koef_mass[0].second*x)+koef_mass[1].first*cos(koef_mass[1].second*x)
+koef_mass[2].first*tan(koef_mass[2].second*x)+koef_mass[3].first*log(koef_mass[3].second*x)+
koef_mass[4].first*log10(koef_mass[4].second*x)+koef_mass[5].first*pow(x,float(2))+koef_mass[5].second*
pow(x,float(3))+koef_mass[6].first*pow(x,float(4))+koef_mass[6].second*pow(x,float(5))+
koef_mass[7].first*x+koef_mass[7].second;
}
float Seeker_of_Roots::diff_funct(float x)
{
return koef_mass[0].first*koef_mass[0].second*cos(koef_mass[0].second*x)-
koef_mass[1].first*koef_mass[1].second*sin(koef_mass[1].second*x)+
koef_mass[2].first*koef_mass[2].second/pow(cos(koef_mass[2].second*x),float(2))+
koef_mass[3].first*koef_mass[3].second/(koef_mass[3].second*x)+
koef_mass[4].first*koef_mass[4].second/(koef_mass[4].second*x*log(float(10)))+
2*koef_mass[5].first*x+3*koef_mass[5].second*pow(x,float(2))+
4*koef_mass[6].first*pow(x,float(3))+5*koef_mass[6].second*pow(x,float(4))+
koef_mass[7].first;
}
bool Seeker_of_Roots::sign_double_diff_funct(float x)
{
return (-koef_mass[0].first*koef_mass[0].second*koef_mass[0].second*sin(koef_mass[0].second*x)-
koef_mass[1].first*koef_mass[1].second*koef_mass[1].second*cos(koef_mass[1].second*x)+2*
koef_mass[2].first*koef_mass[2].second*koef_mass[2].second*sin(koef_mass[2].second*x)/pow(cos(koef_mass[2].second*x),float(3))
-koef_mass[3].first/pow(x,float(2))-koef_mass[4].first/(pow(x,float(2))*log(float(10)))+
2*koef_mass[5].first+6*koef_mass[5].second*x+12*koef_mass[6].first*pow(x,float(2))+
20*koef_mass[6].second*pow(x,float(3)) < 0 ? false:true);
}
const float Seeker_of_Roots::eps=0.0005;
using namespace std;
int main(int argc, char *argv[])
{
QApplication *a=new QApplication(argc, argv);
setlocale(LC_ALL,"rus");
MyWindow *Window=new MyWindow;
Window->setWindowTitle("Методы решения нелинейных уравнений");
Window->show();
return a->exec();
} |