-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab2.java
321 lines (252 loc) · 7.09 KB
/
Lab2.java
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
import java.util.ArrayList;
import java.util.Scanner;
abstract class Calka extends Thread
{
double wynik;
@Override
public void run() {
}
public double result()
{
return wynik;
}
}
class M_prostokatow extends Calka
{
double aii;
double bii;
int nii;
int ii;
public M_prostokatow(double ai, double bi, int ni, int i)
{
aii = ai;
bii = bi;
nii = ni;
ii = i;
}
@Override
public void run()
{
wynik = Metody.Kwadr(aii, bii, nii);
System.out.println("Koniec wątku: "+ii);
}
@Override
public double result()
{
return wynik;
}
}
class M_trapezow extends Calka
{
double aii;
double bii;
int nii;
int ii;
public M_trapezow(double ai, double bi, int ni, int i)
{
aii = ai;
bii = bi;
nii = ni;
ii = i;
}
@Override
public void run()
{
wynik = Metody.Trap(aii, bii, nii);
System.out.println("Koniec wątku: "+ii);
}
@Override
public double result()
{
return wynik;
}
}
class M_Simpsona extends Calka
{
double aii;
double bii;
int nii;
int ii;
public M_Simpsona(double ai, double bi, int ni, int i)
{
aii = ai;
bii = bi;
nii = ni;
ii = i;
}
@Override
public void run()
{
wynik = Metody.Simp(aii, bii, nii);
System.out.println("Koniec wątku: "+ii);
}
@Override
public double result()
{
return wynik;
}
}
class Metody
{
private static int ni;
private static double x;
private static double g;
public static double round(double value, int places) {
double scale = Math.pow(10, places);
return Math.round(value * scale) / scale;
}
public static void main(String[] args)
{
float distance;
Scanner s = new Scanner(System.in);
ArrayList<Calka> watki = new ArrayList<>();
int ni = 8; //dokładność modyfikowalna z tego poziomu
double suma = 0;
System.out.println("Program do całkowania wielowątkowego");
System.out.println("0 - Met. Prostokątów");
System.out.println("1 - Met. Trapezów");
System.out.println("2 - Met. Simpsona");
int met = s.nextInt();
System.out.println("Podaj a: ");
int ai = s.nextInt();
System.out.println("Podaj b: ");
int bi = s.nextInt();
System.out.println("Podaj ilosc watkow: ");
int ilosc_watkow = s.nextInt();
float [] ait = new float[ilosc_watkow];
float [] bit = new float[ilosc_watkow];
distance = (float)(bi - ai)/ilosc_watkow;
distance = (float)round(distance,3);
for(int i = 0; i<ilosc_watkow; i++)
{
ait[i] = (float)round(ai+i*distance,3);
bit[i] = (float)round(ai+(i+1)*distance,3);
}
for(int i = 0; i<ilosc_watkow; i++)
{
if(met == 0)
{
System.out.println("Wpuszczam wątek met. Prostokatow: "+ (i+1));
watki.add(new M_prostokatow (ait[i], bit[i], ni, i+1));
}
else if(met == 1)
{
System.out.println("Wpuszczam wątek met. Trapezow: "+ (i+1));
watki.add(new M_trapezow(ait[i], bit[i], ni, i+1));
}
else if(met == 2)
{
System.out.println("Wpuszczam wątek met. Simpsona: "+ (i+1));
watki.add(new M_Simpsona(ait[i], bit[i], ni, i+1));
}
else
{
break;
}
}
for(int i = 0; i < watki.size(); i++)
{
watki.get(i).start();
}
try
{
for(int i = 0; i < watki.size(); i++)
{
watki.get(i).join();
}
}
catch (InterruptedException e)
{
System.out.println ("error = " + e);
}
for(int i = 0; i < watki.size(); i++)
{
suma += watki.get(i).result();
}
/*
for (int i = 0; i < 10; i++)
{
System.out.println(ait[i]);
System.out.println(bit[i]);
}
*/
System.out.println("Wynik: "+ suma);
s.close();
}
static double funkcja(double x)
{ //tutaj def.funkcji
return (Math.sqrt(1.3*x+2.1))/Math.log(x+0.9);
}
static double Simp(double aii, double bii, int ni) {
double xx = aii;
double xy = bii;
double h;
int n = ni;
double calka;
h = (xy - xx) / (double) n;
calka = 0;
g = 0;
for (int i = 1; i < n; i++) {
x = xx + i * h;
g = g + funkcja(x - h / 2);
calka = calka + funkcja(x);
}
g = g + funkcja(xy - h / 2);
calka = (h / 6) * (funkcja(xx) + funkcja(xy) + 2 * calka + 4 * g);
return calka;
}
static double Trap(double ai, double bi, int ni)
{
double xx= ai;
double xy= bi;
double h;
double calka;
Metody.ni = ni;
int m = Metody.ni;
h = (xy - xx)/m;
calka = 0;
for (int i=1; i<m; i++)
{
calka = calka + funkcja(xx + i * h);
}
calka = calka + (funkcja(xx) + funkcja(xy)) / 2;
calka = calka * h;
return calka;
}
public static double gauss(double poczatek, double koniec, double ilosc, double[] ww, double[] tt)
{
double a = (koniec - poczatek) /2;
double b = (koniec + poczatek) /2;
double wynik = 0;
for(int i=0; i<ilosc; i++)
{
wynik = wynik + ww[i]*funkcja(a*tt[i] + b);
}
return wynik * a;
}
static double Kwadr(double ai, double bi, int ni)
{
int ilosc = ni;
double[] tt = new double[ilosc];
double[] ww = new double[ilosc];
double poczatek = ai;
double koniec= bi;
tt[0] = -0.1834346424956498;
tt[1] = 0.1834346424956498;
tt[2] = -0.5255324099163290;
tt[3] = 0.5255324099163290;
tt[4] = -0.7966664774136267;
tt[5] = 0.7966664774136267;
tt[6] = -0.9602898564975363;
tt[7] = 0.9602898564975363;
ww[0] = 0.3626837833783620;
ww[1] = 0.3626837833783620;
ww[2] = 0.3137066458778873;
ww[3] = 0.3137066458778873;
ww[4] = 0.2223810344533745;
ww[5] = 0.2223810344533745;
ww[6] = 0.1012285362903763;
ww[7] = 0.1012285362903763;
return gauss(poczatek, koniec, 8, ww, tt);
}
}