НЕ получается вывести Y, подскажите в чем ошибка и как исправить?(
24.04.2013, 20:07. Показов 395. Ответов 0
C++ | 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
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include "library.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String S;
int N;
float L0, L9, X1, X0, E1, E, Y;
N = 3.0;
L0 = 1.0;
L9 = 10.0;
E = 0.01;
E1 = 0.01;
X1 = L9;
X0 = L0;
float *L = new float [N];
for (int i=0; i<N; i++)
{ if(i==0)
{
L[0]=L9;
}
else
{
L[i] = 0;
}
}
Tab rt;
float **mas = Get_mem3(N,N);
Vuvod_mas2(Memo1, N, mas);
rt = post(L0, L9, N, mas);
rt = Koff(N, rt);
for (int j = 0; j<N; j++)
{
Y = xar(N, X1, X0, rt, j, E1, E, L);
L[j] = Y;
}
Del_mem1(L); // óäàëÿåì ïàìÿòü..
Del_mem2(N,mas);
} |
|
C++ | 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
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "library.h"
#include "math.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
float *Get_mem1(int n)
{
float *mas = new float [n];
for (int i=0; i<n; i++)
{
mas[i] = 0;
}
return mas;
}
void Del_mem1(float *buffer)
{
delete[]buffer;
}
float **Get_mem2(int x, int y)
{
float **buffer;
buffer = new float *[x];
for (int i=0; i<x; i++)
{
buffer[i]=new float [y];
}
for (int j=0; j<x; j++)
{
for (int k=0; k<x; k++)
{
buffer[j][k] = 0;
}
}
return buffer;
}
void **Del_mem2(int x, float **buffer)
{
for (int i=0; i<x; i++)
{
delete[]buffer[i];
}
delete[]buffer;
return 0;
}
void Vuvod_mas2(TMemo *Memox, int N, float **mas)
{
String S;
for (int i=0; i<N; i++)
{
S.sprintf("");
Memox -> Lines -> Add(S);
for(int j=0; j<N; j++)
S.cat_sprintf("%5.3lf ",mas[i][j]);
Memox -> Lines -> Add(S);
}
}
float Det_Matrix ( float **Matrix, int n)
{
float t, d , max;
int i, j , k;
d = 1.0;
for ( k = 0; k < n; k ++)
{
max = 0.0;
for (i = k; i < n ; i ++)
{
t = Matrix[i][k];
if ( fabs (t) > fabs (max))
{
max = t; j = i;
}
}
if ( fabs (max) < 0.000001) return 0;
if ( j != k)
{
d =- d;
for ( i = k; i < n;i ++)
{
t = Matrix[j][i];
Matrix[j][i] = Matrix[k][i];
Matrix[k][i] = t;
}
}
for ( i = k + 1; i < n; i ++)
{
t = Matrix[i][k] /max;
for ( j = k +1; j < n; j ++)
{
Matrix[i][j] = Matrix[i][j]- t *Matrix[k][j];
}
}
d = d* Matrix[k][k];
}
return d;
}
Tab post(float L0, float L9, int N,float **B)
{
Tab mas;
float H,S;
H = (L9-L0)/N;
float R1 = L0;
float **A = Get_mem2(N,N);
mas.F =Get_mem1(N);
mas.X =Get_mem1(N);
for (int M = 0; M <= N; M++)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
A[i][j]=B[i][j];
if (i==j)
{
A[i][j]=B[i][j]-R1;
}
}
}
S = Det_Matrix(A,N);
mas.F[M]=S;
mas.X[M]=R1;
R1 = R1 +H;
}
Del_mem2(N,A);
return mas;
}
Tab Koff(int n, Tab mas)
{
for (int j=1; j<=n; j++)
{
float P = mas.F[j-1];
float R = mas.X[j-1];
for (int i=j; i<=n; i++)
{
mas.F[i] = (P-mas.F[i])/(R-mas.X[i]);
}
}
return mas;
}
float pol(int N, float Y, Tab mas, int j, float *L)
{
float P = mas.F[N];
for(int i =N-1; i>=0;i--)
{
P=mas.F[i]+(Y-mas.X[i])*P;
}
for (int i=0; i<j; i++)
{
P=P/(L[i]-Y);
}
return P;
}
float xar(int N, float X1,float X0, Tab mas, int j, float E1, float E, float *L)
{
float Y = X1;
float P = pol(N, Y,mas, j, L);
float R = X0-X1;
Y=X0;
float D = P;
P = pol(N, Y,mas, j, L);
if (fabs(P)<E1)
{
return Y;
}
R = R/(D-P)*P;
Y=Y+R;
while (fabs(R)>E)
{
D = P;
P = pol(N, Y,mas, j, L);
if (fabs(P)<E1)
{
return Y;
}
R = R/(D-P)*P;
Y=Y+R;
}
Del_mem1(mas.F);
Del_mem1(mas.X);
return Y;
}
float **Get_mem3(int x, int y)
{
float **buffer;
buffer = new float *[x];
for (int i=0; i<x; i++)
{
buffer[i]=new float [y];
}
for (int j=0; j<x; j++)
{
for (int k=0; k<x; k++)
{
buffer[j][k] = 5;
}
}
return buffer;
} |
|
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| //---------------------------------------------------------------------------
#ifndef libraryH
#define libraryH
struct Tab
{
float *X;
float *F;
};
float *Get_mem1(int n);
void Del_mem1(float *buffer);
float **Get_mem2(int x, int y);
void **Del_mem2(int x, float **buffer);
void Vuvod_mas2(TMemo *Memox, int N, float **mas);
float Det_Matrix ( float **Matrix, int n);
Tab post(float L0, float L9, int N,float **B);
Tab Koff(int n, Tab mas);
float pol(int N, float Y, Tab mas, int j, float *L);
float xar(int N, float X1,float X0, Tab mas, int j, float E1, float E, float *L);
float **Get_mem3(int x, int y); |
|
0
|