-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatsStuff.sce
More file actions
194 lines (160 loc) · 9.21 KB
/
StatsStuff.sce
File metadata and controls
194 lines (160 loc) · 9.21 KB
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
// THIS FILE WILL HOLD MANY STATISTICAL FUNCTIONS //
function avg = PlotRandMean(N)
// THIS FUNCTION PLOTS THE MEAN OF A 1, 2, ..., N-DIMENSIONAL ARRAY. //
// THE X-AXIS WILL BE THE LENGTH OF THE ARRAY, THE Y-AXIS THE AVERAGE. //
// THE ARRAY'S ARE RANDOMLY GENERATED WITH rand(). //
avg = zeros(1, N);
szArr = 1:N
for ii = szArr
avg(ii) = mean(rand(1, ii));
end
plot(szArr, avg);
xgrid;
endfunction
// //
// PiApprox# WILL BE ATTEMPTS AT APPROXIMATING
function pseudoPi = PiApprox1(tol)
// SET UP THE CIRCLE INSIDE A 1x1, BLACK LINE, DOTTED. //
t = linspace(0, %pi*2); plot(cos(t), sin(t), 'k-');
xgrid;
// NOW WE NEED A LIST FOR OUR RANDOM POINTS (z), AND ANOTHER ONE FOR //
// THEIR DISTANCE, ADJACENTLY, (delZ). SO LONG AS THE DIFFERENCE IS //
// GREATER THAN THE TOLERANCE, KEEP GENERATING POINTS. WE OFFICIALLY //
// START WITH 10 POINTS. HOWEVER, THINK A BIT MORE ABOUT WHAT'S HAPPENING //
// IN THE PROBLEM, WHAT WE EXPECT, WHAT WILL ACTUALLY HAPPEN, CHANCES OF //
// THAT HAPPENING, ETC, ETC. FOR NOW, LET'S SEE WHAT HAPPENS ON THIS TRY.//
// THESE GUYS ARE PRETTY SELF-EXPLANATORY(WUT?). BUT JUST IN CASE: //
// cntArr(1, 2) : The count of all points made so far. //
// cntArr(1, 2) : The count of all points inside the circle so far. //
// tst : ...points. This array is (N, 1, 2)-dimensional (?). That //
// is, it's an array of the points being taken. //
// pseudoPi : The approximation of pi. //
// holder : Holds the older version of the value pseudoPi. //
// DECLARING ALL VARIABLES. //
cntArr(1, 2) = 0; cntArr(1, 2) = 0; cntArr(1, 2) = 0; tst = zeros(1, 1, 2); pseudoPi = 0;
holder = 0;
// IT'D BE NICE IF WE HAD SOME DIVERSITY... THIS WHILE-LOOP ENSURES THAT //
// WE GET MORE POINTS IN THE SQUARE THAN IN THE CIRCLE. //
// IT ALSO UPDATES holder AND pseudoPi AS IT GOES ALONG. //
while cntArr(1, 2) == cntArr(1, 2)
cntArr(1, 2) = cntArr(1, 2) + 1;
// _ _ V---- That's the 'randomizer'. //
// | (-1)^n 0 | n/k are either 1 or 0 //
// |_ 0 (-1)^k_| //
randA = [(-1)^(round(rand())) 0 ; 0 (-1)^(round(rand()))];
pnt = rand(1, 2)*randA;
tst(cntArr(1, 2), 1, :) = pnt;
dist = tst(cntArr(1, 2), 1, 1)^2 + tst(cntArr(1, 2), 1, 2)^2;
if dist <= 1 then
cntArr(1, 2) = cntArr(1, 2) + 1;
plot(tst(cntArr(1, 2), 1, 1), tst(cntArr(1, 2), 1, 2), 'or');
else
plot(tst(cntArr(1, 2), 1, 1), tst(cntArr(1, 2), 1, 2), 'ob');
end
holder = pseudoPi;
pseudoPi = cntArr(1, 2) / (cntArr(1, 2) / 4);
end
while abs(holder - pseudoPi) > tol
cntArr(1, 2) = cntArr(1, 2) + 1;
// _ _ V---- That's the 'randomizer'. //
// | (-1)^n 0 | n/k are either 1 or 0 //
// |_ 0 (-1)^k_| //
randA = [(-1)^(round(rand())) 0 ; 0 (-1)^(round(rand()))];
pnt = rand(1, 2)*randA;
tst(cntArr(1, 2), 1, :) = pnt;
dist = tst(cntArr(1, 2), 1, 1)^2 + tst(cntArr(1, 2), 1, 2)^2;
if dist <= 1 then
cntArr(1, 2) = cntArr(1, 2) + 1;
plot(tst(cntArr(1, 2), 1, 1), tst(cntArr(1, 2), 1, 2), 'or');
else
plot(tst(cntArr(1, 2), 1, 1), tst(cntArr(1, 2), 1, 2), 'ob');
end
holder = pseudoPi;
pseudoPi = cntArr(1, 2) / (cntArr(1, 2) / 4);
end
pseudoPi = pseudoPi * 4;
endfunction
function pseudoStuff = PiApprox2(tol)
// SET UP THE CIRCLE INSIDE A 1x1, BLACK LINE, DOTTED. //
figure;
t = linspace(0, %pi/2); plot(cos(t), sin(t), 'k-');
xgrid;
// NOW WE NEED A LIST FOR OUR RANDOM POINTS (z), AND ANOTHER ONE FOR //
// THEIR DISTANCE, ADJACENTLY, (delZ). SO LONG AS THE DIFFERENCE IS //
// GREATER THAN THE TOLERANCE, KEEP GENERATING POINTS. WE OFFICIALLY //
// START WITH 10 POINTS. HOWEVER, THINK A BIT MORE ABOUT WHAT'S HAPPENING //
// IN THE PROBLEM, WHAT WE EXPECT, WHAT WILL ACTUALLY HAPPEN, CHANCES OF //
// THAT HAPPENING, ETC, ETC. FOR NOW, LET'S SEE WHAT HAPPENS ON THIS TRY.//
// THESE GUYS ARE PRETTY SELF-EXPLANATORY(WUT?). BUT JUST IN CASE: //
// cntArr(1, 2) : The count of all points made so far. //
// cntArr(1, 2) : The count of all points inside the circle so far. //
// tst : ...points. This array is (N, 1, 2)-dimensional (?). That //
// is, it's an array of the points being taken. //
// pseudoPi : The approximation of pi. //
// holder : Holds the older version of the value pseudoPi. //
// DECLARING ALL VARIABLES. //
cntArr(1, 2) = 0; cntArr(1, 2) = 0; cntArr(1, 2) = 0; tst = zeros(1, 1, 2);
pseudoPi = 0; holder = 0; approxHolder = []; approxHolder(1) = 1; appHolInd = 2;
// TODO : MAKE AN ALGORITHM THAT KEEPS NUMERATOR AND DENOMINATOR //
// TODO : RELATIVELY PRIME. ONCE THE TOLERANCE HAS BEEN MET, DON'T FORGET //
// TODO : TO MULTIPLY BY FOUR. //
while cntArr(1, 2) == cntArr(1, 2) | cntArr(1, 2) == 0 | abs(holder - pseudoPi) > tol
cntArr(1, 2) = cntArr(1, 2) + 1;
tst(cntArr(1, 2), 1, :) = rand(1, 2);
dist = tst(cntArr(1, 2), 1, 1)^2 + tst(cntArr(1, 2), 1, 2)^2;
if dist <= 1 then
cntArr(1, 2) = cntArr(1, 2) + 1;
plot(tst(cntArr(1, 2), 1, 1), tst(cntArr(1, 2), 1, 2), '+r');
else
plot(tst(cntArr(1, 2), 1, 1), tst(cntArr(1, 2), 1, 2), 'ob');
end
holder = pseudoPi;
pseudoPi = 4*cntArr(1, 2) / cntArr(1, 2);
approxHolder(appHolInd) = pseudoPi; appHolInd = appHolInd + 1;
disp(pseudoPi);
end
figure(); plot(approxHolder);
pseudoStuff = [pseudoPi cntArr(1, 2)];
endfunction
function pseudoStuff = PiApprox3(tol)
// SET UP THE CIRCLE INSIDE A 1x1, BLACK LINE, DOTTED. //
figure;
t = linspace(0, %pi/2); plot(cos(t), sin(t), 'k-');
xgrid;
// NOW WE NEED A LIST FOR OUR RANDOM POINTS (z), AND ANOTHER ONE FOR //
// THEIR DISTANCE, ADJACENTLY, (delZ). SO LONG AS THE DIFFERENCE IS //
// GREATER THAN THE TOLERANCE, KEEP GENERATING POINTS. WE OFFICIALLY //
// START WITH 10 POINTS. HOWEVER, THINK A BIT MORE ABOUT WHAT'S HAPPENING //
// IN THE PROBLEM, WHAT WE EXPECT, WHAT WILL ACTUALLY HAPPEN, CHANCES OF //
// THAT HAPPENING, ETC, ETC. FOR NOW, LET'S SEE WHAT HAPPENS ON THIS TRY.//
// THESE GUYS ARE PRETTY SELF-EXPLANATORY(WUT?). BUT JUST IN CASE: //
// cntArr(1, 2) : The count of all points made so far. //
// cntArr(1, 2) : The count of all points inside the circle so far. //
// tst : ...points. This array is (N, 1, 2)-dimensional (?). That //
// is, it's an array of the points being taken. //
// pseudoPi : The approximation of pi. //
// holder : Holds the older version of the value pseudoPi. //
// DECLARING ALL VARIABLES. //
cntArr(1, 2) = 0; cntArr(1, 2) = 0; cntArr(1, 2) = 0; tst = zeros(1, 1, 2);
pseudoPi = 0; holder = 0; approxHolder = []; approxHolder(1) = 1; appHolInd = 2;
// TODO : MAKE AN ALGORITHM THAT KEEPS NUMERATOR AND DENOMINATOR //
// TODO : RELATIVELY PRIME. ONCE THE TOLERANCE HAS BEEN MET, DON'T FORGET //
// TODO : TO MULTIPLY BY FOUR. //
while cntArr(1, 2) == cntArr(1, 2) | cntArr(1, 2) == 0 | abs(holder - pseudoPi) > tol
cntArr(1, 2) = cntArr(1, 2) + 1;
tst(cntArr(1, 2), 1, :) = rand(1, 2);
dist = tst(cntArr(1, 2), 1, 1)^2 + tst(cntArr(1, 2), 1, 2)^2;
if dist <= 1 then
cntArr(1, 2) = cntArr(1, 2) + 1;
plot(tst(cntArr(1, 2), 1, 1), tst(cntArr(1, 2), 1, 2), '+r');
else
plot(tst(cntArr(1, 2), 1, 1), tst(cntArr(1, 2), 1, 2), 'ob');
end
holder = pseudoPi;
pseudoPi = 4*cntArr(1, 2) / cntArr(1, 2);
approxHolder(appHolInd) = pseudoPi; appHolInd = appHolInd + 1;
disp(pseudoPi);
end
figure(); plot(approxHolder);
pseudoStuff = [pseudoPi cntArr(1, 2)];
endfunction