double double 05 double double -4 Matrix mat vec double double return

  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
double a = 0.4;
double h = 0.05;
double p = 5;
double q = -4;
Matrix mat;
vec d;
double f(double x)
{
return x*x - 5;
}
double v(double x)
{
return 2 + (-3.5) / 0.5*(x-0.4);
}
double F(double x)
{
return f(x) - p*(-3.5) / 0.5 - q*v(x);
}
double Integ(double a, double b, unsigned n, const std::function<double(double x)> f ) {
double h = (b - a) / n;
double sum = 0;
double x0 = a;
double x1 = a + h;
for (int i = 0; i <= n - 1; i++) {
sum += f(x0) + 4 * f(x0 + h / 2) + f(x1);
x0 += h;
x1 += h;
}
return (h / 6)*sum;
}
double phi(double X, int i)
{
if (X >= x[i - 1] && X <= x[i])
return (X - x[i - 1]) / h;
else if (X >= x[i - 1] && X <= x[i + 1])
return -1 / h * (X - x[i + 1]);
else
return 0;
}
int Gauss(Matrix a, vec b, vec &res)
{
for (int k = 0; k < n; ++k)
{
double amax = a[k][k];
int mmax = k;
for (int m = k + 1; m < n; ++m)
{
if (fabs(amax) < fabs(a[m][k]))
{
amax = a[m][k];
mmax = m;
}
}
if (amax == 0) return 0;
else
{
std::swap(b[k], b[mmax]);
for (int j = k; j < n; ++j)
{
std::swap(a[k][j], a[mmax][j]);
}
}
for (int i = k + 1; i < n; ++i)
{
double t = a[i][k] / a[k][k];
b[i] -= t * b[k];
for (int j = k + 1; j < n; ++j)
{
a[i][j] -= t * a[k][j];
}
}
}
res[n - 1] = b[n - 1] / a[n - 1][n - 1];
for (int k = n - 2; k >= 0; --k)
{
double s = 0;
for (int j = k + 1; j < n; ++j)
s += a[k][j] * res[j];
res[k] = (b[k] - s) / a[k][k];
}
}
double Solve(double sX)
{
for (int i = 1; i <= n; ++i)
{
mat[i-1][i-1] = -2 / h + 1 / h / h*(p*Integ(x[i - 1], x[i], 6, [&i](double X) -> double{ return X - x[i-1]; }) +
q*Integ(x[i - 1], x[i], 6, [&i](double X) -> double{ return (X - x[i - 1])*(X - x[i - 1]); }) +
p*Integ(x[i], x[i+1], 6, [&i](double X) -> double{ return X - x[i + 1]; }) +
q*Integ(x[i], x[i+1], 6, [&i](double X) -> double{ return (X - x[i + 1])*(X - x[i + 1]); })
);
if (i < n)
mat[i-1][i] = 1 / h - 1 / h / h*(p*Integ(x[i], x[i + 1], 6, [&i](double X) -> double{ return X - x[i + 1]; }) +
q*Integ(x[i], x[i + 1], 6, [&i](double X) -> double{ return (X - x[i + 1])*(X-x[i]); }));
if (i > 1)
mat[i-1][i - 2] = 1 / h - 1 / h / h*(p*Integ(x[i-1], x[i], 6, [&i](double X) -> double{ return X - x[i + 1]; }) +
q*Integ(x[i-1], x[i], 6, [&i](double X) -> double{ return (X - x[i - 1])*(X - x[i]); }));
d[i - 1] = 1 / h*(Integ(x[i - 1], x[i], 6, [&i](double X) -> double {return F(X)*(X - x[i-1]); }) -
Integ(x[i], x[i + 1], 6, [&i](double X) -> double {return F(X)*(X - x[i + 1]); })
);
}
vec res(n);
Gauss(mat, d, res);
double u = 0;
for (int i = 1; i <= n; ++i)
u+= res[i-1]*phi(sX, i);
return v(sX) + u;
}
int main()
{
n = (b - a) / h - 1;
mat.assign(n, vec(n, 0));
d.assign(n, 0);
double cur = a;
for (int i = 0; i < n + 2; ++i)
{
x.push_back(cur);
cur += h;
}
std::cout << "y(" << 0.65 <<")" << " = " << Solve(0.65) << std::endl;
}