Review Session 1 Nov. 2009

When to use expressions; when to use functions

Expressions when repeated computation is not needed---i.e., something static

x = xo + vo t + a t^2/2

Want to know expression for a particular case---use replacements and rules

what is x if a=g, vo=0, xo=0

specificX = x/.{xo->0, vo->0, a-> -9.8}

Craig prefers to work symbollicly

specificX = x/.{xo->0, vo->0, a-> -g}

wcc never puts in values until very end (why, because I lose track of what is what)

Use "assigned expressions" thing = something when you want to operating on thing
Use "functions" function[x_] := DoSomethingWithX when you want to operation using funciton

Functions when things are repeated over and over, and the function's "duty" is more important than the code that produces it.

c

quadsol[a_,b_,c_] := Solve[a s^2 + b s + c==0,s]

quadsol[1,1,c]

theroots[a_, b_, c_] := s/.quadsol[a,b,c]

theroots[1,1,c]

Plot[Evaluate[theroots[1,1,c]],{c,-20,20}]

Manipulate[
GraphicsColumn[{b,Plot[Evaluate[theroots[1,b,c]],{c,-20,20}]}
],{b,-20,20}]

Manipulate[
GraphicsColumn[{b,Plot[Evaluate[theroots[a,b,c]],{c,-20,20}]}
],{a,-20,20},{b,-20,20}]

How to use ToString to include useful graphical info.

aPlot[a_, b_]:=Plot[Evaluate[theroots[a,b,c]],{c,-20,20},PlotLabel-> StringJoin["Linear Term = ", ToString[a],"   quadratic term= ", ToString[b]]]

aPlot[1,2]

Manipulate[aPlot[a,b],{a,-20,20},{b,-20,20}]

Recursion (when to use a previous value to compute a current value)

Example of not using "remembered" previous values but using a function to call itself (recursion)

randomWalkStep[i_] := randomWalkStep[i-1] + RandomInteger[{-1,1},{2}]

randomWalkStep[0]= {0,0}

aRandomWalkSequence = Table[randomWalkStep[i],{i,1,200}];
aRandomWalkSequence//Short

This was "the result of 200 ****different*** random walks

ListLinePlot[aRandomWalkSequence]

A better way to do (what was probably intended) (remember the position of the previous step

A Tangent conversation: when functions use the a known value or use the function defintion.

aFunc[x_] := 1000^x

aFunc[jim] = 12

aFunc[sally]

aFunc[jim]

Clear[aRandomWalkSequence,randomWalkStep]

remember the location of any step that is taken.

randomWalkStep[i_] := (randomWalkStep[i] = randomWalkStep[i-1] + RandomInteger[{-1,1},{2}])

randomWalkStep[0] = {0,0}

aRandomWalkSequence = Table[randomWalkStep[i],{i,1,400}];
aRandomWalkSequence//Short

ListLinePlot[aRandomWalkSequence]

An alternative method with goal of stopping the walk when the distance is 100

NewStep := RandomInteger[{-1,1},{2}]

build up a walk by appending to a list of steps, the first step starts at {0,0}

beginning = {0,0}

aWalk= {beginning}

aWalk

While[Norm[Last[aWalk]] <  100, aWalk= Append[aWalk,Last[aWalk] + NewStep]]

aWalk//Short

ListLinePlot[aWalk]

Craig showing off, ignore this....

NestWhileList[(# + NewStep)&,{0,0},(Norm[#] <100)&]//Short

gFunc = Function[{x,y}, Cos[x Sin[y]]]

gFunc[1,2]

Pure Function (advanced examples)

Plot[Sin[Sin[x]/x],{x,0,10 Pi},ColorFunction-> Function[{x},Hue[x]]]

Plot[Sin[Sin[x]/x],{x,0,10 Pi},ColorFunction-> Function[{x,y},Hue[y]]]

Plot[Sin[Sin[x]/x],{x,0,10 Pi},ColorFunction-> Function[{x,y},Hue[y]]]

Plot[Sin[Sin[x]/x],{x,0,10 Pi},ColorFunction-> (Hue[#2]&)]

ArcTan[0,1]

Plot[Sin[Sin[x]/x],{x,0,10 Pi},ColorFunction-> (Hue[ArcTan[#1,#2]]&)]

End of advanced purefunction examples

Use Table to construct lists (typically)

aList = Table[ i^3,{i,1,20}]

What is the cube of 6? (use double brackets)

aList[[6]]

Multiple values: 4-10

aList[[4;;10]]

Table[aList[[i]],{i,4,10}]

Evens only examples

EvenQ[3]

EvenQ[2]

Select uses a criterion of true or false

Select[aList,EvenQ]

cases uses a pattern which matches or doesnt

Cases[aList,a_/;EvenQ[a]]

Velazquez asking a difficult question, he want to pick out members of a list that only have 1,3,7 as at least one of the digits

HasDigit[input_,digit_] := 
Module[
{digitList= IntegerDigits[input]},
MemberQ[digitList,digit]
]
HasDigit[123,4]

aList = Table[ i^3,{i,1,100}];

Cases[aList,a_/;(HasDigit[a,1]|| HasDigit[a,3] || HasDigit[a,7])]

End of Velazquez challenge!

Try not to use While and For, but use Table or Nest or Map

Ramachandran asks for Map (what follows is an advanced example)

Suppose you wish to use a function on each element in a list...

Map[Cos,{Pi,Pi/3,Pi/2}]

Generalize

Map[Cos, Table[Pi/n,{n,1,20}]]

Generalize more, supose that each element of the list is a list itself...

atab=Table[RandomReal[],{i,1,10},{j,1,10}];
atab//Short

Map[Total,atab]

Generalize more, supose that I want to do something different to each member of a list.

atab = Table[{i,2 i },{i,1,20}]

Map[(#[[1]]^#[[2]])&,atab]

End of AR's question...

Kish Kuestion: How to make complex things out of simpler things, and how to get the simple things back

ran2x2:= RandomInteger[{1,10},{2,2}]

ran2x2

aMatofMats = Table[ran2x2,{i,1,3},{j,1,4}];
MatrixForm[aMatofMats]

aMatofMats

Investigating the structure of a complicated object

Dimensions[aMatofMats]

aMatofMats[[1]]

aMatofMats[[1,1]]

aMatofMats[[1,1,1]]

aMatofMats[[1,1,1,1]]

aMatofMats[[1,2]]

aMatofMats[[2,1]]

rn := RandomInteger[{1,5},{3}]

anInstance = Table[rn,{10}]

Table[Total[anInstance[[i]]],{i,1,Length[anInstance]}]

Problem of finding the isotropic Cijkl

kdelta[i_,j_] := If[i==j,1,0]

Cijkl[i_,j_,k_, l_]:=
lambda kdelta[i,j]kdelta[k,l] +
 mu (kdelta[i,k]kdelta[j,l]+
kdelta[i,l]kdelta[j,k])

Cijkl[1,2,2,1]

allC = Table[Cijkl[i,j,k,l],{i,1,3},{j,1,3},{k,1,3},{l,1,3}];
allC//MatrixForm

Cijkl[p_,q_] := Module[
{arg1,arg2,arg3,arg4},
If[p ≤ 3,arg1=arg2=p];
If[p == 4, arg1=2; arg2=3];
If[p == 5, arg1=3; arg2=1];
If[p == 6, arg1=1; arg2=2];
If[q≤ 3,arg3=arg4=q];
If[q == 4, arg3=2; arg4=3];
If[q== 5, arg3=3; arg4=1];
If[q== 6, arg3=1; arg4=2];
Cijkl[arg1,arg2,arg3,arg4]
]

Cijkl[2,2]

Table[Cijkl[p,q],{p,1,6},{q,1,6}]//MatrixForm