Fight Club 13 Sept

Plotting a histogram of differences

need a vector to take differences of..

small vector

randVecSmall = RandomReal[{0,1},{20}]

ListPlot[randVecSmall]

Max[randVecSmall]
Min[randVecSmall]
Mean[randVecSmall]
StandardDeviation[randVecSmall]

large vector

randVecLarge = RandomReal[{0,1},{20000}];

ListPlot[randVecLarge]

Max[randVecLarge]
Min[randVecLarge]
Mean[randVecLarge]
StandardDeviation[randVecLarge]

Looking to plot histogram of differences

the hard way

newListOfDifferences = Table[randVecLarge[[i+1]] - randVecLarge[[i]],{i,1,Length[randVecLarge]-1}];

Short[newListOfDifferences]

Histogram[newListOfDifferences]

the easy way

Histogram[Differences[randVecLarge],10]

Loops....

Do loop

?Do

j = 2
Do[j= j+2;Print[Sin[ Pi/(j)]],{30}]

j

?For

sequence[n_] :=
Module[
{init=0},
 For[ i=1,i≤ n, i++,init = init + 1/(1+i)];
init
]

sequence[60]

ListPlot[Table[sequence[i],{i,1,1000}]]

Table

theSeq[n_]  := 
Module[
{init=0},
Table[init= init + 1/(1+i),{i,1,n}]
]

theSeq[25]

ListPlot[theSeq[1000]]

Writing a function for a dot product of two vectors

method 1

myDotProduct[vector1_, vector2_] := Sum[vector1[[i]] vector2[[i]],{i,1,Length[vector1]}]

myDotProduct[{a,b,c},{1,2,3}]

making method 1 fail  ( these are failures....)

myDotProduct[ {a, b, c},22]

myDotProduct[ {a, b, c},{1,2}]

method 2

myDotProduct2[vector1_, vector2_] := 
Module[
{len1,len2},
len1 = Length[vector1];
len2 = Length[vector2];
(*Print[{len1,len2,vector1,vector2}];*)
If[len1 ≠ len2, Print["Can't dot vectors of different lengths"]; Return[]];
Sum[vector1[[i]] vector2[[i]],{i,1,len1}]
]

myDotProduct2[{a,b,c},{1,2,3}]

myDotProduct2[ {a, b, c},22]

?Dot

{a,b,c}.{1,2,3}

{a, b, c}.{1,2}

Aesthetics (thinking about graphics)

surface1 = x^2 - 3 x y + y^2

surface2 = x y^2 + y x^2

Plot3D[{surface1,surface2}, {x,-1,1},{y,-1,1},PlotStyle-> {Darker[Green],Yellow},ClippingStyle-> None,PlotPoints-> 24,BoxRatios-> {1,1,1},BaseStyle-> Large,AxesLabel-> {"X","Y","Z"},PlotLabel-> "Intersecting Surfaces",Boxed-> False]

surface1 = x^2 -b  x y + y^2

surface2 = x y^2 + y x^2

Plot3D[{surface1/.b-> -6,surface2}, {x,-1,1},{y,-1,1},PlotStyle-> {Darker[Green],Red},ClippingStyle-> None,PlotPoints-> 24,BaseStyle-> Large,AxesLabel-> {"X","Y","Z"},PlotLabel-> "Intersecting Surfaces",Boxed-> False]

Manipulate[
Plot3D[{surface1/.b->bvalue,surface2}, {x,-1,1},{y,-1,1},PlotStyle-> {{Opacity[0.5],Darker[Green]},Red},ClippingStyle-> None,PlotPoints-> 24,PlotLabel-> "Intersecting Surfaces",Boxed-> False],
{bvalue, -10,10}]

Manipulate[
Plot3D[{surface1/.b->bvalue,surface2}, {x,-bnd,bnd},{y,-bnd,bnd},PlotStyle-> {{Opacity[0.5],Darker[Green]},Red},ClippingStyle-> None,PlotPoints-> 24,PlotLabel-> "Intersecting Surfaces",Boxed-> False],
{bvalue, -10,10},
{bnd,.1,100}]

WHere the three extrema come together

trick, I know there must be an inflection point between any two extrema.

myfunction = a (x) (1-x) + x Log[x] + (1-x) Log[1-x]

Manipulate[
Plot[myfunction/.a-> aval,{x,0,1}],
{aval,-3,3}]

locate the positions where second derivatives vanish

dfun2 = D[myfunction,{x,2}]

solve for the x where the derivatives are zero

Solve[dfun2==0,x]

{{x->((a-(Sqrt[-2 a+(a^2)]))/(2 a))},{x->((a+(Sqrt[-2 a+(a^2)]))/(2 a))}}

Solve[-2 a+(a^2)==0 ,a]

dmyfunction = D[myfunction,x]

looking at behavior of the first direvative, magic moment is when the maxima kisses the mimima.

Manipulate[
Plot[dmyfunction/.a-> aval,{x,0,1}],
{aval,-3,3}]

sorting a list and reversing the sort

list = RandomInteger[{-20,20},{100}]

Sort[list]

Reverse[Sort[list]]

plotting the sequence 1/n

ListPlot[Table[1/j,{j,1,100}]]

Plotting an ellipse ( 2 ways)

ParametricPlot[
{3 Cos[theta], Sin[theta]},{theta,0, 2 Pi}]

Graphics[{Blue, Disk[{0,0}, {3,1}]}]

Show[Graphics[{Blue, Disk[{0,0}, {3,1}]}],Axes-> True,BaseStyle-> White]

Show[Table[Graphics[{Opacity[0.4],Hue[RandomReal[{0,2/3}]], Disk[10{RandomReal[],RandomReal[]}, {RandomReal[],RandomReal[]}]}],{100}]]