Skip to content

Instantly share code, notes, and snippets.

View BrianWeinstein's full-sized avatar

Brian Weinstein BrianWeinstein

View GitHub Profile
ic[x_, y_] := 1 E^(-350 ((x - 1/5)^2 + ( y - 1/3)^2))
solnDir =
NDSolve[
{D[u[x, y, t], {t, 2}] == D[u[x, y, t], {x, 2}] + D[u[x, y, t], {y, 2}],
u[x, y, 0] == ic[x, y],
(D[u[x, y, t], t] /. t -> 0) == 0,
u[0, y, t] == ic[0, y],
u[1, y, t] == ic[1, y],
u[x, 0, t] == ic[x, 0],
nc = 15; nr = 3;
cx = Table[ToExpression[StringJoin["cx", ToString[i]]], {i, 1, nc}];
cy = Table[ToExpression[StringJoin["cy", ToString[i]]], {i, 1, nc}];
rx = Table[ToExpression[StringJoin["rx", ToString[i]]], {i, 1, nr}];
ry = Table[ToExpression[StringJoin["ry", ToString[i]]], {i, 1, nr}];
coordList = Flatten[{Transpose[{cx, cy}], Transpose[{rx, ry}]}];
cspeed = 1;
rspeed = 1.1;
eqns = Flatten[
Ueff[x_, y_, m1_, x1_, y1_, m2_, x2_, y2_] :=
-((G*m1)/Sqrt[(x - x1)^2 + (y - y1)^2]) -
(G*m2)/Sqrt[(x - x2)^2 + (y - y2)^2] - (G*(m1 + m2)*(x^2 + y^2))/(2*Sqrt[(x1 - x2)^2 + (y1 - y2)^2]^3)
G = 1; m1 = 1; x1 = -1.25; y1 = 0; m2 = 0.45; x2 = 1.25; y2 = 0;
L1 = {FindRoot[D[Ueff[x, 0, m1, x1, y1, m2, x2, y2], x], {x, 0.5}][[1,2]], 0};
L2 = {FindRoot[D[Ueff[x, 0, m1, x1, y1, m2, x2, y2], x], {x, 1.5}][[1,2]], 0};
L3 = {FindRoot[D[Ueff[x, 0, m1, x1, y1, m2, x2, y2], x], {x, -1.5}][[1,2]], 0};
L4 = FindRoot[{D[Ueff[x, y, m1, x1, y1, m2, x2, y2], x] == 0,
is_outlier <- function(x) {
# Computes a bootstrapped confidence interval
# INPUT:
# x: a numeric vector
# OUTPUT:
# a boolean vector, indicating if each value in x is an outlier
return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}
x[A1_, A2_, f1_, f2_, p1_, p2_, d1_, d2_, t_] := A1 Sin[t f1 + p1] E^(-d1 t) + A2 Sin[t f2 + p2] E^(-d2 t)
y[A3_, A4_, f3_, f4_, p3_, p4_, d3_, d4_, t_] := A3 Sin[t f3 + p3] E^(-d3 t) + A4 Sin[t f4 + p4] E^(-d4 t)
Manipulate[
ParametricPlot[
{x[A1, A2, f1, f2, p1, p2, d1, d2, t],
y[A3, A4, f3, f4, p3, p4, d3, d4, t]},
{t, 0, tmax},
PlotPoints -> 200, Axes -> False, PlotStyle -> {Thick, Opacity[0.5]}, PlotRange -> All
(* Rutherford model *)
es[t_] := {{Cos[t + Pi], 2.5*Sin[t + Pi], 2*Sin[t + Pi]},
{Cos[t + (4*Pi)/5], 2*Sin[t + (4*Pi)/5], -1.5*Sin[t + (4*Pi)/5]},
{2*Sin[t + (3*Pi)/5], Cos[t + (3*Pi)/5], 2*Sin[t + (3*Pi)/5]},
{2.5*Sin[t + (2*Pi)/5], Cos[t + (2*Pi)/5], -1.5*Sin[t + (2*Pi)/5]}}
Manipulate[Show[
ParametricPlot3D[Evaluate[es[2*u]], {u, 0, 2*Pi}, PlotStyle -> Directive[Thick, Dotted]],
Graphics3D[
{Specularity[White, 200],
bootstrap_ci <- function(data, sample_size_pct = 0.50, samples = 100, conf_level = 0.95){
# Computes a bootstrapped confidence interval
# INPUT:
# data: a numeric vector
# sample_size_pct: the percentage of the input data to be used in each bootsrapped sample
# samples: the number of samples
# conf_level: the desired confidence level
# OUTPUT:
# a bootstrapped conf_level confidence interval
G = 1;
time = 20;
spScale = 8;
mA = 1.3;
xA0 = 0;
yA0 = 0;
zA0 = 0;
vxA0 = 0;
vyA0 = 0;
@BrianWeinstein
BrianWeinstein / RemoveSparseTermsLarge.R
Last active October 19, 2016 16:43
remove sparse terms on a large document term matrix
# tm::removeSparseTerms attempts to remove sparse terms via slicing a sparse matrix.
# The slicing operation tries to convert the sparse matrix to a dense matrix, but this
# fails if the dense matrix has more than ((2^31) - 1) entries [i.e., if (nrow * ncol) > ((2^31) - 1)]
#
# The error message is
# In nr * nc : NAs produced by integer overflow
#
# Instead of using tm::removeSparseTerms, the following function subsets the sparse matrix directly
# and avoids converting the sparse matrix to a dense one.
x1[t_] := R1*Sin[\[Theta]1[t]]
y1[t_] := (-R1)*Cos[\[Theta]1[t]]
x2[t_] := R1*Sin[\[Theta]1[t]] + R2*Sin[\[Theta]2[t]]
y2[t_] := (-R1)*Cos[\[Theta]1[t]] - R2*Cos[\[Theta]2[t]]
v1[t_] := Sqrt[D[x1[t], t]^2 + D[y1[t], t]^2]
v2[t_] := Sqrt[D[x2[t], t]^2 + D[y2[t], t]^2]
T1[t_] := (1/2)*m1*v1[t]^2
T2[t_] := (1/2)*m2*v2[t]^2
U[t_] := m1*g*y1[t] + m2*g*y2[t]