using System using System Collections Generic using System Linq using

  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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CourseWork_eko
{
class Program
{
/*
* Первое задание - аппроксимация функции
*
* */
static double[] x_samples = new double[] { 0, 1.97, 3.91, 5.8, 6.22, 6.52, 6.87, 8.73, 10.73, 13.62, 15.4 };
static double[] y_samples = new double[] { 800, 840, 880, 905, 970, 995, 1003, 1020, 1030, 1060, 1100 };
static double[] approxKoeff = new double[] { 0.0, 0.0, 0.0 };
static double l = 1, tm0 = 20, tmk = 1400, c = 0.7 * 1000, R = 20.9 * 1000000, hi = 0.8;
static double[] metalTempFirstZone;
static double[] metalTempSecondZone;
static double[,] equationMatrix = new double[3, 3] ;
static double[] rightSide = new double[3];
static void generateMatrix()
{
///////////////Fill matrix with nulls //////////////////////
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
equationMatrix[i, j] = 0;
rightSide[0] = rightSide[1] = rightSide[2] = 0;
////////////////////////////////////////////////////////////
int n = x_samples.Length;
for (int i = 0; i < n; ++i)
{
double x = x_samples[i];
///////// Fill first line in matrix //////////////////
equationMatrix[0, 0] += 1;
equationMatrix[0, 1] += Math.Tan(x / (1 + x) );
equationMatrix[0, 2] += Math.Log(1 + x);
///////// Fill second line in matrix //////////////////
equationMatrix[1, 0] += Math.Tan(x / (1 + x));
equationMatrix[1, 1] += Math.Tan(x / (1 + x)) * Math.Tan(x / (1 + x));
equationMatrix[1, 2] += Math.Log(1 + x) * Math.Tan(x / (1 + x));
///////// Fill third line in matrix //////////////////
equationMatrix[2, 0] += Math.Log(1 + x);
equationMatrix[2, 1] += Math.Tan(x / (1 + x)) * Math.Log(1 + x);
equationMatrix[2, 2] += Math.Log(1 + x) * Math.Log(1 + x);
///////////////////////////////////////////////////////
rightSide[0] += y_samples[i];
rightSide[1] += y_samples[i] * Math.Tan(x / (1 + x));
rightSide[2] += y_samples[i] * Math.Log(1 + x);
}
}
static void gauss_solver()
{
int n = rightSide.Length;
for(int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
{
double value = equationMatrix[i, i];
double koeff = equationMatrix[j, i] / value;
for (int c = 0; c < n; ++c)
equationMatrix[j, c] -= koeff * equationMatrix[i, c];
rightSide[j] -= koeff * rightSide[i];
}
for (int i = n - 1; i >= 0; --i)
for (int j = i - 1; j >= 0; --j)
{
double value = equationMatrix[i, i];
double koeff = equationMatrix[j, i] / value;
for (int c = 0; c < n; ++c)
equationMatrix[j, c] -= koeff * equationMatrix[i, c];
rightSide[j] -= koeff * rightSide[i];
}
for (int i = 0; i < n; ++i)
{
approxKoeff[i] = rightSide[i] / equationMatrix[i, i];
}
}
// First zone 0 <= x <= 15.4
static double firstZone(double x)
{
return approxKoeff[0] + approxKoeff[1] * Math.Tan(x / (1 + x)) + approxKoeff[2] * Math.Log(1 + x);
}
// Second zone 15.4 < x <= 49.6
static double secondZone(double x)
{
return 365.861 - 5.697 * Math.Sin(x) + 258.083 * Math.Log(1 + x);
}
/*/////////////////////////////////////////////////////////////////////////////////////////*/
/* Задание 2 */
/* *///////////
static int rungeStepsCount = 100;
delegate double solveFunc(double x);
static double diffEq(double x, double y)
{
return x - y;
}
static void rungeKutta(double[] temperatureArray, solveFunc func, double x_a, double x_b)
{
double h = (x_b - x_a) / rungeStepsCount;
double x = x_a;
double k1, k2, k3, k4;
for (int i = 0; i < rungeStepsCount; ++i)
{
k1 = h * diffEq(func(x), temperatureArray[i] );
k2 = h * diffEq(func(x + h / 2), temperatureArray[i] + k1 / 2 );
k3 = h * diffEq(func(x + h / 2), temperatureArray[i] + k2 / 2 );
k4 = h * diffEq(func(x + h), temperatureArray[i] + k3);
temperatureArray[i + 1] = temperatureArray[i] + (k1 + 2 * k2 + 2 * k3 + k4) / 6;
x += h;
if (x == 49.6)
break;
}
}
static double currCons(double x)
{
if (x <= 15.4)
{
double tn = firstZone(x);
int index = (int)(x / (15.4 / rungeStepsCount));
return (tn - metalTempFirstZone[index]) / (tmk - tn);
}
else
{
double tn = secondZone(x);
int index = (int)( (x - 15.4) / ((49.6 - 15.4) / rungeStepsCount));
return (tn - metalTempSecondZone[index]) / (tmk - tn);
}
}
static double simpson(double a, double b, int n)
{
double h = (b - a) / n;
double s = currCons(a) + currCons(b);
double x = a;
int c = 1;
for (int i = 1; i < n; ++i)
{
x = x + h;
s += (3 + c) * currCons(x);
c = -c;
}
return s * h / 3;
}
static void Main(string[] args)
{
generateMatrix();
gauss_solver();
metalTempFirstZone = new double[rungeStepsCount + 1];
metalTempSecondZone = new double[rungeStepsCount + 1];
metalTempFirstZone[0] = tm0;
rungeKutta(metalTempFirstZone, firstZone, 0, 15.4);
metalTempSecondZone[0] = metalTempFirstZone[rungeStepsCount];
rungeKutta(metalTempSecondZone, secondZone, 15.4, 49.6);
Console.WriteLine("--------------Результаты вычислений-----------------");
Console.WriteLine("Коэффициенты a,b,c: a = {0:F4}, b = {1:F4}, c = {2:F4}", rightSide[0], rightSide[1], rightSide[2]);
Console.WriteLine("----Распределение температуры металла в печи--------");
Console.WriteLine("------------------Первая зона-----------------------");
double h = 15.4 / rungeStepsCount;
double x = 0;
for (int i = 0; i < metalTempFirstZone.Length; ++i)
{
Console.WriteLine("x = {0:F4} Temperature = {1:F4}", x, metalTempFirstZone[i]);
x += h;
}
x -= h;
Console.WriteLine("------------------Вторая зона-----------------------");
h = (49.6 - 15.4) / rungeStepsCount;
for (int i = 0; i < metalTempFirstZone.Length; ++i)
{
Console.WriteLine("x = {0:F4} Temperature = {1:F4}", x, metalTempSecondZone[i]);
x += h;
}
double oilConsumption = tmk * c / (hi * R) * simpson(0, 49.6, 100);
Console.WriteLine("Потребление топлива: {0:F4} ", oilConsumption);
}
}
}