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
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include<math.h>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int n;
double (*f)(int k,double s,double l);
double f1(int k,double s,double l)
{return 0;}
double f2(int k,double s,double l)
{return sin(s);}
double f3(int k,double s,double l)
{return 4*s*s+s-5 ;}
double f4(int k,double s,double l)
{return cos(3*s+1);}
double SplineCount(double xprev,double xi,double yprev,double yi,double mprev,double mi,double xs);
void CountMs(int m,double *x,double *y,double *mi);
double Derivative(int i,int m,double *x,double *y); // Производная
void Gauss(int n,double **ar,double *br,double *ms);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
f=f1; // По умолчанию функция с экспонентой
StringGrid1->Cells[0][0]="x";
StringGrid1->Cells[1][0]="y(x)";
} double SplineCount(double xprev,double xi,double yprev,double yi,double mprev,double mi,double xs)
{
double s;
double droby1,droby2,drobm1,drobm2;
double x_step=xi-xprev;
droby1=(xs-xi)*(xs-xi)*(2*(xs-xprev)+x_step)/pow(x_step,3);
droby2=(xs-xprev)*(xs-xprev)*(2*(xi-xs)+x_step)/pow(x_step,3);
drobm1=(xs-xi)*(xs-xi)*(xs-xprev)/pow(x_step,2);
drobm2=(xs-xprev)*(xs-xprev)*(xs-xi)/pow(x_step,2);
s=yprev*droby1+yi*droby2+mprev*drobm1+mi*drobm2;
return s;
}
//------------------------------------------------------
/* --- Производная по определению ---*/
double Derivative(int i,int m,double *x,double *y) // Производная
{
double der;
if(i==0) // Если первая точка
{
der=(y[1]-y[0])/(x[1]-x[0]); //Правые разности
}
else if(i==m-1) // Если последняя точка
{
der=(y[m-1]-y[m-2])/(x[m-1]-x[m-2]);
}
else // Если внутренняя точка отрезка
der=(y[i+1]-y[i-1])/(x[i+1]-x[i-1]);
return der;
}
//---------------------------
/* --- Вычисление коэффициентов m --- */
void CountMs(int m,double *x,double *y,double *mi)
{
int i,j;
double **a,*b;
a=new double *[m];
for(i=0;i<m;i++)
a[i]=new double [m];
b=new double [m];
double h;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=0;
if(i==0 || i==m-1) a[i][i]=1;
else
{
a[i][i-1]=1;
a[i][i]=4;
a[i][i+1]=1;
}
}
}
for(i=1;i<m-1;i++)
{
h=x[i+1]-x[i];
b[i]=3*(y[i+1]-y[i-1])/(2*h);
}
b[0]=Derivative(0,m,x,y);
b[m-1]=Derivative(m-1,m,x,y);
Gauss(m,a,b,mi);
for(i=0;i<n;i++)
delete a[i];
delete []a;
delete []b;
}
//-------------------------------
void Gauss(int n,double **ar,double *br,double *ms)
{
int k,i,j;
float t;
float sum;
for(i=0;i<n;i++)
{
ms[i]=0;
}
//Расчеты по методу Гаусса
for(k=0;k<n-1;k++)
{
for(i=k+1;i<n;i++)
{
t=ar[i][k]/ar[k][k];
br[i]=br[i]-t*br[k];
for(j=k+1;j<n;j++)
{
ar[i][j]=ar[i][j]-t*ar[k][j];
}
}
}
// Вычисляются неизвестные
for(k=n-1;k>=0;k--)
{
sum=0;
for(j=k+1;j<n;j++)
{
sum=sum+ar[k][j]*ms[j];
}
ms[k]=(br[k]-sum)/ar[k][k];
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ switch (RadioGroup1->ItemIndex)
{
case 0:
{
f=f2;
break;
};
case 1:
{
f=f3;
break;
};
case 2:
{
f=f4;
break;
};
};
Series1->Clear();
Series2->Clear();
Series1->Title="f(x)";
Series2->Title="S(x)";
int i,j;
int n=Edit2->Text.ToInt();
int grPoints=Edit1->Text.ToInt();
double *x,*y,*mi,xs,step,spline;
x=new double [n];
y=new double [n];
StringGrid1->RowCount=Edit2->Text.ToInt()+1;
mi=new double [n+1];
for(i=0;i<n;i++)
{
x[i]=StringGrid1->Cells[0][i+1].ToDouble();
y[i]=StringGrid1->Cells[1][i+1].ToDouble();
}
Series1->LinePen->Width=3;
CountMs(n+1,x,y,mi);
step=(x[n-1]-x[0])/grPoints;
for(xs=x[0];xs<=x[n-1];xs+=step)
{
for(i=0;i<n;i++)
{
if(x[i-1]<=xs && xs<=x[i])
{
spline=SplineCount(x[i-1],x[i],y[i-1],y[i],mi[i-1],mi[i],xs);
}
Series1->AddXY(x[i],y[i],"",Series1->SeriesColor);
}
Series2->AddXY(xs,spline,"",Series2->SeriesColor);
}
}
//--------------------------------------------------------------------------- |